Java开发Web应用时越来越多场景提出需要发送邮件功能,现在springboot提供了简单易用的封装。发送邮件,要有发送方和接收方,我们需要在系统中配置发送方的信息,而接受方的信息我们需要根据从客户端发来的请求决定。作为发送方的我们,需要被授权,被谁授权?
假如使用QQ邮箱发送邮件,那么我就需要开启QQ邮箱授权码。
假如使用网易邮箱发送邮件,那我就需要开启网易邮箱授权码。
如
发送邮件有简单邮件、HTML格式邮件、带附件邮件等情况,实例如下
pom文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
核心代码
@RestController
@RequestMapping("/email")
public class EmailController {
@Autowired
private EmailService emailService;
@Value("${spring.mail.username}")
private String from;
// 此应为请求参数
private String to = "***@qq.com";
// 简单邮件
@RequestMapping("sendSimpleEmail")
public String sendSimpleEmail() {
String content = "端午安康!";
emailService.sendSimpleMail("***@qq.com","简单邮件",content);
return "ok";
}
// 发送Html格式邮件
@RequestMapping("sendHtmlEmail")
public String sendHtmlEmail() {
String content = "<a href='https://www.baidu.com/'>你好,欢迎注册网站,请点击链接激活</a>";
emailService.sendHtmlMail("***@qq.com","Html格式邮件",content);
return "ok";
}
// 发送邮件携带附件
@RequestMapping("sendAttachmentsMail")
public String sendAttachmentsMail() {
String content = "这是dog";
emailService.sendAttachmentsMail("***@qq.com","带附件邮件",content,"***\\src\\main\\resources\\static\\11.docx");
return "ok";
}
// 发送附带静态资源的邮件
@RequestMapping("sendInlineMail")
public String sendInlineMail() {
String content = "\"<html><body>生日快乐:<img src='cid:img'/></body></html>\"";
emailService.sendInlineMail("***@qq.com","带静态资源邮件",content,"***\\src\\main\\resources\\static\\11.jpg");
return "ok";
}
}
public interface EmailService {
/**
* 发送文本邮件
* @param to 收件人
* @param subject 主题
* @param content 内容
*/
void sendSimpleMail(String to, String subject, String content);
/**
* 发送HTML邮件
* @param to 收件人
* @param subject 主题
* @param content 内容
*/
void sendHtmlMail(String to, String subject, String content);
/**
* 发送带附件的邮件
* @param to 收件人
* @param subject 主题
* @param content 内容
* @param filePath 附件
*/
void sendAttachmentsMail(String to, String subject, String content, String filePath);
/**
* 发送附带静态资源的邮件
* @param to 收件人
* @param subject 主题
* @param content 内容
* @param filePath 附件
*/
void sendInlineMail(String to, String subject, String content, String filePath);
}
@Service
public class EmailServiceImpl implements EmailService {
private static final Logger logger = LoggerFactory.getLogger(EmailServiceImpl.class);
@Autowired
private JavaMailSender javaMailSender;
@Value("${spring.mail.from}")
private String from;
@Override
public void sendSimpleMail(String to, String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();
//发件人
message.setFrom(from);
//收件人
message.setTo(to);
//邮件主题
message.setSubject(subject);
//邮件内容
message.setText(content);
//发送邮件
javaMailSender.send(message);
logger.info("邮件已经发送!");
}
@Override
public void sendHtmlMail(String to, String subject, String content) {
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper messageHelper;
try {
messageHelper = new MimeMessageHelper(message,true);
messageHelper.setFrom(from);
messageHelper.setTo(to);
message.setSubject(subject);
messageHelper.setText(content,true);
javaMailSender.send(message);
logger.info("邮件已经发送!");
} catch (MessagingException e) {
logger.error("发送邮件时发生异常:"+e);
}
}
@Override
public void sendAttachmentsMail(String to, String subject, String content, String filePath) {
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper messageHelper;
try {
messageHelper = new MimeMessageHelper(message,true);
messageHelper.setFrom(from);
messageHelper.setTo(to);
messageHelper.setSubject(subject);
messageHelper.setText(content,true);
//携带附件
FileSystemResource file = new FileSystemResource(filePath);
String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
messageHelper.addAttachment(fileName,file);
javaMailSender.send(message);
logger.info("邮件已经发送!");
} catch (MessagingException e) {
logger.error("发送失败:"+e);
}
}
@Override
public void sendInlineMail(String to, String subject, String content, String filePath) {
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper messageHelper;
try {
messageHelper = new MimeMessageHelper(message, true);
messageHelper.setFrom(from);
messageHelper.setTo(to);
messageHelper.setSubject(subject);
messageHelper.setText(content, true);
// 传入附件
FileSystemResource file = new FileSystemResource(new File(filePath));
messageHelper.addInline("img", file);
javaMailSender.send(message);
logger.info("邮件已经发送!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
application.properties
spring.mail.host=smtp.163.com
spring.mail.username=
spring.mail.password=
spring.mail.from=
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.default-encoding=utf-8
测试结果