admin管理员组文章数量:1530035
注意,使用的是qq邮箱,qq邮箱-设置-账户里的协议要开启!
application.properties:
# 邮件 smtp 设置
spring.mail.username=你的邮箱
# 如果用 QQ 邮箱, 就用下面的 host
spring.mail.host=smtp.qq
spring.mail.password=你的密码
spring.mail.port=465
spring.mail.protocol=smtps
异步、同步实现
package kybmig.ssm.controller;
import kybmig.ssm.Utility;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
@Component
class AsyncMail {
private MailSender sender;
private MailProperties mailProperties;
public AsyncMail(MailSender sender, MailProperties mailProperties) {
this.sender = sender;
this.mailProperties = mailProperties;
}
@Async
public void sendMail(String address, String title, String content) {
Utility.log("异步发送邮件");
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setFrom(mailProperties.getUsername());;
mailMessage.setSubject(title);
mailMessage.setTo(address);
mailMessage.setText(content);
sender.send(mailMessage);
}
}
@Component
class SyncMail {
private MailSender sender;
private MailProperties mailProperties;
public SyncMail(MailSender sender, MailProperties mailProperties) {
this.sender = sender;
this.mailProperties = mailProperties;
}
public void sendMail(String address, String title, String content) {
Utility.log("同步发送邮件");
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setFrom(mailProperties.getUsername());
;
mailMessage.setSubject(title);
mailMessage.setTo(address);
mailMessage.setText(content);
sender.send(mailMessage);
}
}
@Controller
@RequestMapping("/mail")
public class MailController {
private SyncMail syncMail;
private AsyncMail asyncMail;
public MailController(SyncMail syncMail, AsyncMail asyncMail) {
this.syncMail = syncMail;
this.asyncMail = asyncMail;
}
@GetMapping("/index")
public ModelAndView index() {
return new ModelAndView("mail/index");
}
@GetMapping("/send")
@ResponseBody
public String send(String address, String title, String content) {
boolean valid = address !=null && title != null && content != null;
if (valid) {
syncMail.sendMail(address, title, content);
Utility.log("同步发送结束");
return "发送成功";
} else {
return "发送失败, 格式不对";
}
}
@GetMapping("/asySend")
@ResponseBody
public String asySend(String address, String title, String content) {
boolean valid = address !=null && title != null && content != null;
if (valid) {
asyncMail.sendMail(address, title, content);
Utility.log("异步发送结束");
return "发送成功";
} else {
return "发送失败, 格式不对";
}
}
}
加异步注解@EnableAsync
@SpringBootApplication
@EnableAsync
public class SsmApplication {
public static void main(String[] args) {
System.out.println(System.getProperty("file.encoding"));
System.out.println("本地 ide 运行入口或者命令行 java -jar 的入口,生产级 tomcat 不运行这个 main 函数");
SpringApplication.run(SsmApplication.class, args);
}
}
版权声明:本文标题:spring里发邮件到邮箱的实现 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1726722045a1081904.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论