目录
项目结构:
1、创建springboot工程,引入依赖:
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.37</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
2、MailController
import com.alibaba.fastjson2.JSONObject;
import com.cl.entry.MailBody;
import com.cl.service.MailService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class MailController {
@Resource
private MailService mailService;
@PostMapping("/sendMail")
public JSONObject sendMail(@RequestBody MailBody mailBody) {
JSONObject res = new JSONObject();
boolean status = mailService.sendMail(mailBody);
res.put("code", status ? 200 : 500);
res.put("msg", status ? "发送成功" : "发送失败");
return res;
}
}
3、MailService
import com.cl.entry.MailBody;
import com.cl.utils.MailUtil;
import org.springframework.stereotype.Service;
@Service
public class MailService {
public boolean sendMail(MailBody mailBody) {
try {
MailUtil.sendEmil(mailBody);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
4、MailBody
import lombok.Data;
@Data
public class MailBody {
private String to;
private String title;
private String message;
}
5、MailUtil
username是自己的163邮箱,password是授权码,如何获取可以查看我的另一篇文章
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.Security;
import java.util.Date;
import java.util.Properties;
public class MailUtil {
/**
* s
* 使用加密的方式,利用465端口进行传输邮件,开启ssl
*/
public static void sendEmil(MailBody mailBody) throws Exception {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
//设置邮件会话参数
Properties props = new Properties();
//邮箱的发送服务器地址
props.setProperty("mail.smtp.host", "smtp.163.com");
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.smtp.socketFactory.fallback", "false");
//邮箱发送服务器端口,这里设置为465端口
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.auth", "true");
final String username = "jyfu@163.com";
final String password = "HFFQBWCTJRU";
//获取到邮箱会话,利用匿名内部类的方式,将发送者邮箱用户名和密码授权给jvm
Session session = Session.getDefaultInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
//通过会话,得到一个邮件,用于发送
Message msg = new MimeMessage(session);
//设置发件人
msg.setFrom(new InternetAddress(username));
//设置收件人,to为收件人,cc为抄送,bcc为密送
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mailBody.getTo(), false));
msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(mailBody.getTo(), false));
msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(mailBody.getTo(), false));
//设置邮件消息
msg.setSubject(mailBody.getTitle());
msg.setText(mailBody.getMessage());
//设置发送的日期
msg.setSentDate(new Date());
//调用Transport的send方法去发送邮件
Transport.send(msg);
System.out.println("发送了邮件 -- to:" + mailBody.getTo() + ", title: " + mailBody.getTitle() + ", message: " + mailBody.getMessage());
}
}
6、启动测试
项目源码:
email163.rar java springboot集成163邮箱发送https://download.csdn.net/download/weixin_47315082/88167365