【JavaMail】02 HTML邮件 & 附件邮件
创建HTML邮件:
【email.properties】创建邮件信息配置文件:
# 邮箱厂商信息 mail.smtp.host = smtp.qq.com mail.smtp.port = 587 mail.smtp.auth = true # 授权信息 mail.smtp.user = zeal4j mail.smtp.password = frcgbdjqjzptbegg # 发送者 & 接收者 mail.smtp.sender = zeal4j@qq.com mail.smtp.recipient = zeal4j@qq.com
测试方法:
@Test
public void sendHtmlEmail() throws Exception {
// 配置信息脱离硬编码,放在配置文件中
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("email.properties");
Properties properties = new Properties();
properties.load(inputStream);
// 得到邮件会话实例
Session emailSession = Session.getInstance(properties);
emailSession.setDebug(true);
Transport transport = emailSession.getTransport();
transport.connect(properties.getProperty("mail.smtp.host"), properties.getProperty("mail.smtp.user"), properties.getProperty("mail.smtp.password"));
// HTML邮件创建 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
MimeMessage mimeMessage = new MimeMessage(emailSession);
mimeMessage.setFrom(properties.getProperty("mail.smtp.sender"));
mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(properties.getProperty("mail.smtp.recipient")));
mimeMessage.setSubject("JavaMail邮件发送测试!!!");
mimeMessage.setSentDate(new Date());
// 创建多媒体容器对象
Multipart multipart = new MimeMultipart();
MimeBodyPart mimeBodyPart = new MimeBodyPart();
// HTML代码
final String htmlCode =
"<!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
"<head>\n" +
" <meta charset=\"UTF-8\">\n" +
" <title>Title</title>\n" +
"</head>\n" +
"<body>\n" +
" <h2>Hello Html Email</h2>\n" +
"\n" +
" <p>testing text in here 测试Html邮件</p>\n" +
"</body>\n" +
"</html>";
mimeBodyPart.setContent(htmlCode, "text/html;charset=utf-8");
multipart.addBodyPart(mimeBodyPart);
mimeMessage.setContent(multipart);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// 发送邮件
transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
// 资源释放
transport.close();
}

带附件的邮件:
@Test
public void sendIncludeAttachmentsEmail() throws Exception {
// 配置信息脱离硬编码,放在配置文件中
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("email.properties");
Properties properties = new Properties();
properties.load(inputStream);
// 得到邮件会话实例
Session emailSession = Session.getInstance(properties);
emailSession.setDebug(true);
Transport transport = emailSession.getTransport();
transport.connect(properties.getProperty("mail.smtp.host"), properties.getProperty("mail.smtp.user"), properties.getProperty("mail.smtp.password"));
MimeMessage mimeMessage = new MimeMessage(emailSession);
mimeMessage.setFrom(properties.getProperty("mail.smtp.sender"));
mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(properties.getProperty("mail.smtp.recipient")));
mimeMessage.setSubject("JavaMail带附件邮件发送测试!!!");
mimeMessage.setSentDate(new Date());
// 创建多媒体容器对象
Multipart multipart = new MimeMultipart();
MimeBodyPart mimeBodyPart = new MimeBodyPart();
// HTML代码,也可以不输入HTML,普通文本也支持
final String htmlCode =
"<!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
"<head>\n" +
" <meta charset=\"UTF-8\">\n" +
" <title>Title</title>\n" +
"</head>\n" +
"<body>\n" +
" <h2>Hello Html Email</h2>\n" +
"\n" +
" <p>附件测试</p>\n" +
"</body>\n" +
"</html>";
mimeBodyPart.setContent(htmlCode, "text/html;charset=utf-8");
multipart.addBodyPart(mimeBodyPart);
mimeMessage.setContent(multipart);
// 附件创建 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// 需要重新的一个BodyPart对象
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
// 文件源对象创建
FileDataSource fileDataSource = new FileDataSource("D:\\picture\\collection\\330624.jpg");
// 数据处理器
DataHandler dataHandler = new DataHandler(fileDataSource);
// 设置附件
attachmentBodyPart.setDataHandler(dataHandler);
// 设置附件名称
attachmentBodyPart.setFileName(dataHandler.getName());
// 装填附件Part
multipart.addBodyPart(attachmentBodyPart);
// 追加类型为混合的
// multipart.setSubType("mixed");
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// 发送邮件
transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
// 资源释放
transport.close();
}

虽然教程中有提到需要设置混合类型,但是编写发现,根本没有提供这样的API,就直接这样测试了,发现也没有问题

浙公网安备 33010602011771号