使用javaMail发送html邮件
一个博客怎么能少得了邮件功能,于是我想到在留言的同时还可以发送邮件通知,但只限于管理员可以发送邮件。只是普通的发送文本邮件还是比较简单的,但却没有自己博客的特点,于是我想到了使用javaMail发送html邮件
添加依赖
首先在pom.xml文件里添加JavaMail所需依赖
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
创建MailInfo实体类
这里我用了Lombok,没有使用的话需要添加set和get方法
@Data
@EqualsAndHashCode(callSuper = false)
public class MailInfo implements Serializable {
//邮箱服务器 如smtp.163.com
private String host ;
//用户邮箱 如**@163
private String formName ;
//用户授权码,可以在相关邮件服务器查看
private String formPassword ;
//消息回复邮箱
private String replayAddress ;
//发送地址
private String toAddress ;
//发送主题
private String subject ;
//发送内容
private String content ;
}
用户授权码可以在你的邮箱设置里开通并查看。
注意:授权码只显示一次,要记住了。
创建邮件发送工具类MailSendUtil
由于我只需要发送html文件,所以我只写了发送html的方法。
package com.xie.myblog.util;
import com.xie.myblog.vo.MailInfo;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Date;
import java.util.Properties;
/**
* @description: 邮件发送工具类
* @author: 先谢郭嘉
* @time: 2020/7/16 15:39
*/
public class MailSendUtil {
public static void sendHtmlMail(MailInfo info)throws Exception{
Message message = getMessage(info);
// MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
Multipart mainPart = new MimeMultipart();
// 创建一个包含HTML内容的MimeBodyPart
BodyPart html = new MimeBodyPart();
// 设置HTML内容
html.setContent(info.getContent(), "text/html; charset=utf-8");
mainPart.addBodyPart(html);
// 将MiniMultipart对象设置为邮件内容
message.setContent(mainPart);
Transport.send(message);
}
private static Message getMessage(MailInfo info) throws Exception {
final Properties p = System.getProperties();
p.setProperty("mail.smtp.host", info.getHost());
p.setProperty("mail.smtp.auth", "true");
p.setProperty("mail.smtp.user", info.getFormName());
p.setProperty("mail.smtp.pass", info.getFormPassword());
p.setProperty("mail.smtp.port", "465");
p.setProperty("mail.smtp.socketFactory.port", "465");
p.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
p.setProperty("mail.smtp.socketFactory.fallback", "false");
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session session = Session.getInstance(p, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(p.getProperty("mail.smtp.user"), p.getProperty("mail.smtp.pass"));
}
});
session.setDebug(true);
Message message = new MimeMessage(session);
//消息发送的主题
message.setSubject(info.getSubject());
//接受消息的人
message.setReplyTo(InternetAddress.parse(info.getReplayAddress()));
//消息的发送者
message.setFrom(new InternetAddress(p.getProperty("mail.smtp.user"), "先谢郭嘉の留言提醒"));
// 创建邮件的接收者地址,并设置到邮件消息中
message.setRecipient(Message.RecipientType.TO, new InternetAddress(info.getToAddress()));
// 消息发送的时间
message.setSentDate(new Date());
return message;
}
}
注意:邮件发送默认是25端口,但有些服务器禁用了25端口,所以我将邮件发送的端口改为了465端口,并加了以下代码:
p.setProperty("mail.smtp.port", "465");
p.setProperty("mail.smtp.socketFactory.port", "465");
p.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
p.setProperty("mail.smtp.socketFactory.fallback", "false");
业务处理(邮件发送)
MailInfo info = new MailInfo();
info.setHost("smtp.163.com");
info.setFormName("xie430423@163.com");//你的邮箱
info.setFormPassword("xxxxxxxx");//你的授权码
info.setReplayAddress("xie430423@163.com");//你的邮箱
info.setToAddress(user.getEmail());//发送给谁
info.setSubject("哇!!!先谢郭嘉 给您回复啦~~~");//邮件主题
info.setContent("xxxxxx");//html代码
try {
MailSendUtil.sendHtmlMail(info);
} catch (Exception e) {
System.out.print("邮件发送失败!");
e.printStackTrace();
}
原文链接(发布地址):https://xiejiabin.online/blog.html?blogId=202007010015