1. 程式人生 > >springBoot(18):多賬號輪詢發送郵件

springBoot(18):多賬號輪詢發送郵件

spring boot 多賬號

一、添加依賴

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

二、配置application.properties文件

#############################mail配置#############################
spring.mail.host: smtp.163.com,smtp.mxhichina.com
spring.mail.username:[email protected]
/* */,[email protected] spring.mail.password:123456geren,ASDasd123 spring.mail.properties.mail.smtp.auth: true

三、實現發送功能

MailConfiguration.java
package com.example.demo.config;

import java.util.ArrayList;
import java.util.Map;
import java.util.Properties;

import javax.mail.internet.MimeMessage;

import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

/**
 * 實現多賬號,輪詢發送
 *
 * @Author: 我愛大金子
 * @Description: 實現多賬號,輪詢發送
 * @Date: Create in 16:07 2017/7/4
 */
@Configuration
@EnableConfigurationProperties(MailProperties.class)
public class MailConfiguration extends JavaMailSenderImpl implements JavaMailSender {

   private ArrayList<String> usernameList;
   private ArrayList<String> passwordList;
   private ArrayList<String> hostList;
   private int currentMailId = 0;

   private final MailProperties properties;

   public MailConfiguration(MailProperties properties) {
      this.properties = properties;
      // 初始化郵件服務器
      if (hostList == null)
         hostList = new ArrayList<String>();
      String[] hosts = this.properties.getHost().split(",");
      if (hosts != null) {
         for (String host : hosts) {
            hostList.add(host);
         }
      }

      // 初始化賬號
      if (usernameList == null)
         usernameList = new ArrayList<String>();
      String[] userNames = this.properties.getUsername().split(",");
      if (userNames != null) {
         for (String user : userNames) {
            usernameList.add(user);
         }
      }

      // 初始化密碼
      if (passwordList == null)
         passwordList = new ArrayList<String>();
      String[] passwords = this.properties.getPassword().split(",");
      if (passwords != null) {
         for (String pw : passwords) {
            passwordList.add(pw);
         }
      }
   }

   @Override
   protected void doSend(MimeMessage[] mimeMessage, Object[] object) throws MailException {
      super.setUsername(usernameList.get(currentMailId));
      super.setPassword(passwordList.get(currentMailId));
      super.setHost(hostList.get(currentMailId));
      // 設置編碼和各種參數
      super.setDefaultEncoding(this.properties.getDefaultEncoding().name());
      super.setJavaMailProperties(asProperties(this.properties.getProperties()));
      super.doSend(mimeMessage, object);

      // 輪詢
      currentMailId = (currentMailId + 1) % usernameList.size();
   }

   private Properties asProperties(Map<String, String> source) {
      Properties properties = new Properties();
      properties.putAll(source);
      return properties;
   }

   @Override
   public String getUsername() {
      return usernameList.get(currentMailId);
   }

}
MailComponent.java
package com.example.demo.utils.component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.example.demo.config.MailConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

/**
 * 郵件發送
 *
 * @Author: 我愛大金子
 * @Description: 實郵件發送
 * @Date: Create in 16:07 2017/7/4
 */
@Component
public class MailComponent {
   private static final String template = "mail/roncoo.ftl";
   @Autowired
   private ThymeleafAutoConfiguration thymeleafAutoConfiguration;
   @Autowired
   private MailConfiguration mailConfiguration;

   /**
    * 發送郵件
    * @Author: 我愛大金子
    * @Description: 發送郵件
    * @Date: 16:46 2017/7/4
    * @param to 接收人
    * @param text 發送內容
    */
   public void sendMail(String to, String text) {
      Map<String, Object> map = new HashMap<String, Object>();
      map.put("email", to);
      try {
         send(to, text);
      } catch (IOException e) {
         e.printStackTrace();
      } catch (MessagingException e ) {
         e.printStackTrace();
      }
   }

   private String send(String email, String text) throws MessagingException, UnsupportedEncodingException {
      MimeMessage message = mailConfiguration.createMimeMessage();
      MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
      InternetAddress from = new InternetAddress();
      from.setAddress(mailConfiguration.getUsername());
      from.setPersonal("我愛大金子", "UTF-8");
      helper.setFrom(from);
      helper.setTo(email);
      helper.setSubject("測試郵件");
      helper.setText(text, true);
      mailConfiguration.send(message);
      return text;
   }

}

四、測試

package com.example.demo;

import com.example.demo.utils.component.MailComponent;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootEmailsApplicationTests {
   @Autowired
   private MailComponent mailComponent;
   @Test
   public void contextLoads() {
      mailComponent.sendMail("[email protected]", "測試第1次");
   }

}

效果:

技術分享


本文出自 “我愛大金子” 博客,請務必保留此出處http://1754966750.blog.51cto.com/7455444/1944506

springBoot(18):多賬號輪詢發送郵件