码迷,mamicode.com
首页 > 编程语言 > 详细

[Java] JavaMail 发送带图片的 html 格式的邮件

时间:2016-11-10 02:58:43      阅读:269      评论:0      收藏:0      [点我收藏+]

标签: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 = "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

 

[Java] JavaMail 发送带图片的 html 格式的邮件

标签:int   位置   instance   href   pat   htm   add   参数   服务器   

原文地址:http://www.cnblogs.com/TonyYPZhang/p/6049412.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!