场景
项目搭建专栏:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/column/info/35688
实现最简单的带标题以及文本内容的邮件发送。使用qq邮件服务器。
实现
项目中引入邮件依赖
<!-- 邮件依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
找到项目的全局配置文件application.properties
将username改为要发送邮件的账号,这里是qq邮箱账号
下面password设置为qq邮箱的授权码
#JavaMail邮件发送的配置
#指明邮件发送服务器 如果是163的则为smtp.163.com
spring.mail.host=smtp.qq.com
spring.mail.username=
#授权码qq邮箱需要设置并获取授权码 163则直接为邮箱密码
spring.mail.password=
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
qq邮箱设置授权码:
qq邮箱--设置--账户--POP3/SMTP服务
开启服务后就会获得qq的授权码
在项目下新建email包
在email包下新建配置类实现从全局配置文件中获取自定义属性
package com.example.demo.email;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class EmailConfig {
@Value("${spring.mail.username}")
private String emailFrom;
public String getEmailFrom() {
return emailFrom;
}
public void setEmailFrom(String emailFrom) {
this.emailFrom = emailFrom;
}
}
新建接口EmailService,有一个发送邮件的方法。
package com.example.demo.email;
import org.springframework.stereotype.Service;
@Service
public interface EmailService {
void sendSimpleMail(String senfTo,String title,String content);
}
新建接口实现类
package com.example.demo.email;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
@Service
public class EmailServiceImpl implements EmailService {
@Autowired
private EmailConfig emailConfig;
@Autowired
private JavaMailSender mailSender;
@Override
public void sendSimpleMail(String sendTo, String title, String content) {
//简单邮件的发送
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(emailConfig.getEmailFrom());
message.setTo(sendTo);
message.setSubject(title);
message.setText(content);
mailSender.send(message);
}
}
通过SimpleMailMessage 设置要发送邮件的账号以及标题和内容。
然后使用JavaMailSender 实现发送邮件。
新建controller实现测试邮件发送。
package com.example.demo.email;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class EmailController {
@Autowired
private EmailService emailService;
@RequestMapping("simpleEmail")
@ResponseBody
public String sendSimpleEmail() {
emailService.sendSimpleMail("****@qq.com", "测试", "简单邮件");
return "success";
}
}
sendSimpleMail方法第一个参数就是要发送的邮箱账号。
项目启动类下添加此包为可扫描。
@SpringBootApplication(scanBasePackages= {"com.example.demo.controller","com.example.demo.service","com.example.demo.interceptor","com.example.demo.handler","com.example.demo.job","com.example.demo.email"})
启动项目,访问
http://localhost:8080/simpleEmail
可以看到浏览器返回success,然后收到邮件。
效果
源码下载
https://download.csdn.net/download/badao_liumang_qizhi/11114781