2012-01-23 80 views
6

我有一个问题已经今早解决: Java Mail, sending multiple attachments not workingJava邮件 - 附件&&内嵌图像

这一次我有一个稍微复杂的问题:我想附加的文件与图像相结合。

import java.io.IOException; 
import java.util.Properties; 

import javax.activation.DataHandler; 
import javax.activation.FileDataSource; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.AddressException; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeBodyPart; 
import javax.mail.internet.MimeMessage; 
import javax.mail.internet.MimeMultipart; 

public class MailTest 
{ 

    public static void main(String[] args) throws AddressException, MessagingException, IOException 
    { 
     String host = "***"; 
     String from = "***"; 
     String to = "***"; 

     // Get system properties 
     Properties props = System.getProperties(); 

     // Setup mail server 
     props.put("mail.smtp.host", host); 

     // Get session 
     Session session = Session.getDefaultInstance(props, null); 

     // Define message 
     MimeMessage message = new MimeMessage(session); 
     message.setFrom(new InternetAddress(from)); 
     message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); 
     message.setSubject("Hello JavaMail"); 

     // Handle attachment 1 
     MimeBodyPart messageBodyPart1 = new MimeBodyPart(); 
     messageBodyPart1.attachFile("c:/Temp/a.txt"); 

     // Handle attachment 2 
     MimeBodyPart messageBodyPart2 = new MimeBodyPart(); 
     messageBodyPart2.attachFile("c:/Temp/b.txt"); 

     FileDataSource fileDs = new FileDataSource("c:/Temp/gti.jpeg"); 
     MimeBodyPart imageBodypart = new MimeBodyPart(); 
     imageBodypart.setDataHandler(new DataHandler(fileDs)); 
     imageBodypart.setHeader("Content-ID", "<myimg>"); 
     imageBodypart.setDisposition(MimeBodyPart.INLINE); 

     // Handle text 
     String body = "<html><body>Elotte<img src=\"cid:myimg\" width=\"600\" height=\"90\" alt=\"myimg\" />Utana</body></html>"; 

     MimeBodyPart textPart = new MimeBodyPart(); 
     textPart.setHeader("Content-Type", "text/plain; charset=\"utf-8\""); 
     textPart.setContent(body, "text/html; charset=utf-8"); 

     MimeMultipart multipart = new MimeMultipart("mixed"); 

     multipart.addBodyPart(textPart); 
     multipart.addBodyPart(imageBodypart); 
     multipart.addBodyPart(messageBodyPart1); 
     multipart.addBodyPart(messageBodyPart2); 

     message.setContent(multipart); 

     // Send message 
     Transport.send(message); 
    } 
} 

当我打开Gmail中的一切电子邮件是好的:我有两个附件,并且图像显示在邮件内容中(img标签)。

问题出在Thunderbird和RoundCubic webmail上:每一个像图片一样显示都丢失,并以附件的形式显示在底部。

我该如何做这项工作?

+0

Microsoft Outlook 2010不允许内嵌图像!这对你来说是一个沉重的问题吗? –

+0

那么我没有使用Outlook,但雷鸟。在我将multipart从“related”改为“mixed”之前,它正在工作。 PLS。看到链接的stackoverflow问题。 – dbalakirev

+0

所以我得到这个使用这个整理: http://static.springsource.org/spring/docs/1.2.x/reference/mail.html 春天在后台做的伎俩。 但我不能关闭这个问题,因为我的代表不够高。 – dbalakirev

回答

2

相当方便的是从org.apache.commons.mail library使用ImageHtmlEmail。 (更新:仅包含在1.3的快照中)例如:

HtmlEmail email = new ImageHtmlEmail(); 
    email.setHostName("mail.myserver.com"); 
    email.addTo("[email protected]", "John Doe"); 
    email.setFrom("[email protected]", "Me"); 
    email.setSubject("Test email with inline image"); 

    // embed the image and get the content id 
    URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif"); 
    String cid = email.embed(url, "Apache logo"); 

    // set the html message 
    email.setHtmlMsg(htmlEmailTemplate, new File("").toURI().toURL(), false); 
相关问题