本篇內(nèi)容主要講解“springboot中JavaMailer使用ssl發(fā)送郵件”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“springboot中JavaMailer使用ssl發(fā)送郵件”吧!
創(chuàng)新互聯(lián)建站專注于企業(yè)網(wǎng)絡(luò)營(yíng)銷推廣、網(wǎng)站重做改版、鳳縣網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5技術(shù)、成都商城網(wǎng)站開發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為鳳縣等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
springboot使用發(fā)送郵件非常簡(jiǎn)單,都已經(jīng)封裝好了
首先就是引入依賴
org.springframework.boot spring-boot-starter-mail
然后配置發(fā)送郵箱的用戶名密碼等,這里以使用騰訊企業(yè)郵箱為例,因?yàn)樾枰渲昧藄sl,所以需要配置額外屬性properties,若不配置額外屬性使用默認(rèn)配置端口25也可以發(fā)送,但有些情況不行,比如阿里云的ecs內(nèi)就不允許訪問(wèn)外網(wǎng)的25端口,因?yàn)楸话⒗镌葡拗屏恕?/p>
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發(fā)送郵件
@ConditionalOnProperty(prefix = "spring.mail", name = "host") @Service public class CommonMailSender { @Value("${spring.mail.username}") private String from; @Autowired private JavaMailSender mailSender; /** * 簡(jiǎn)單文本郵件 * @param to 接收者郵件 * @param subject 郵件主題 * @param contnet 郵件內(nèi)容 */ @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內(nèi)容 * @throws MessagingException */ @Async public void sendHtmlMail(Listto, 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內(nèi)容 * @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內(nèi)容 * @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); } }
注意這里的發(fā)送郵件方法會(huì)阻塞進(jìn)程,所以使用了異步注解@Async
默認(rèn)異步方法每次調(diào)用都會(huì)啟用一個(gè)新線程執(zhí)行,非常耗資源,所以這里還需要配置一下異步的線程池
@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(); } }
到此,相信大家對(duì)“springboot中JavaMailer使用ssl發(fā)送郵件”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!