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=http://www.likecs.com/\"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 }