写一篇 发送邮件的代码,自己看
先上一下配置 :
#邮件发送配置
spring.mail.default-encoding=UTF-8
spring.mail.host=smtp.sina.cn
spring.mail.username=hahahahahha@sina.com
spring.mail.password=eeeeeee
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
上一下pom文件
<!--邮件发送核心包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
上代码:
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
@Value("${spring.mail.username}")
private String from;
@Autowired
private JavaMailSender mailSender;
public int sendSimpleMail(String to, String title, String content)throws Exception {
MimeMessage message = mailSender.createMimeMessage();
message.addHeader("X-Mailer","Microsoft Outlook Express 6.00.2900.2869");
//true 表⽰示需要创建⼀一个 multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(title);
helper.setText(content, true);
mailSender.send(message);
return 1;
}
然后就是在代码中调用发送的方法:
这个地方一定要注意点
String a = httpUrl+"/gwty/html/verification.html?"+verifyToken+"&"+type;
String concat = "<html><head></head><body><h3>注册验证: 点击链接跳转</h3>"+a+"</body></html>";
sendEmilUtil.sendSimpleMail(email, "用户验证通知", concat);
councat 这要用html格式的 不然的话会被大部分的邮箱 当作垃圾邮件
时间长了发送账号会被拉入黑名单,导致接收不到邮件(这个坑我已经进去过了,警钟长鸣)