原创文章,转载请注明原文章地址,谢谢!
一、获取邮箱授权码
我以163邮箱为例,登录邮箱后,在“设置”里开启IMAP/SMTP服务,然后获取授权密码,切记一定把授权密码保存下来。(后面要用)
另外,记住邮箱的SMTP服务器: http://smtp.163.com
二、springboot接入邮件发送
1)application.yml配置
邮箱服务器的一些配置写在了属性文件中,为了安全最好配置在数据库中。
spring:
mail:
username: tgnhh9@163.com #用户名就是上面你的登录邮箱
password: 123456 #授权码,在服务选项中获取
host: smtp.163.com #163邮箱服务器
properties:
mail:
smtp:
ssl:
enable: true #开启安全连接
2)service
import com.qh.common.exception.BizException;
import com.qh.enroll.app.service.EmailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
/**
* @Date: 2019/4/12 10:22
* @Desc:
**/
@Service
@Transactional(rollbackFor = BizException.class)
public class EmailServiceImpl implements EmailService {
public final static Logger logger = LoggerFactory.getLogger(EmailServiceImpl.class);
@Value("${spring.mail.username}")
private String from;
@Autowired
private JavaMailSender javaMailSender;
@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);
try {
javaMailSender.send(message);
logger.info("简单邮件已经发送。");
} catch (Exception e) {
logger.error("发送简单邮件时发生异常!", e);
}
}
@Override
public void sendHtmlMail(String to, String subject, String content) {
MimeMessage message = javaMailSender.createMimeMessage();
try {
//true表示需要创建一个multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true,"utf-8");
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
javaMailSender.send(message);
logger.info("html邮件发送成功");
} catch (MessagingException e) {
logger.error("发送html邮件时发生异常!", e);
}
}
@Override
public void sendAttachmentsMail(String to, String subject, String content, String filePath) {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true,"utf-8");
helper.setSubject(subject);
helper.setText(content, true);
FileSystemResource file = new FileSystemResource(new File(filePath));
String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
helper.addAttachment(fileName, file);
//收件人
helper.setTo(to);
//发送人
helper.setFrom(from);
javaMailSender.send(mimeMessage);
} catch (MessagingException e) {
logger.error("发送附件的邮件时发生异常!", e);
}
}
@Async
@Override
public void sendAttachmentsMail(String to, String subject, String content, byte[] bytes) {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true,"utf-8");
helper.setSubject(subject);
helper.setText(content, true);
helper.addAttachment(subject, new ByteArrayResource(bytes));
//收件人
helper.setTo(to);
//抄送给自己
helper.setCc(from);
//发送人
helper.setFrom(from);
javaMailSender.send(mimeMessage);
} catch (MessagingException e) {
logger.error("发送附件的邮件时发生异常!", e);
}catch (Exception e) {
logger.error("发送附件的邮件时发生异常!", e);
}
}
@Override
public void sendInlineResourceMail(String to, String subject, String content, String rscPath, String rscId) {
MimeMessage message = javaMailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true,"utf-8");
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
FileSystemResource res = new FileSystemResource(new File(rscPath));
helper.addInline(rscId, res);
javaMailSender.send(message);
logger.info("嵌入静态资源的邮件已经发送。");
} catch (MessagingException e) {
logger.error("发送嵌入静态资源的邮件时发生异常!", e);
}
}
}
坑:1)当然邮件发送也有一些坑要踩,其中一个就是163服务器会禁止你频繁调用它的服务器,导致邮件发不出去
2)有条件的话,最好申请一个企业邮箱
3)实在还不行,就自己部署一套邮件服务器,这个后续有机会的话我会再写一篇,敬请期待。。。