在推广当中一定会用到的武器,那就是发送大量的营销性质的邮件。对于个人站长来说,也会用到,用于推广的自己的站点。同时由于搭建smtp服务器需要一定的技术及成本,为此我们可以利用一些免费的smtp服务器。比如说,新浪邮箱、网易邮箱、雅虎邮箱等等都会提供smtp服务,但是免费的smtp服务器会有发送邮件数目的限制,为此需要申请很多这样的邮箱,然后随机挑选一个邮箱来发送邮件。

 

java批量上传如何接收_java


 基上以上的这些需求,我编写了一个利用线程池,随机挑选发信箱的批量邮件发送器。在此分享给各位学友。。。

 

EmailConfig.java

这是发信邮件的配置类:

 

package com.tongtongxue.mail;

public class EmailConfig {
    private String username;
    private String password;
    private String smtpHost;
    private String serverPort;
    private boolean isSSL;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSmtpHost() {
        return smtpHost;
    }

    public void setSmtpHost(String smtpHost) {
        this.smtpHost = smtpHost;
    }

    public String getServerPort() {
        return serverPort;
    }

    public void setServerPort(String serverPort) {
        this.serverPort = serverPort;
    }

    public boolean isSSL() {
        return isSSL;
    }

    public void setSSL(boolean isSSL) {
        this.isSSL = isSSL;
    }
}

 

EmailContent.java

这是邮件内容的封装类:

package com.tongtongxue.mail;

public class EmailContent {
    private String subject;
    private String content;

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

 

EmailAuthenticator.java

这是用于发信邮件的用户名密码校验类:

package com.tongtongxue.mail;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

public class EmailAuthenticator extends Authenticator {
    private String username;
    private String password;

    public EmailAuthenticator() {
    }

    public EmailAuthenticator(String username, String password) {
        this.username = username;
        this.password = password;
    }

    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(username, password);
    }
}

 

RandomUtil.java

这是产生随机数的工具类:

package com.tongtongxue.mail;

import java.util.Random;

public class RandomUtil {

    public static int getRandomInt(int size) {
        Random random = new Random();
        return random.nextInt(size);
    }
}

 

EmailHelper.java

这是邮件发送器的核心类:

package com.tongtongxue.mail;

import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

public class EmailHelper {
    private ExecutorService executorService;

    public EmailHelper() {
        // 默认启动线程个数为10的线程池
        executorService = Executors.newFixedThreadPool(10);
    }

    public EmailHelper(int num) {
        // 指定初始化线程个数为num的线程池
        executorService = Executors.newFixedThreadPool(num);
    }

    public void sendMail(List receiveAdds, List emailConfigs, EmailContent emailContent) {
        // 用于发送邮件的邮箱配置个数
        int size = emailConfigs.size();

        // 循环待发送的邮箱地址
        for (String receiveAdd : receiveAdds) {
            // 随机获取一个用于发送邮件的邮箱配置
            EmailConfig emailConfig = emailConfigs.get(RandomUtil.getRandomInt(size));
            executorService.execute(new EmailItem(emailConfig, emailContent, receiveAdd));
        }
    }

    public void shutdown() {
        executorService.shutdown();
    }

    /**
     * 这个内部类,是真正用于发送邮件
     */
    class EmailItem implements Runnable {
        private EmailConfig emailConfig;
        private EmailContent emailContent;
        private String receiveAdd;

        public EmailItem(EmailConfig emailConfig, EmailContent emailContent, String receiveAdd) {
            this.emailConfig = emailConfig;
            this.emailContent = emailContent;
            this.receiveAdd = receiveAdd;
        }

