Improved SES Mail Speed

I am using SES and am trying to send email to almost 2000 people in one go. The code I am using is written in JAVA using the javamail library and it takes about 28 seconds to deliver 22 emails.

Is there a way to increase the speed by changing the language or library or any other way. Here is my code for reference, I am already using streams:

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();
    }

}

      

+3


source to share





All Articles