前言:我以前用Log4j發郵件,發現特別影響性能,若是頻繁遇到異常觸發郵件就會影響其餘請求訪問,後來換成 new thread 來單獨 發送 javaXMail 。html
今天發現 spring-boot-starter-mail,雖然也是依賴javaxMail 實現的,可是強迫症犯了 ,認爲Spring大法是最好的。java
1.maven依賴spring
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
2.配置相關配置服務器
#mail spring.mail.host=smtp.exmail.qq.com spring.mail.username= spring.mail.password= spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true spring.mail.sendTo=
3.使用方法異步
@Autowired private JavaMailSender mailSender; SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(env.getProperty("spring.mail.username")); message.setTo(env.getProperty("spring.mail.sendTo")); message.setSubject(title); message.setText(text); mailSender.send(message);
測試發現用JavaMailSender 發送也是同步,影響性能maven
4.使用Spring的TaskExecutor實現異步發送。spring-boot
@Bean public ThreadPoolTaskExecutor createThreadPoolTaskExecutor() { ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor(); threadPoolTaskExecutor.setCorePoolSize(10); threadPoolTaskExecutor.setMaxPoolSize(20); return threadPoolTaskExecutor; }
taskExecutor.execute(() -> { try { SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(env.getProperty("spring.mail.username")); message.setTo(env.getProperty("spring.mail.sendTo")); message.setSubject(title); message.setText(text); mailSender.send(message); } catch (Exception e) { logger.warn("發送郵件消息失敗=exception:{}", e); } });
上面的()->的寫法是 java8的Lambda寫法, new Runnable()
5.使用Spring的@Async實現異步性能
@Async public static void AsyncMethod(){ //執行發郵件操做 }
須要啓用Async , 配置一下@EnableAsync測試
經過@Async註解代表該方法是異步方法,若是註解在類級別上則代表該類全部的方法都是異步方法。ui
java原生也有線程池,有興趣的同窗能夠去看看ThreadPoolExecutor類的說明
6.Main方法運行
// 建立Properties 類用於記錄郵箱的一些屬性 final Properties props = new Properties(); // 表示SMTP發送郵件,必須進行身份驗證 props.put("mail.smtp.auth", "true"); //此處填寫SMTP服務器 props.put("mail.smtp.host", "smtp.qq.com"); //端口號,QQ郵箱給出了兩個端口,可是另外一個我一直使用不了,因此就給出這一個587 props.put("mail.smtp.port", "587"); // 此處填寫你的帳號 props.put("mail.user", "2251181679@qq.com"); // 此處的密碼就是前面說的16位STMP口令 props.put("mail.password", ""); // 構建受權信息,用於進行SMTP進行身份驗證 Authenticator authenticator = new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { // 用戶名、密碼 String userName = props.getProperty("mail.user"); String password = props.getProperty("mail.password"); return new PasswordAuthentication(userName, password); } }; // 使用環境屬性和受權信息,建立郵件會話 Session mailSession = Session.getInstance(props, authenticator); // 建立郵件消息 MimeMessage message = new MimeMessage(mailSession); // 設置發件人 InternetAddress form = new InternetAddress(props.getProperty("mail.user")); message.setFrom(form); // 設置收件人的郵箱 InternetAddress[] sendTo = new InternetAddress[2]; sendTo[0] = new InternetAddress("22511181167911@qq.com"); sendTo[1] = new InternetAddress("1431081761916@qq.com"); message.setRecipients(Message.RecipientType.TO, sendTo); // 設置郵件標題 message.setSubject("測試郵件"); // 設置郵件的內容體 message.setContent("這是一封測試郵件", "text/html;charset=UTF-8"); // 最後固然就是發送郵件啦 Transport.send(message);
博客地址:http://my.oschina.net/wangnian