SpringBoot+JMail

1.导入jar
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
2.SpringBoot配置
spring.mail.host=smtp.qq.com
spring.mail.username=发送邮箱
spring.mail.password=cutkvuqshjgobbcj
spring.mail.protocol=smtp
spring.mail.default-encoding=UTF-8

3.编码
public class MailUtil {
    @Autowired
JavaMailSenderImpl mailSender;

public void sendEmail(HashMap map) throws Exception{
//建立一个复杂的消息邮件
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setSubject((String) map.get("subject"));//邮件主题
helper.setText((String) map.get("text"),true);//使用html,邮件内容
helper.setTo((String) map.get("to"));//收件地址
helper.setFrom((String) map.get("from"));//发件人
String filePath = (String) map.get("file");//附件路径 if(!StringUtils.isEmpty(filePath)){ FileSystemResource fileSystemResource = new FileSystemResource(filePath); helper.addAttachment((String) map.get("newName"), fileSystemResource); } mailSender.send(mimeMessage); }}
相关文章
相关标签/搜索
本站公众号
   欢迎关注本站公众号,获取更多信息