SpringBoot整合JavaMail通过阿里云企业邮箱发送邮件的实现
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
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: #阿⾥云发送服务器地址
port: 25 #端⼝号
username: XXX@ #发送⼈地址
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@";
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发送邮件内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!。