@File
2019-09-27T01:51:25.000000Z
字数 1316
阅读 191
java
POP3/IMAP/SMTP 协议
spring-boot发邮件
授权码:作为配置时必须的密码
收发流程:


<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency>
spring:# 配置发送方信息mail:# 邮箱类型(当前为qq邮箱)# qq企业邮箱:smtp.exmail.qq.com# 阿里云个人邮箱:smtp.aliyun.com# 163个人邮箱:smtp.163.com# 163邮箱企业:smtp.qiye.163.comhost: smtp.qq.com# 邮箱账号(重点修改)username: 88888888@qq.com# 授权码,非登录密码(重点修改)password: z5qc4sn1kyd7jva2u5bb4b6a0# 常用配置properties:mail:smtp:auth: truetimeout: 25000ssl:enable: true
@Componentpublic class MailUtil {/*** 发邮件类*/@Resourceprivate JavaMailSenderImpl mailSender;/*** 读取配置中的发送方邮箱*/@Value("${spring.mail.username}")private String from;/*** 发送邮件* @param toMail 接收方email* @param subject 邮件标题* @param content 邮件内容* @return 是否成功发送*/public boolean send(String toMail, String subject, String content) {// 邮件配置类SimpleMailMessage message = new SimpleMailMessage();// 发件方邮箱message.setFrom(from);// 收件方邮箱message.setTo(toMail);// 邮件标题message.setSubject(subject);// 邮件内容message.setText(content);try {mailSender.send(message);// 发送成功return true;}catch (Exception e) {// 发送失败// e.printStackTrace();return false;}}}
@Servicepublic class MailServiceImpl implements MailService {@Resourceprivate MailUtil mailUtil;@Overridepublic boolean sendMail() {return mailUtil.send("邮箱地址","标题","正文");}}