javamail发送邮件太慢了

时间:2015-01-06 19:27:36

标签: java javamail

我正在尝试从我的JAVA网络应用程序发送电子邮件,现在电子邮件正在发送,但唯一的问题是它需要很长时间。现在速度是每秒1封电子邮件,是否可以每秒发送20封邮件。

我读过this,但我想知道是否有其他替代JAR可以更快地发送它。

这是我的代码:

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

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

public class AmazonSESSample {
    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 = "HOST";
    static final int PORT = 25;

    public static void main(String[] args) throws Exception {
        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");
        Transport transport = session.getTransport();
        ArrayList<String> emails = new ArrayList<String>(); //This is a string array with email addresses
        for (int i = 0; i < emails.size(); i++) {
            send_mail(emails.get(i).toString(), transport);
        }
    }

    public static void send_mail(String emailid, Transport transport) throws Exception {
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(FROM));
        msg.setRecipient(Message.RecipientType.TO, new InternetAddress(emailid));
        msg.setSubject(SUBJECT);
        msg.setContent(BODY, "text/plain");
        transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD);
        try {
            System.out.println("Attempting to send an email through the Amazon SES SMTP interface...");
            msg.saveChanges();
            transport.sendMessage(msg, msg.getAllRecipients());
            System.out.println("Email sent!");
        } catch (Exception ex) {
            System.out.println("The email was not sent.");
            System.out.println("Error message: " + ex.getMessage());
        } finally {
            transport.close();
        }
    }
}

0 个答案:

没有答案