本篇內容主要講解“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發送郵件”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。