本篇内容主要讲解“springboot中JavaMailer使用ssl发送邮件”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“springboot中JavaMailer使用ssl发送邮件”吧!
springboot使用发送邮件非常简单,都已经封装好了
首先就是引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
然后配置发送邮箱的用户名密码等,这里以使用腾讯企业邮箱为例,因为需要配置了ssl,所以需要配置额外属性properties,若不配置额外属性使用默认配置端口25也可以发送,但有些情况不行,比如阿里云的ecs内就不允许访问外网的25端口,因为被阿里云限制了。
spring:
mail:
host: smtp.exmail.qq.com
username: 10000@qq.com
password: 12345678
default-encoding: utf-8
properties:
'[mail.smtp.auth]': true
'[mail.smtp.timeout]': "5000"
'[mail.smtp.socketFactory.port]': "465"
'[mail.smtp.socketFactory.fallback]': false
'[mail.smtp.socketFactory.class]': "javax.net.ssl.SSLSocketFactory"
然后使用javamail发送邮件
@ConditionalOnProperty(prefix = "spring.mail", name = "host")
@Service
public class CommonMailSender {
@Value("${spring.mail.username}")
private String from;
@Autowired
private JavaMailSender mailSender;
/**
* 简单文本邮件
* @param to 接收者邮件
* @param subject 邮件主题
* @param contnet 邮件内容
*/
@Async
public void sendSimpleMail(String to, String subject, String contnet){
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);
message.setSubject(subject);
message.setText(contnet);
message.setFrom(from);
mailSender.send(message);
}
/**
* HTML 文本邮件
* @param to 接收者邮件
* @param subject 邮件主题
* @param contnet HTML内容
* @throws MessagingException
*/
@Async
public void sendHtmlMail(List<String> to, String subject, String contnet) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo(to.toArray(new String[] {}));
helper.setSubject(subject);
helper.setText(contnet, true);
helper.setFrom(from);
mailSender.send(message);
}
/**
* HTML 文本邮件
* @param to 接收者邮件
* @param subject 邮件主题
* @param contnet HTML内容
* @throws MessagingException
*/
@Async
public void sendHtmlMail(String to, String subject, String contnet) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(contnet, true);
helper.setFrom(from);
mailSender.send(message);
}
/**
* 附件邮件
* @param to 接收者邮件
* @param subject 邮件主题
* @param contnet HTML内容
* @param filePath 附件路径
* @throws MessagingException
*/
@Async
public void sendAttachmentsMail(String to, String subject, String contnet,
String filePath) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(contnet, true);
helper.setFrom(from);
FileSystemResource file = new FileSystemResource(new File(filePath));
String fileName = file.getFilename();
helper.addAttachment(fileName, file);
mailSender.send(message);
}
}
注意这里的发送邮件方法会阻塞进程,所以使用了异步注解@Async
默认异步方法每次调用都会启用一个新线程执行,非常耗资源,所以这里还需要配置一下异步的线程池
@Configuration
@EnableAsync
public class AsyncConfig extends AsyncConfigurerSupport {
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(7);
executor.setMaxPoolSize(42);
executor.setQueueCapacity(11);
executor.setThreadNamePrefix("AsyncExecutor-");
executor.initialize();
return executor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return new SimpleAsyncUncaughtExceptionHandler();
}
}
到此,相信大家对“springboot中JavaMailer使用ssl发送邮件”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
亿速云提供多种品牌、不同类型SSL证书签发服务,包含:域名型、企业型、企业型专业版、增强型以及增强型专业版,单域名SSL证书300元/年起。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/u/233090/blog/5041483