如何使用Java邮件发送html电子邮件时嵌入多个图像

时间:2011-05-01 13:29:00

标签: java image embed mime javamail

我正在尝试从javamail发送电子邮件。我正在嵌入图像并使用CID。但问题是我如何在一条消息中嵌入多个图像。如果我尝试添加标题..它只是采取最后设置标题。如何使用CID添加多个图像和参考。

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);

2 个答案:

答案 0 :(得分:13)

每张图片都需要是自己的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);

答案 1 :(得分: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 = "abcd@gmail.com";

  // Sender's email ID needs to be mentioned
  String from = "web@gmail.com";

  // 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();
  }}}