使用线程在java中发送批量电子邮件?

时间:2015-12-04 10:27:38

标签: java multithreading email sendmail bulk

嘿,我必须通过Java发送批量邮件。首先我通过smtpserver完成它我能够发送邮件,但它很慢。 所以我搜索并使用线程来实现这一目标。但是,每次总是想要连接和验证时,它们正在向smtp服务器发送新的请求。     现在我的问题是如何请求服务器连接和验证只一次 [运输运输= session.getTransport(" smtps"); transport.connect(主机,端口,用户名,密码);]

以及如何多次调用发送消息? [transport.sendMessage(msg,msg.getRecipients(Message.RecipientType.TO));] 我尝试的是如下

public class EmailService implements Runnable{
     String customerMail="";
     String  messageType="";
     String subject="";
     String message="";
     int customerMailId=0;
     int campaignId=0;




    @Override
    public void run() {
        try {
            System.out.println("inside thread");
            this.sendEmail();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setParameters(String customerMail,String messageType,String subject,String message,int customerMailId,int campaignId) {
        this.customerMail=customerMail;
        this.messageType=messageType;
        this.subject=subject;
        this.message=message;
        this.customerMailId=customerMailId;
        this.campaignId=campaignId;
    }
final String username = "**********";
        final String password = "*******";
        String host = "smtp.gmail.com";
      String password = "******";
        int port = 465;

        public void sendEmail() {
        System.out.println("inside emailservice");

        Properties props = new Properties();
        props.put("mail.smtp.username", username);
        props.put("mail.smtp.password", password);
        props.put("mail.smtp.port", port);
        props.put("mail.smtp.host", host);
        props.put("mail.transport.protocol.", "smtps");
        props.put("mail.smtps.starttls.enable", "true");
        props.put("mail.smtp.socketFactory.port", port);
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.debug", "true");
        props.put("mail.smtp.socketFactory.fallback", "false");

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });

        try {

            Transport transport = session.getTransport("smtps");
            transport.connect(host, port, username, password);

            if (messageType.equals("TO")) {

                    MimeMessage msg = new MimeMessage(session);
                    msg.setFrom(new InternetAddress(username));
                    msg.setSubject(subject);
                    System.out.println("inside to method");
                    msg.setRecipients(Message.RecipientType.TO, customerMail);
                    int mailingId = customerMailId;

                    message = message.replaceAll(".jpeg", ".jpeg?&campId=" + campaignId + "&opId=" + mailingId);
                    message = message.replaceAll(".JPEG", ".jpeg?&campId=" + campaignId + "&opId=" + mailingId);
                    message = message.replaceAll(".jpg", ".jpg?&campId=" + campaignId + "&opId=" + mailingId);
                    message = message.replaceAll(".JPG", ".jpg?&campId=" + campaignId + "&opId=" + mailingId);
                    message = message.replaceAll(".png", ".png?&campId=" + campaignId + "&opId=" + mailingId);
                    message = message.replaceAll(".PNG", ".PNG?&campId=" + campaignId + "&opId=" + mailingId);
                    String msgBody = "<a href=http://wmail.*****.in/?&campId=" + campaignId
                            + "&clkId=" + mailingId + ">" + message + "</a>" + "<br><br>" + "<b>To stop receiving these emails please" + "<a  href=http://wmail.****.in?&campId=" + campaignId
                            + "&unsubsId=" + mailingId + ">" + " click here </a>" + "to unsubscribe.</b>";
                    msg.setContent(msgBody, "text/html; charset=UTF-8");
                    transport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));   }
            System.out.println("inside sucees");
        } catch (MessagingException e) {
            e.printStackTrace();
        }

    }
    public void MailSender(List<NameEmailRecord> receiverEmail, String messageType, String subject, String message, int campaignId)
    {
        String mail="";
        int mailId=0;       
          for (NameEmailRecord ner : receiverEmail) {
           mail=ner.getMailingEmail();
           mailId=ner.getMailingEmailId();
            EmailService es = new EmailService();            es.setParameters(mail,messageType,subject,message,mailId,campaignId);
            Thread t = new Thread(es);
            t.start();       
        }       
    }

}

1 个答案:

答案 0 :(得分:0)

SMTP连接发生在Socket上。因此,对于任何一个SMTP服务器的连接,它一次一个地发送消息(即单个消息,每条消息可能有许多接收者)。

我想在你的情况下,这意味着在登录成本和每线程电子邮件之间找到一个很好的折衷方案。

通过一个传输发送多个电子邮件(通过相同的SMTP服务器发送),您只需创建一个传输并重复使用它,为每个要发送的邮件调用sendMessage。