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

时间:2023-03-09 02:31:00
[Java] JavaMail 发送带图片的 html 格式的邮件

JavaMail 发送的邮件正文和附件是相互独立的,但是内置图片需要定位图片在正文中的位置,所以内置图片和邮件正文是互相依赖的。

发送带附件的邮件可参考JavaMail 发送 html 格式、带附件的邮件

发送纯文本的邮件可参照 JavaMail 简单案例

具体例子

EmailHelper, Email 的帮助类,向帮助类提供 SMTP 服务器域名、用户名、密码、发送人邮箱、收件人邮箱、邮件主题、html 格式的内容、图片的路径,便可发送一份内置图片的邮件。在创建 MimeMultipart 时, 需要传入参数 related,并在正文中声明图片的位置。

SendEmailDemo, 演示发送邮件。

EmailHelper.java

 package mail;

 import java.util.Properties;

 import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart; public class EmailHelper { private String host;
private String username;
private String password;
private String from; private String to;
private String subject;
private String htmlContent;
private String imagePath; public EmailHelper(String host, String username, String password, String from) throws AddressException, MessagingException{
this.host = host;
this.username = username;
this.password = password;
this.from = from;
} public void sendWithImage() throws Exception { Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", host); final String username1 = username;
final String password1 = password; Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username1, password1);
}
}); Message message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); message.setSubject(subject); Multipart multipart = new MimeMultipart("related"); System.out.println(" html ");
BodyPart htmlPart = new MimeBodyPart();
htmlContent = "<img src=\"cid:image\">" + htmlContent;
htmlPart.setContent(htmlContent, "text/html");
multipart.addBodyPart(htmlPart); System.out.println(" image ");
System.out.println("image path : " + imagePath);
BodyPart imgPart = new MimeBodyPart();
DataSource fds = new FileDataSource(this.imagePath); imgPart.setDataHandler(new DataHandler(fds));
imgPart.setHeader("Content-ID", "<image>"); multipart.addBodyPart(imgPart);
message.setContent(multipart);
Transport.send(message); System.out.println(" Sent -| ");
} public void setTo(String to) {
this.to = to;
} public void setSubject(String subject) {
this.subject = subject;
} public void setHtmlContent(String htmlContent) {
this.htmlContent = htmlContent;
} public String getImagePath() {
return imagePath;
} public void setImagePath(String imagePath) {
this.imagePath = imagePath;
}
}

SendEmailDemo.java

 public class SendEmailDemo {

     public static void main(){

         String host = "smtp.163.com";        // use your smtp server host

         final String username = "sender@163.com"; // use your username
final String password = "password";   // use your password String from = "sender@163.com";     // use your sender email address String to = "reciever@foxmail.com";  // use your reciever email address
try {
EmailHelper emailHelper = new EmailHelper(host, username, password, from);
emailHelper.setTo(to);
emailHelper.setSubject("subject ttt test");
emailHelper.setHtmlContent("<h1> This is html </h1>");
emailHelper.setImagePath("/Users/grs/Documents/Java/mavenEmail/test/src/main/resource/promises.png"); emailHelper.send(); } catch (Exception e) {
e.printStackTrace();
}
}
}

参考资料

JavaMail API - Sending Email With Inline Imagess