发送邮件
作者:互联网
发送邮件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
文本文件
spring.mail.host=smtp.qq.com
spring.mail.port=587
spring.mail.username=1451053372@qq.com
spring.mail.password=ijknvfovjuohgdij
spring.mail.default-encoding=utf-8
spring.mail.properties.mail.smtp.auth=true
package com.yhd.kuangboot.service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
*邮件
**/
@Service
public class EmailService {
@Value("${spring.mail.username}")
private String form;
@Resource
private JavaMailSender javaMailSender;
//subject主题,content内容
public void sendSimpleMail(String to,String subject,String content){
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);
message.setSubject(subject);
message.setText(content);
message.setFrom(form);
javaMailSender.send(message);
}
}
@PostMapping("/sendEmail")
public String email(@RequestParam String to){
String subjext="您的验证码";
String context="这是您的验证码";
emailService.sendSimpleMail(to,subjext,context);
return "success";
}
html邮件
public void sendHtmlMail(String to,String subject,String content) throws MessagingException {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
mimeMessageHelper.setFrom(form);
mimeMessageHelper.setTo(to);
mimeMessageHelper.setSubject(subject);
mimeMessageHelper.setText(content,true);
javaMailSender.send(mimeMessage);
}
@PostMapping("/sendHtmlEmail")
public String htmlEmail(@RequestParam String to) throws MessagingException {
String subjext="您的验证码";
String context="<html>\n" +
"<body>\n"+
"<h3>hello,这是html邮件"+
"</h3>\n"+
"</body>\n"+
"</html>";
emailService.sendHtmlMail(to,subjext,context);
return "success";
}
附件邮件
public void sendAttachmentMail(String to,String subject,String content,String filePath) throws MessagingException {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
mimeMessageHelper.setFrom(form);
mimeMessageHelper.setTo(to);
mimeMessageHelper.setSubject(subject);
mimeMessageHelper.setText(content,true);
FileSystemResource file=new FileSystemResource(new File(filePath));
//获取文件名
String fileName=file.getFilename();
mimeMessageHelper.addAttachment(fileName,file);
javaMailSender.send(mimeMessage);
}
@PostMapping("/sendAttEmail")
public String attEmail(@RequestParam String to) throws MessagingException {
String subjext="您的验证码";
String context="<html>\n" +
"<body>\n"+
"<h3>hello,这是html邮件"+
"</h3>\n"+
"</body>\n"+
"</html>";
String filePath="C:\\Users\\岳\\Downloads\\Docker容器学习笔记全(狂神说Java).pdf";
emailService.sendAttachmentMail(to,subjext,context,filePath);
return "success";
}
图片邮件
public void sendImageMail(String to,String subject,String content,String rscPath,String rscId) throws MessagingException {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
mimeMessageHelper.setFrom(form);
mimeMessageHelper.setTo(to);
mimeMessageHelper.setSubject(subject);
mimeMessageHelper.setText(content,true);
FileSystemResource res=new FileSystemResource(new File(rscPath));
mimeMessageHelper.addInline(rscId,res);
javaMailSender.send(mimeMessage);
}
@PostMapping("/sendImageEmail")
public String imageEmail(@RequestParam String to) throws MessagingException {
String subjext="图片邮件";
String imgPath="C:\\Users\\岳\\Pictures\\Screenshots\\p1.png";
String rscId="neo001";
String context="<html><body>这是有图片的邮件:<img src=\'cid:"+rscId+"\'></img></body></html>";
String filePath="C:\\Users\\岳\\Downloads\\Docker容器学习笔记全(狂神说Java).pdf";
emailService.sendImageMail(to,subjext,context,imgPath,rscId);
return "success";
}
标签:mimeMessageHelper,content,String,mimeMessage,发送,mail,邮件,subject 来源: https://www.cnblogs.com/yhdxx/p/16579655.html