[Java] JavaMail 發送帶圖片的 html 格式的郵件


JavaMail 發送的郵件正文和附件是相互獨立的,但是內置圖片需要定位圖片在正文中的位置,所以內置圖片和郵件正文是互相依賴的。

發送帶附件的郵件可參考 JavaMail 發送 html 格式、帶附件的郵件

發送純文本的郵件可參照 JavaMail 簡單案例

具體例子

EmailHelper, Email 的幫助類,向幫助類提供 SMTP 服務器域名、用戶名、密碼、發送人郵箱、收件人郵箱、郵件主題、html 格式的內容、圖片的路徑,便可發送一份內置圖片的郵件。在創建 MimeMultipart 時, 需要傳入參數 related,並在正文中聲明圖片的位置。

SendEmailDemo, 演示發送郵件。

 

EmailHelper.java

  1 package mail;
  2 
  3 import java.util.Properties;
  4 
  5 import javax.activation.DataHandler;
  6 import javax.activation.DataSource;
  7 import javax.activation.FileDataSource;
  8 import javax.mail.BodyPart;
  9 import javax.mail.Message;
 10 import javax.mail.MessagingException;
 11 import javax.mail.Multipart;
 12 import javax.mail.PasswordAuthentication;
 13 import javax.mail.Session;
 14 import javax.mail.Transport;
 15 import javax.mail.internet.AddressException;
 16 import javax.mail.internet.InternetAddress;
 17 import javax.mail.internet.MimeBodyPart;
 18 import javax.mail.internet.MimeMessage;
 19 import javax.mail.internet.MimeMultipart;
 20 
 21 public class EmailHelper {
 22     
 23     private String host;
 24     private String username;
 25     private String password;
 26     private String from;
 27     
 28     private String to;
 29     private String subject;
 30     private String htmlContent;
 31     private String imagePath;
 32     
 33     public EmailHelper(String host, String username, String password, String from) throws AddressException, MessagingException{
 34         this.host = host;
 35         this.username = username;
 36         this.password = password;
 37         this.from = from;
 38     }
 39     
 40     public void sendWithImage() throws Exception {
 41 
 42         Properties props = new Properties();
 43         props.put("mail.smtp.auth", "true");
 44         props.put("mail.smtp.host", host);
 45 
 46         final String username1 = username;
 47         final String password1 = password;
 48 
 49         Session session = Session.getInstance(props, new javax.mail.Authenticator() {
 50             protected PasswordAuthentication getPasswordAuthentication() {
 51                 return new PasswordAuthentication(username1, password1);
 52             }
 53         });
 54 
 55         Message message = new MimeMessage(session);
 56 
 57         message.setFrom(new InternetAddress(from));
 58 
 59         message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
 60 
 61         message.setSubject(subject);
 62 
 63         Multipart multipart = new MimeMultipart("related");
 64 
 65         System.out.println(" html ");
 66         BodyPart htmlPart = new MimeBodyPart();
 67         htmlContent = "<img src=\"cid:image\">" + htmlContent;
 68         htmlPart.setContent(htmlContent, "text/html");
 69         multipart.addBodyPart(htmlPart);
 70         
 71         System.out.println(" image ");
 72         System.out.println("image path : " + imagePath);
 73         BodyPart imgPart = new MimeBodyPart();
 74         DataSource fds = new FileDataSource(this.imagePath);
 75 
 76         imgPart.setDataHandler(new DataHandler(fds));
 77         imgPart.setHeader("Content-ID", "<image>");
 78 
 79         multipart.addBodyPart(imgPart);
 80         message.setContent(multipart);
 81         Transport.send(message);
 82 
 83         System.out.println(" Sent -| ");
 84     }
 85 
 86     public void setTo(String to) {
 87         this.to = to;
 88     }
 89 
 90     public void setSubject(String subject) {
 91         this.subject = subject;
 92     }
 93 
 94     public void setHtmlContent(String htmlContent) {
 95         this.htmlContent = htmlContent;
 96     }
 97     
 98     public String getImagePath() {
 99         return imagePath;
100     }
101 
102     public void setImagePath(String imagePath) {
103         this.imagePath = imagePath;
104     }
105 }

SendEmailDemo.java

 1 public class SendEmailDemo {
 2     
 3     public static void main(){
 4         
 5         String host = "smtp.163.com";        // use your smtp server host
 6 
 7         final String username = "sender@163.com"; // use your username
 8         final String password = "password";   // use your password
 9 
10         String from = "sender@163.com";     // use your sender email address
11 
12         String to = "reciever@foxmail.com";  // use your reciever email address
13         try {
14             EmailHelper emailHelper = new EmailHelper(host, username, password, from);
15             emailHelper.setTo(to);
16             emailHelper.setSubject("subject ttt test");
17             emailHelper.setHtmlContent("<h1> This is html </h1>");
18             emailHelper.setImagePath("/Users/grs/Documents/Java/mavenEmail/test/src/main/resource/promises.png");
19             
20             emailHelper.send();
21             
22         } catch (Exception e) {
23             e.printStackTrace();
24         }
25     }
26 }

 

參考資料

JavaMail API - Sending Email With Inline Imagess

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM