SES提高邮件速度

时间:2015-01-07 06:26:31

标签: java javamail amazon-ses

我正在使用SES,并试图一次性向近2000人发送电子邮件。我使用的代码是使用javamail库在JAVA中编写的,它需要大约28秒才能发送22封邮件。

有什么方法可以通过更改语言或库或任何其他方式来提高速度。这是我的代码供参考,我已经在使用线程:

import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class AmazonSESSample implements Runnable {
    private String to = "";
    public static Properties props = System.getProperties();
    public static Session session = Session.getDefaultInstance(props);

    static final String FROM = "info@domain.com";
    static final String BODY = "This email was sent through the Amazon SES SMTP interface by using Java.";
    static final String SUBJECT = "Amazon SES test (SMTP interface accessed using Java)";
    static final String SMTP_USERNAME = "username";
    static final String SMTP_PASSWORD = "password";
    static final String HOST = "ses_host";
    static final int PORT = 000;

    public static Transport transport;

    @Override
    public void run() {
        try {
            this.sendMail();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    public void setParameters(String to) {
        this.to = to;
    }

    private void sendMail() throws MessagingException {
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(FROM));
        msg.setRecipients(Message.RecipientType.TO, to);
        msg.setSubject(SUBJECT);
        msg.setContent(BODY, "text/plain");
        try {
            System.out.println("Attempting to send an email through the Amazon SES SMTP interface...");
            msg.saveChanges();
            transport.sendMessage(msg, msg.getAllRecipients());
            Date date = new Date();
            System.out.println("Email sent!" + date.toString());
        } catch (Exception ex) {
            System.out.println("The email was not sent.");
            System.out.println("Error message: " + ex.getMessage());
        }
    }

    public static void main(String[] args) throws MessagingException {
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.port", PORT);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.starttls.required", "true");
        ArrayList<String> emails = new ArrayList<String>(); // String list of emails
        transport = session.getTransport();
        transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD);
        Date date = new Date();
        System.out.println(date.toString());

        for (int i = 0; i < emails.size(); i++) {
            AmazonSESSample sendmail = new AmazonSESSample();
            sendmail.setParameters(emails.get(i).toString());
            Thread t = new Thread(sendmail);
            t.start();
        }
        // transport.close();
    }

}

0 个答案:

没有答案