views:

183

answers:

2

We have a thread program that sends bulk mail. The information like

1. To
2. Subject
Etc.

are fetched from database, mail is composed and pushed to SMTP server. One of our customer sent a bulk mail with 2390 email. After sending 40 emails, suddenly the following exception occurred

EXCEPTION
javax.mail.AuthenticationFailedException

STACKTRACE
javax.mail.Service.connect(Service.java:306)
javax.mail.Service.connect(Service.java:156)
javax.mail.Service.connect(Service.java:105)
...............
java.lang.Thread.run(Thread.java:619)

and the rest 2350 emails failed.

Why does this occur?

Thanks for the Suggestions and Help

Ezhil

==============================================

My Code:

Session session = Session.getInstance(properties, new SMTPAuthenticator(smtpAuthenticationBean.getUserName(), smtpAuthenticationBean.getPassword()))) : (Session.getInstance(properties, null))
for each email id
{

    InternetAddress iAddress    = new InternetAddress(getFromHeader(jobListBean.getFromDisplayName(), jobListBean.getFromEmail()));
    Multipart multipart         = new MimeMultipart(); // By default, Content Type is "mixed"


    msg.setSubject(jobListBean.getSubject());
    msg.setSentDate(new Date());

    // Set Internet Headers
    msg.setHeader("Importance", priorityType);

    msg.setHeader("Disposition-Notification-To", jobListBean.getFromEmail());

    FileDataSource fds = new FileDataSource(tempAbsoluteFileName);
    MimeBodyPart htmlBodyPart = new MimeBodyPart();

    String fileContent = org.objectstyle.woproject.util.FileStringScanner.stringFromFile(new File(tempAbsoluteFileName));
    htmlBodyPart.setText(fileContent);
    multipart.addBodyPart(htmlBodyPart);

    msg.setContent(multipart);

    InternetAddress address[] = InternetAddress.parse(emailList.toString(), true);

    Transport smtpTransport = session.getTransport();
    smtpTransport.addTransportListener(this);

    smtpTransport.connect();

    smtpTransport.sendMessage(msg, address);

    smtpTransport.close();

    File file = new File(tempAbsoluteFileName);
    file.delete();
}

====================================

Yes there is a chance for smtp server to get disconnected or not respond since its thread program, i can say at max case more than 1000 mails can get pushed in to smtp server at same time.

At any cast, will smtp server throw

EXCEPTION
javax.mail.AuthenticationFailedException

STACKTRACE
javax.mail.Service.connect(Service.java:306)
javax.mail.Service.connect(Service.java:156)
javax.mail.Service.connect(Service.java:105)
...............
java.lang.Thread.run(Thread.java:619)

if it is not able to serve our request

=============

Still i need to look in to SMTP server log.

Ezhil

+1  A: 

An AuthenticationFailedException has nothing to do with your code, it is raised when the SMTP server returns an authentication failure.

From the javadoc:

This exception is thrown when the connect method on a Store or Transport object fails due to an authentication failure (e.g., bad user name or password).

So you will need to investigate your mail server to find out why it accepts some mail but not others. One thing I can think of would be some sort of rate-limiting mechanism.

Adam Batkin
I have little knowledge about rate-limiting, even is there any situation"Authentication Failed Exception"occurs because of rate-limiting factor
Ezhil
The mail server isn't throwing an exception, it is just returning an error (for all you know, the internal error could be something like "we are worried you are sending too many messages and may have a virus, talk to your sysadmin" but JavaMail interprets that as an authentication error, which is why you get that exception)
Adam Batkin
Thanks adam, we talked with the smtp server providers.They said, "throttling going on, even in a dedicated server environment. In this case it is to protect the server from being overburdened".we have requested for smtp server log from the providers, to confirm it.
Ezhil
+1  A: 

Is it the same message being sent over and over again to the mail server?

Instead of doing the for loop for each email address, you should send all the emails in one call using sendMessage. (which your code does seem to be doing)

smtpTransport.sendMessage(msg, address); 

The mail server will then take the one copy of the message and send it to multiple addresses.

May be that will prevent the rate threshold.

JoseK
In such type of bulk mails. Client will ask for individual identification.Such as in "To" field only one email should be there. Which means he is personally addressed. Leads we need to compose mail for each separately.There is content differentiation for each mail.For Example. It can say...Dear Alka in one mail and Dear Raju in another mail. So only we have done like this.
Ezhil
Then you might have split into multiple threads capped at 1000 mails per thread
JoseK
Thanks josek, Yes, we should restrict to some extent the no of threads reaching the smtp server at same time to avoid this. Since smtp server should balances our request.
Ezhil