溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

springboot中JavaMailer使用ssl發(fā)送郵件

發(fā)布時間:2021-06-28 17:50:14 來源:億速云 閱讀:735 作者:chen 欄目:編程語言

本篇內(nèi)容主要講解“springboot中JavaMailer使用ssl發(fā)送郵件”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“springboot中JavaMailer使用ssl發(fā)送郵件”吧!

springboot使用發(fā)送郵件非常簡單,都已經(jīng)封裝好了

首先就是引入依賴

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-mail</artifactId>
		</dependency>

然后配置發(fā)送郵箱的用戶名密碼等,這里以使用騰訊企業(yè)郵箱為例,因為需要配置了ssl,所以需要配置額外屬性properties,若不配置額外屬性使用默認配置端口25也可以發(fā)送,但有些情況不行,比如阿里云的ecs內(nèi)就不允許訪問外網(wǎng)的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發(fā)送郵件

@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 郵件內(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(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內(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ā)送郵件方法會阻塞進程,所以使用了異步注解@Async

默認異步方法每次調(diào)用都會啟用一個新線程執(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();
    }

}

到此,相信大家對“springboot中JavaMailer使用ssl發(fā)送郵件”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI