JAVA使用多个邮箱账号发送邮箱

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

 

application.yml配置文件(邮箱):

spring:
  mail:
    username: 邮箱账号@qq.com,邮箱账号@sina.com//多个邮箱以逗号隔开
    password: 123123,hfnjpxafjeigbhde//确保邮箱已经开启SMTP服务,有授权码则使用授权码,没有则使用登陆密码
    host: smtp.qq.com,smtp.afwaf.com//邮箱对应host地址
    properties:
      mail.smtp.auth: true
      mail.smtp.starttls: true

MyJavaMailSenderImpl

@Configuration
@EnableConfigurationProperties(MailProperties.class)
@Slf4j
public class MyJavaMailSenderImpl extends JavaMailSenderImpl implements JavaMailSender {

    private ArrayList<String> accountList = null;
    private ArrayList<String> passwordList = null;
    private ArrayList<String> hostList = null;

    private int currentMailId = 0;

    public final MailProperties properties;

    public MyJavaMailSenderImpl(MailProperties properties) {
        this.properties = properties;
        if (hostList == null) {
            hostList = new ArrayList<>();
            String[] hosts = this.properties.getHost().split(",");//获取配置文件host地址,并以,切割.添加到ArrayList中
            if (hosts != null) {
                for (String ht : hosts) {
                    hostList.add(ht);
                }
            }
        }

        if (accountList == null) {
            accountList = new ArrayList<>();
        }
        String[] username = this.properties.getUsername().split(",");//获取配置文件邮箱用户名并以,切割
        if (username != null) {
            for (String user : username) {
                accountList.add(user);
            }
        }
        if (passwordList == null) {
            passwordList = new ArrayList<>();
        }
        String[] passwords = this.properties.getPassword().split(",");//获取密码
        if (passwords != null) {
            for (String password : passwords) {
                passwordList.add(password);
            }
        }
    }

    @Override
    protected void doSend(MimeMessage[] mimeMessages, Object[] originalMessages) throws MailException {
        super.setUsername(accountList.get(currentMailId));
        super.setPassword(passwordList.get(currentMailId));
        super.setHost(hostList.get(currentMailId));
        log.info("发送Host: --------------"+super.getHost());
        log.info("发送账号: --------------"+super.getUsername());
        super.setDefaultEncoding(this.properties.getDefaultEncoding().name());
        super.setJavaMailProperties(asProperties(this.properties.getProperties()));
        try {
            super.doSend(mimeMessages, originalMessages);
        }catch (Exception e){
            log.info("doSend():异常");
            e.printStackTrace();
        }
        currentMailId = ((currentMailId + 1) % accountList.size());
    }

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

    @Override
    public String getUsername() {
        return accountList.get(currentMailId);
    }
MyJavaMailComponent
@Slf4j
@Component
public class MyJavaMailComponent {

    @Autowired
    private MyJavaMailSenderImpl myJavaMailSenderImpl;

    @Autowired
    private CrawlMapper crawlMapper;

    public int sendMail(String to, String text, int pid) {
        Map<String, Object> map = new HashMap<String, Object>();
        int row = 0;
        map.put("email", to);
        try {
            String result = send(to, text);
            if (result.equals("success")) {
                row = crawlMapper.updateStatusByContext(pid);
            }
            boolean resu = result.equals("success") ? true : false;
            boolean resu2 = row > 0 ? true : false;
            log.info("邮箱发送结果:" + resu + ", 数据库状态修改:" + resu2);
        } catch (IOException e) {
            e.printStackTrace();
            row = 0;
        } catch (MessagingException e) {
            e.printStackTrace();
            row = 0;
        }
        return row;
    }

    public int sendMailTwoDay(String to, String text) {
        Map<String, Object> map = new HashMap<String, Object>();
        int row = 0;
        map.put("email", to);
        try {
            String result = send(to, text);
        } catch (IOException e) {
            e.printStackTrace();
            row = 0;
        } catch (MessagingException e) {
            e.printStackTrace();
            row = 0;
        }
        return row;
    }

    private String send(String email, String text) throws MessagingException, UnsupportedEncodingException {
        MimeMessage message = myJavaMailSenderImpl.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
        InternetAddress from = new InternetAddress();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        String date = simpleDateFormat.format(new Date());
        from.setAddress(myJavaMailSenderImpl.getUsername());
        from.setPersonal("我是一份警告邮件", "UTF-8");
        helper.setFrom(from);
        helper.setTo(email);
        helper.setSubject(date + "-信息反馈");
        helper.setText(text, true);
        myJavaMailSenderImpl.send(message);
        return "success";
    }

发送方法

@Scheduled(cron = "0 0 * * * ?")//每小时的第0分0秒执行
    public void sendEmail() {
         mailComponent.sendMail(对方邮箱, 发送内容);
         }
    }