2011-05-01 67 views
5

我試圖從javamail發送電子郵件。我正在嵌入圖像並使用CID。但問題是我如何在一條消息中嵌入多個圖像。如果我嘗試添加標題..它只是採取最後設置的標題。我如何使用CID添加多個圖像和參考。如何在使用Java郵件發送HTML郵件時嵌入多個圖像

MimeMultipart multipart = new MimeMultipart("related"); 

    // first part (the html) 
    BodyPart messageBodyPart = new MimeBodyPart(); 
// BodyPart messageBodyPart = new MimeBodyPart(); 
    String htmlText = "<H1>Hello</H1><br/> <p align=center><img src=\"cid:senny\"> </p>"; 
    htmlText+="<p align=center><img src=\"cid:senny\"> </p>"; 
    htmlText+="<p align=center><img src=\"cid:image\"> </p>"; 
    messageBodyPart.setContent(htmlText, "text/html"); 

    // add it 
    multipart.addBodyPart(messageBodyPart); 

    // second part (the image) 
    messageBodyPart = new MimeBodyPart(); 
    DataSource fds = new FileDataSource 
     ("C:\\images\\cec_header_457.png"); 
    DataSource fds1 = new FileDataSource 
    ("C:\\images\\cec_header_420.png"); 
    messageBodyPart.setDataHandler(new DataHandler(fds)); 
    messageBodyPart.setDataHandler(new DataHandler(fds1)); 
    messageBodyPart.addHeader("Content-ID","<image>"); 
    messageBodyPart.addHeader("Content-ID","<senny>"); 
    // add it 
    multipart.addBodyPart(messageBodyPart); 

    // put everything together 
    message.setContent(multipart); 

回答

12

每個圖像需要將自己的MimeBodyPart的,打破這種代碼,

// second part (the image) 
messageBodyPart = new MimeBodyPart(); 
DataSource fds = new FileDataSource 
    ("C:\\images\\cec_header_457.png"); 
DataSource fds1 = new FileDataSource 
("C:\\images\\cec_header_420.png"); 
messageBodyPart.setDataHandler(new DataHandler(fds)); 
messageBodyPart.setDataHandler(new DataHandler(fds1)); 
messageBodyPart.addHeader("Content-ID","<image>"); 
messageBodyPart.addHeader("Content-ID","<senny>"); 
// add it 
multipart.addBodyPart(messageBodyPart); 

進入兩個多件,像

// second part (the image) 
messageBodyPart = new MimeBodyPart(); 
DataSource fds1 = new FileDataSource 
("C:\\images\\cec_header_420.png"); 
messageBodyPart.setDataHandler(new DataHandler(fds1)); 
messageBodyPart.addHeader("Content-ID","<senny>"); 
// add it 
multipart.addBodyPart(messageBodyPart); 

messageBodyPart = new MimeBodyPart(); 
DataSource fds = new FileDataSource 
    ("C:\\images\\cec_header_457.png"); 
messageBodyPart.setDataHandler(new DataHandler(fds)); 
messageBodyPart.addHeader("Content-ID","<image>"); 
// add it 
multipart.addBodyPart(messageBodyPart); 
+0

非常感謝。那是我正在做的錯誤。現在正在工作...... – user57421 2011-05-01 15:34:55

0

是每個圖像都需要自己MimeBodyPart的。以上解決方案僅適用於兩個圖像。萬一,如果有多個圖像,爲MimeBodyPart創建多個對象效率不高。

所以,讓我們去了功能,

