Springboot 发送邮件

Spring Boot对Mail功能已经配置了相关的基本配置,我们只需要稍加修改即可使用

官方文档:https://docs.spring.io/spring/docs/5.1.2.RELEASE/spring-framework-reference/integration.html#mail


1、加入 pom 依赖

package ch.springboot.demo.email;import org.jasypt.encryption.StringEncryptor;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;@SpringBootTest
@RunWith(SpringRunner.class)
public class CreatePW {@Autowiredprivate StringEncryptor stringEncryptor;/*** 生成加密密码*/@Testpublic void testGeneratePassword() {// 你的邮箱密码String password = "abcdefg";// 加密后的密码(注意:配置上去的时候需要加 ENC(加密密码))String encryptPassword = stringEncryptor.encrypt(password);String decryptPassword = stringEncryptor.decrypt(encryptPassword);System.out.println("密码是 : " + password);System.out.println("加密后密码是 " + encryptPassword);System.out.println("解密后密码是 " + decryptPassword);}
}

console->


3、设置邮箱(以 163 为例)

  登录163邮箱后 点击设置,选择POP3

POP3/SMTP 取消打钩。然后再点打钩 弹出页面,点击确定

点击开启,需要手机发送短信验证 

发送完成后填写授权码,自定义,记住该授权码,需要在代码中配置

到此邮箱配置大功告成


4、application 配置

替换密码为加密后的 授权码,替换用户名为你的邮箱 

package ch.springboot.demo.email.service;import javax.mail.MessagingException;/*** mail 接口.** @author ch* @version 1.0.0* @since 1.0.0* 

* Created at 2019/11/26 10:08*/ public interface MailService {/*** 发送文本邮件** @param to 收件人地址* @param subject 邮件主题* @param test 邮件内容* @param cc 抄送地址*/void sendSimpleMail(String to, String subject, String test, String... cc)throws MessagingException;/*** 发送HTML邮件** @param to 收件人地址* @param subject 邮件主题* @param content 邮件内容* @param cc 抄送地址* @throws MessagingException 邮件发送异常*/void sendHtmlMail(String to, String subject, String content, String... cc) throws MessagingException;/*** 发送带附件的邮件** @param to 收件人地址* @param subject 邮件主题* @param content 邮件内容* @param filePath 附件地址* @param cc 抄送地址* @throws MessagingException 邮件发送异常*/void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc) throws MessagingException;}

package ch.springboot.demo.email.service.impl;import ch.springboot.demo.email.service.MailService;
import java.io.File;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.stereotype.Service;/*** mail 实现类.** @author ch* @version 1.0.0* @since 1.0.0* 

* Created at 2019/11/26 10:20 下午*/ @Service public class MailServiceImpl implements MailService {@Autowiredprivate JavaMailSender mailSender;@Value("${spring.mail.username}")private String from;@Overridepublic void sendSimpleMail(String to, String subject, String text, String... cc)throws MessagingException {SimpleMailMessage mailMessage = new SimpleMailMessage();// 邮箱用户名mailMessage.setFrom(from);// 邮件接收人mailMessage.setTo(to);// 设置邮件主题mailMessage.setSubject(subject);// 设置邮件内容mailMessage.setText(text);// 设置抄送人if (cc != null && cc.length != 0) {mailMessage.setCc(cc);}// 发送邮件mailSender.send(mailMessage);}/*** @param to 收件人地址* @param subject 邮件主题* @param content 邮件内容* @param cc 抄送地址* @throws MessagingException*/@Overridepublic void sendHtmlMail(String to, String subject, String content, String... cc)throws MessagingException {MimeMessage message = mailSender.createMimeMessage();MimeMessageHelper helper = new MimeMessageHelper(message, true);helper.setFrom(from);helper.setTo(to);helper.setSubject(subject);helper.setText(content, true);if (cc != null && cc.length != 0) {helper.setCc(cc);}mailSender.send(message);}/*** 发送带附件的邮件** @param to 收件人地址* @param subject 邮件主题* @param content 邮件内容* @param filePath 附件地址* @param cc 抄送地址* @throws MessagingException 邮件发送异常*/@Overridepublic void sendAttachmentsMail(String to, String subject, String content, String filePath,String... cc) throws MessagingException {MimeMessage message = mailSender.createMimeMessage();MimeMessageHelper helper = new MimeMessageHelper(message, true);helper.setFrom(from);helper.setTo(to);helper.setSubject(subject);helper.setText(content, true);if (cc != null && cc.length != 0) {helper.setCc(cc);}FileSystemResource file = new FileSystemResource(new File(filePath));String fileName = filePath.substring(filePath.lastIndexOf(File.separator));helper.addAttachment(fileName, file);mailSender.send(message);}}

参考文献:https://github.com/xkcoding/spring-boot-demo/tree/master/spring-boot-demo-email


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部