        @Override
        public void run() {
            Properties prop = new Properties();
            prop.put("mail.smtp.host", emailConfig.getSmtpHost());
            prop.put("mail.smtp.port", emailConfig.getServerPort());
            prop.put("mail.smtp.auth", "true");
            String username = emailConfig.getUsername();
            EmailAuthenticator authenticator = new EmailAuthenticator(username, emailConfig.getPassword());
            // 如果需要ssl来发送邮件,则
            if (emailConfig.isSSL()) {
                prop.put("mail.smtp.socketFactory.port", emailConfig.getServerPort());
                prop.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            }

            // 根据邮件会话属性和密码验证器构造一个发送邮件的session
            Session sendMailSession = Session.getDefaultInstance(prop, authenticator);
            sendMailSession.setDebug(false);

            try {
                // 根据session创建一个邮件消息
                Message mailMessage = new MimeMessage(sendMailSession);
                // 设置自定义发件人昵称
                String nick = MimeUtility.encodeText("通通学");
                // 创建邮件发送者地址
                Address from = new InternetAddress(nick + " ");
                // 设置邮件消息的发送者
                mailMessage.setFrom(from);
                // 创建邮件的接收者地址,并设置到邮件消息中
                Address to = new InternetAddress(receiveAdd);
                // Message.RecipientType.TO属性表示接收者的类型为TO
                mailMessage.setRecipient(Message.RecipientType.TO, to);
                // 设置邮件消息的主题
                mailMessage.setSubject(emailContent.getSubject());
                // 设置邮件消息发送的时间
                mailMessage.setSentDate(new Date());
                // MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象
                Multipart mainPart = new MimeMultipart();
                // 创建一个包含HTML内容的MimeBodyPart
                BodyPart html = new MimeBodyPart();
                // 设置HTML内容
                html.setContent(emailContent.getContent(), "text/html; charset=utf-8");
                mainPart.addBodyPart(html);
                // 将MiniMultipart对象设置为邮件内容
                mailMessage.setContent(mainPart);
                // 发送邮件
                Transport.send(mailMessage);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

 

Main.java

这是用于测试的类:

package com.tongtongxue.mail;

import java.util.ArrayList;
import java.util.List;

/**
 * 用于测试发送邮件的类
 */
public class Main {

    public static void main(String[] args) {
        // 初始化用于发送邮件的邮箱配置
        List emailConfigs = initEmailConfigs();
        // 初始化接收邮件的email地址
        List receiveAdds = initReceiveAdds();
        // 初始化邮件发送的内容
        EmailContent emailContent = initEmailContent();

        // 初始化<a href="http://www.tongtongxue.com/tags/%e9%82%ae%e4%bb%b6%e5%8f%91%e9%80%81%e5%99%a8" title="查看更多关于邮件发送器的文章" target="_blank">邮件发送器</a>
        EmailHelper emailHelper = new EmailHelper();
        emailHelper.sendMail(receiveAdds, emailConfigs, emailContent);

        emailHelper.shutdown();

    }

    private static List initEmailConfigs() {
        List emailConfigs = new ArrayList();
        EmailConfig emailConfig01 = new EmailConfig();
        emailConfig01.setUsername("xxxxx@163.com");
        emailConfig01.setPassword("xxxxx");
        emailConfig01.setServerPort("25");
        emailConfig01.setSmtpHost("smtp.163.com");
        emailConfig01.setSSL(false);
        emailConfigs.add(emailConfig01);

        EmailConfig emailConfig02 = new EmailConfig();
        emailConfig02.setUsername("xxxxx@yahoo.com");
        emailConfig02.setPassword("xxxxx");
        emailConfig02.setServerPort("465");
        emailConfig02.setSmtpHost("smtp.163.com");
        emailConfig02.setSSL(true);
        emailConfigs.add(emailConfig02);

        return emailConfigs;
    }

    private static List initReceiveAdds() {
        List receiveAdds = new ArrayList();
        receiveAdds.add("xxxxx@qq.com");
        receiveAdds.add("xxxxx@qq.com");
        receiveAdds.add("xxxxx@qq.com");
        return receiveAdds;
    }

    private static EmailContent initEmailContent() {
        EmailContent emailContent = new EmailContent();
        emailContent.setSubject("测试邮件邮件");
        emailContent.setContent("<title>Test</title><h1>这是一封测试邮件。。。欢迎来到通通学,http://www.tongtongxue.com</h1>");
        return emailContent;
    }
}