public void imgUpload(Multipart multipart,String fileName) throws MessagingException 
{ 
    int no = rand.nextInt(); 
    String contentId = Integer.toString(no); 
    System.out.println(contentId); 

    BodyPart messageBodyPart = new MimeBodyPart(); 
    String htmlText = "<img align=\" center \" src=\"cid:"+contentId+"\"><br>"; 
    messageBodyPart.setContent(htmlText+"<br>", "text/html"); 

    // add it 
    multipart.addBodyPart(messageBodyPart); 
    System.out.println(contentId); 

    // second part (the image) 
    messageBodyPart = new MimeBodyPart(); 
    DataSource fds = new FileDataSource(fileName); 

    messageBodyPart.setDataHandler(new DataHandler(fds)); 
    messageBodyPart.setHeader("Content-ID", "<"+contentId+">"); 
    System.out.println(contentId); 

    // add image to the multipart 
    multipart.addBodyPart(messageBodyPart); 
} 

這裏要傳遞的參數是MimeBodyPart的的對象和圖像的路徑。 每個圖片需要自己的content-id,因此content-id是針對此場景隨機生成的。

全部代碼中嵌入多張圖片:

import java.util.*; 
import javax.mail.*; 
import javax.mail.internet.*; 
import javax.activation.*; 

public class SendImageEmail 
{ 
    public static void main(String [] args) 
    { 
     SendImageEmail sendEmail = new SendImageEmail(); 
     sendEmail.SendemailwithImage(); 
    } 
    public void imgUpload(Multipart multipart,String fileName) throws MessagingException 
    { 
    int no = rand.nextInt(); 
    String contentId = Integer.toString(no); 
    System.out.println(contentId); 

    BodyPart messageBodyPart = new MimeBodyPart(); 
    String htmlText = "<img align=\" center \" src=\"cid:"+contentId+"\"><br>"; 
    messageBodyPart.setContent(htmlText+"<br>", "text/html"); 

    // add it 
    multipart.addBodyPart(messageBodyPart); 
    System.out.println(contentId); 

    // second part (the image) 
    messageBodyPart = new MimeBodyPart(); 
    DataSource fds = new FileDataSource(fileName); 

    messageBodyPart.setDataHandler(new DataHandler(fds)); 
    messageBodyPart.setHeader("Content-ID", "<"+contentId+">"); 
    System.out.println(contentId); 

    // add image to the multipart 
    multipart.addBodyPart(messageBodyPart); 
    } 

    public void SendemailwithImage(){ 
     SendImageEmail imgEmail = new SendImageEmail(); 
     List<String> imgPath = new ArrayList<String>(); 
     imgPath.add("D:\\img1.png"); 
     imgPath.add("D:\\img2.png"); 
     imgPath.add("D:\\img3.png"); 

    // Recipient's email ID needs to be mentioned. 
    String to = "[email protected]"; 

    // Sender's email ID needs to be mentioned 
    String from = "[email protected]"; 

    // Assuming you are sending email from localhost 
    String host = "localhost"; 

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

    // Setup mail server 
    properties.setProperty("mail.smtp.host", host); 

    // Get the default Session object. 
    Session session = Session.getDefaultInstance(properties); 

    try{ 
    // Create a default MimeMessage object. 
    MimeMessage message = new MimeMessage(session); 

    // Set From: header field of the header. 
    message.setFrom(new InternetAddress(from)); 

    // Set To: header field of the header. 
    message.addRecipient(Message.RecipientType.TO, 
           new InternetAddress(to)); 

    // Set Subject: header field 
    message.setSubject("This is the Subject Line!"); 

    // Create the message part 
    BodyPart messageBodyPart = new MimeBodyPart(); 

    // Fill the message 
    messageBodyPart.setText("This is message body"); 

    // Create a multipar message 
    Multipart multipart = new MimeMultipart(); 

    // Set text message part 
    multipart.addBodyPart(messageBodyPart); 

    for(String fileName : imgPath) 
    { 
     imgEmail.upload(multipart,fileName); 
    }  
    // Send the complete message parts 
    message.setContent(multipart); 

    // Send message 
    Transport.send(message); 
    System.out.println("Sent message successfully...."); 
    }catch (MessagingException mex) { 
    mex.printStackTrace(); 
    }}} 
相關問題