SpringBoot整合JavaMail通过阿里云企业邮箱发送邮件的实现
作者:发送邮件
这篇文章主要介绍了SpringBoot整合JavaMail通过阿里云企业邮箱发送邮件的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
JavaMail是Java开发中邮件处理的开源类库,支持常用协议如:SMTP、POP3、IMAP
一、SpringBoot整合
1、需要在pom文件中添加依赖spring-boot-starter-mail
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
构建项目,加载相关jar包
2、在application.yml中配置邮件发送的相关信息
spring: mail: host: smtp.mxhichina.com #阿里云发送服务器地址 port: 25 #端口号 username: XXX@XXXX.com #发送人地址 password: ENC(Grg2n2TYzgJv9zpwufsf37ndTe+M1cYk) #密码
3、编写邮件发送工具类MailUtil
MailUtil.java
/** * @author chenzan * @version V1.0 * @description 邮件发送 */ @Component public class MailUtil { @Autowired JavaMailSendermailSender; @Autowired MailPropertiesmailProperties; /** * 发送邮件测试方法 */ public void sendMail(MailBean mailBean) { SimpleMailMessage mimeMessage =new SimpleMailMessage(); mimeMessage.setFrom(mailProperties.getUsername()); mimeMessage.setTo(mailBean.getToAccount()); mimeMessage.setSubject(mailBean.getSubject()); mimeMessage.setText(mailBean.getContent()); mailSender.send(mimeMessage); } /** * 发送邮件-附件邮件 * @param boMailBean */ public boolean sendMailAttachment(MailBean mailBean) { try { MimeMessage mimeMessage =mailSender.createMimeMessage(); MimeMessageHelper helper =new MimeMessageHelper(mimeMessage, true); helper.setFrom(mailProperties.getUsername()); helper.setTo(boMailBean.getToAccount()); helper.setSubject(mailBean.getSubject()); helper.setText(mailBean.getContent(), true); // 增加附件名称和附件 helper.addAttachment(MimeUtility.encodeWord(boMailBean.getAttachmentFileName(), "utf-8", "B"), mailBean.getAttachmentFile()); mailSender.send(mimeMessage); return true; }catch (MessagingException e) { e.printStackTrace(); return false; } }
MailBean.java
/** * @author chenzan * @version V1.0 */ @Data public class MailBean { private Stringsubject; private String content; private String toAccount; private File attachmentFile; private String attachmentFileName; }
4.发送
String email = "XXX@email.com"; String content="测试内容"; String subject = "测试主题"; MailBean mailBean =new MailBean (); mailBean .setToAccount(email); boMailBean.setSubject(subject ); boMailBean.setContent(content); boolean resultSend =mailUtil.sendMailAttachment(boMailBean);
二、查看阿里云邮箱设置
支持SMTP/POP3/IMAP功能,轻松通过客户端软件(outlook、foxmail等)收发邮件。 详细设置 点此查看
三、SpringBoot中集成了jasypt在一定程度上保证密码的安全
1、添加依赖
<properties> <jasypt.version>1.9</jasypt.version> </properties> <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>${jasypt.version}</version> </dependency>
2、在application.yml中配置加密密钥
jasypt: encryptor: password: XXXXXXX
3、生成密码对应的密文
@RunWith(SpringRunner.class) @SpringBootTest public class EncryptTests { @Autowired StringEncryptor stringEncryptor; @Test public void encryptPwd() { String result = stringEncryptor.encrypt("XXXX"); System.out.println(result); } }
4、密文替换
在ENC()中添加对应密文
password: ENC(Grg2n2TYzgJv9zpwufsf37ndTe+M1cYk) #密码
到此这篇关于SpringBoot整合JavaMail通过阿里云企业邮箱发送邮件的实现的文章就介绍到这了,更多相关SpringBoot JavaMail发送邮件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!