Springboot实现发邮件功能简单方便,得空就详细记录。。。。。简单直接上硬菜
1.基础依赖
<!-- 发邮件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- 整合Thymeleaf ,方便实现发送带有模板的邮件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2.获取qq邮箱/163邮箱的授权码
QQ邮箱->设置->账户->POP3/SMTP服务:开启服务后会获得QQ的授权码.
登录163邮箱->设置->POP3/SMTP/IMAP设置->开启协议,正确设置授权码
3.编写配置文件
qq邮箱的配置
# JavaMailSender 邮件发送的配置
spring.mail.host=smtp.qq.com
spring.mail.username=qq邮箱号
#QQ邮箱的授权码
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
spring.mail.default-encoding=UTF-8
163邮箱的配置
spring.mail.host=smtp.163.com
spring.mail.username=用户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
spring.mail.default-encoding=UTF-8
实现过程(仓促间把实现代码写在了一个类中,需要用到哪个邮箱复制哪个邮箱的代码即可。。。。。此处省略service层)
package com.dsx.service.serviceImp;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import com.dsx.config.EmailConfig;
import com.dsx.service.EmailService;
import freemarker.template.Template;
@Service
public class EmailServiceImp implements EmailService {
@Autowired
private EmailConfig emailConfig;//发件人账号
@Autowired
private JavaMailSender sender;
@Override
public void sendSimpleEmail(String emailTo, String msg,String subject) {
try {
SimpleMailMessage smm = new SimpleMailMessage();
smm.setFrom(emailConfig.getEmailFrom());
smm.setTo(emailTo);
smm.setSubject(subject);//主题
smm.setText(msg);//内容
sender.send(smm);
} catch (MailException e) {
System.out.println("邮件发送失败。。。"+e.getMessage());
}
}
@Override
public void sendAttachmentEmail(String emailTo, String msg, String subject, File file) {
MimeMessage m = sender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(m,true);
helper.setFrom(emailConfig.getEmailFrom());
helper.setTo(emailTo);
helper.setSubject(subject);
helper.setText(msg);
FileSystemResource r = new FileSystemResource(file);
helper.addAttachment("附件", r);
} catch (MessagingException e) {
// TODO Auto-generated catch block
System.out.println("邮件发送失败。。。"+e.getMessage());
}
sender.send(m);
}
@Autowired
private FreeMarkerConfigurer fmc;
//163邮箱
@Override
//emailTo收件人
//subject 主题
//file 模板的名称 此处使用的是Thymeleaf模板,默认找src/main/resources/templates下的.html文件。此处我便是在该文件夹下创建了a.html模板。
public void send163(String emailTo, String subject, String file){
MimeMessage m = sender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(m,true);
helper.setFrom(emailConfig.getEmailFrom());
helper.setTo(emailTo);
helper.setSubject(subject);
//封装模板使用的数据
Map<String, Object> model = new HashMap<String, Object>();
model.put("content", "练习");
model.put("title", "标题Mail中使用了FreeMarker");
//得到模板
Template template = fmc.getConfiguration().getTemplate(file);
String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
helper.setText(html, true);
} catch (Exception e) {
System.out.println("邮件发送失败。。。"+e.getMessage());
}
sender.send(m);
}
}
为了解决硬编码问题,特编写了EmailConfig类(学Java的朋友都明白。。。。。)
package com.dsx.config;
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;
}
}
调用方法即可完成发邮件功能,本人亲测有效。在此特附上一些使用163邮箱发邮件遇到的问题及解决
1.连接smtp.163.com异常,报错:java.net.UnknownHostException: smtp.163.com
解决思路:
1.进行网络问题的检查
2.查看配置是否正常,application.properties文件中配置后面不能有空格呦
2.用户权限不足,报错:javax.mail.AuthenticationFailedException: 550 User has no permission
解决思路:
1.没有开启163邮箱的pop3/smtp协议(获取授权码即可)
2.登录163邮箱->设置->POP3/SMTP/IMAP设置->开启协议,正确设置授权码
3.认证错误,报错:javax.mail.AuthenticationFailedException: 535 Error: authentication failed
解决思路:
1.163邮箱开通smtp服务后,javaMail发送邮箱要使用授权码作为登录密码
2.设置spring.mail.password=授权码
至此springboot整合Mail完成,欢迎指正留言。。。。。