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

來源:互聯網
上載者:User

標籤:int   位置   instance   href   pat   htm   add   參數   伺服器   

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 = "[email protected]"; // use your username 8         final String password = "password";   // use your password 9 10         String from = "[email protected]";     // use your sender email address11 12         String to = "[email protected]";  // use your reciever email address13         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

 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.