检查邮箱的SMTP/ POP3服务是否开通,如果没有开通,先开通
SMTP/ POP3服务
spring:
# 邮件配置
mail:
# smtp服务主机
host: smtp.163.com / smtp.qq.com
# 服务协议
protocol: smtp
# 编码集
default-encoding: UTF-8
# 允许测试连接
test-connection: true
# 发件人
username: 发送方邮箱
# 授权码
password: 第二步获取的授权码
package com.jojo.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
/**
* 邮件对象
*/
@Data
@ApiModel(value = "邮件内容")
public class MailVo implements Serializable {
@ApiModelProperty(value = "是否是html格式")
private boolean isHtml;
@ApiModelProperty(value = "接收者(可以多个)")
private String[] receivers;
@ApiModelProperty(value = "邮件主题")
private String title;
@ApiModelProperty(value = "邮件内容")
private String content;
}
@Value("${spring.mail.username}")
private String mailSender;
@Value("${spring.mail.password}")
private String mailPwd;
@Bean
public JavaMailSenderImpl JavaMailSender(){
JavaMailSenderImpl Sender = new JavaMailSenderImpl();
Sender.setHost("smtp.qq.com");
Sender.setUsername(mailSender);
Sender.setPassword(mailPwd);
return Sender;
}
package com.jojo.utils;
import com.jojo.vo.MailVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import javax.mail.internet.MimeMessage;
import java.util.Arrays;
/**
* 发送邮件
*/
@Component
@Slf4j
public class MailUtil {
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String sender;
/**
* 发送邮件
* @param mail 邮件内容
* @return
*/
public String sendMail(MailVo mail){
try{
if (mail.isHtml()){
MimeMessage mailMessage = mailSender.createMimeMessage();
// 可以发送文件
MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true);
messageHelper.setFrom(sender);
messageHelper.setTo(mail.getReceivers());
messageHelper.setSubject(mail.getTitle());
messageHelper.setText(mail.getContent(),true);
mailSender.send(mailMessage);
log.info("HTMl格式邮件发送成功!==> 收件人:{}", Arrays.asList(mail.getReceivers()));
} else {
// 创建邮件对象
SimpleMailMessage mailMessage = new SimpleMailMessage();
// 发件人
mailMessage.setFrom(sender);
// 收件人
mailMessage.setTo(mail.getReceivers());
// 邮件主题
mailMessage.setSubject(mail.getTitle());
// 邮件内容
mailMessage.setText(mail.getContent());
mailSender.send(mailMessage);
log.info("普通邮件发送成功!==> 收件人:{}", Arrays.asList(mail.getReceivers()));
}
return "邮件发送成功!";
} catch (Exception e){
log.error("邮件发送失败!==> {}",e.getMessage());
return "邮件发送失败!";
}
}
}
@Test
void TestMail(){
MailVo mailVo = new MailVo();
mailVo.setReceivers(new String[]{"[email protected]"});
mailVo.setTitle("邮箱测试");
mailVo.setContent("这是一条测试邮箱");
System.out.println(mailUtil.sendMail(mailVo));
}
标签:mail,spring,boot,private,mailMessage,import,邮件,String From: https://www.cnblogs.com/jingzhi-sksk/p/18248995