一, 简介

    为什么会用到多邮箱发送? 在实际应用中,不管是腾讯邮箱,网易邮箱都有上限阈值,即使开通VIP也一样会遇到该问题;

    各大邮箱每天发送数量限制: https://blog.csdn.net/qq_31617637/article/details/73230942?tdsourcetag=s_pctim_aiomsg

    在项目中, 经常会遇到 550连接频率受限 的错误:

com.sun.mail.smtp.SMTPSendFailedException: 550 Connection frequency limited

 二, 解决方法

    既然遇到该问题,哪我们总不能不管吧,哪怎么解决该问题呢?

    当然最简单的办法就是: 增加发送邮箱数量,轮流执行发送;

    想想看,既然一个邮箱遭到限制, 那有10个邮箱轮流发送呢?发件数量将会是之前的10倍; 100个发送邮箱呢....

三, 具体实例

    1, 引入Maven Jar包

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

    2, 邮件发送接口

package com.richfun.wxappi.utils.email;

/**
 * 多邮箱发送邮件
 * */
public interface EmailMoreService {

    /**
     * 发送文本邮件
     * */
    boolean sendSimpleMail(String to, String subject, String content);

    /**
     * 发送HTML邮件
     * */
    boolean sendHtmlMail(String to, String subject, String content);

}

    3, 邮件发送实现

package com.richfun.wxappi.utils.email;

import com.richfun.wxappi.mapper.CommonDao;
import com.richfun.wxappi.utils.Namespace;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.mail.internet.MimeMessage;
import java.util.List;
import java.util.Map;
import java.util.Properties;

@Service
public class EmailMoreServiceImpl implements EmailMoreService {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private CommonDao<Map<String, Object>> commonDao;

    private int emailId = 0;

    private List<Map<String, Object>> listEmail;

    @PostConstruct
    private void setListEmail() {
        // 设置邮箱发送列表, 从数据库中获取可发送邮箱列表
        logger.info("=== 初始化可发送邮箱列表 ===");
        this.listEmail = commonDao.findByMapList(Namespace.EMAIL + "listEmail", null);
        if (listEmail == null || listEmail.isEmpty()) {
            logger.error("=== 可发送邮箱列表为NULL, 请检测代码 ===");
        } else {
            for (Map<String, Object> email : listEmail) {
                logger.info("=== 可发送邮箱: " + email.get("email"));
            }
        }
    }

    private synchronized void setEmailId() {
        // 设置下次可发送邮箱
        boolean isMax = emailId == (listEmail.size()-1);
        emailId = isMax ? 0 : emailId+1;
    }

    private synchronized Map<String, Object> getEmail() {
        // 获取当前可发送邮箱
        Map<String, Object> email = listEmail.get(emailId);
        setEmailId();
        return email;
    }

    private synchronized JavaMailSenderImpl getJavaMailSender() {
        // 获取邮箱发送实例
        Map<String, Object> email = getEmail();
        String host = email.get("host_addr").toString();
        String username = email.get("email").toString();
        String password = email.get("pwd").toString();
        String sslPort = email.get("ssl_port").toString();
        JavaMailSenderImpl javaMailSenderImpl = new JavaMailSenderImpl();
        javaMailSenderImpl.setHost(host);
        javaMailSenderImpl.setUsername(username);
        javaMailSenderImpl.setPassword(password);
        javaMailSenderImpl.setDefaultEncoding("UTF-8");
        Properties properties = new Properties();
        properties.setProperty("mail.smtp.auth", "true");
        properties.setProperty("mail.smtp.socketFactory.port", sslPort);
        properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        javaMailSenderImpl.setJavaMailProperties(properties);
        return javaMailSenderImpl;
    }

    @Override
    public boolean sendSimpleMail(String to, String subject, String content) {
        logger.info("简单邮件开始发送");
        try {
            JavaMailSenderImpl javaMailSender = getJavaMailSender();
            String username = javaMailSender.getUsername();
            logger.info("当前发送邮箱: " + username);
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(username);
            message.setTo(to);
            message.setSubject(subject);
            message.setText(content);
            javaMailSender.send(message);
            logger.info("简单邮件发送成功");
            return true;
        } catch (Exception e) {
            logger.error("简单邮件发送异常", e);
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean sendHtmlMail(String to, String subject, String content) {
        logger.info("HTML邮件开始发送");
        try {
            JavaMailSenderImpl javaMailSender = getJavaMailSender();
            MimeMessage message = javaMailSender.createMimeMessage();
            MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);
            String username = javaMailSender.getUsername();
            logger.info("当前发送邮箱: " + username);
            messageHelper.setFrom(username);
            messageHelper.setTo(to);
            messageHelper.setSubject(subject);
            messageHelper.setText(content, true);
            javaMailSender.send(message);
            logger.info("HTML邮件发送成功");
            return true;
        } catch (Exception e) {
            logger.error("HTML邮件发送异常", e);
            e.printStackTrace();
        }
        return false;
    }

}

四, 总结

    1, 邮件发送的配置参数都已经放到数据库,需要注意;

    2, 如果你不知道有哪些配置参数,请阅读: https://my.oschina.net/u/3681868/blog/1596181

01-14 13:40