0

I am trying to send emails from my JAVA web application, for now emails are going but the only problem is it takes very long time. Now the speed is 1 email per second, is it possible to make it 20 mails per sec.

I read this but I want to know is there any alternative JAR available to send it much faster.

Here's my code:

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();
        }
    }
}
Community
  • 1
  • 1
Niraj Chauhan
  • 7,677
  • 12
  • 46
  • 78
  • 5
    Have you tried not actually "reconnecting" to the mail server every time you send a mail? Moving the "transport.connect(HOST, SMTP_USERNAME, SMTP_PASSWORD);" (and closing it) outside the for loop should help at least.. – Peter Svensson Jan 06 '15 at 19:31
  • 2
    Send lots at once. Also, you could thread off multiple connections to the server. – Martin James Jan 06 '15 at 19:32
  • @PeterLiljenberg thanks for the tip, I tried and it worked but now it is sending 2mails per second, is there any faster way to do it – Niraj Chauhan Jan 06 '15 at 19:34
  • @MartinJames threading might be the solution but I am going to deal almost 1000 emails at a go. Looking for best solution possible – Niraj Chauhan Jan 06 '15 at 19:35
  • In addition to the suggestions above, you need to do some performance analysis to determine **why** it's so slow. Perhaps your server is purposely limiting the rate at which you can send message to avoid spam? If you're really using Amazon SES, that seems likely. Perhaps you have a firewall or anti-virus that's slowing it down? There are so many possible reasons that without some data it's hard to guess which reason is the one causing your problem. – Bill Shannon Jan 07 '15 at 03:37

0 Answers0