views:

149

answers:

2

We found out today that for two of our customers, JavaMail is not authenticating and so our product can't send email. We have been sending emails through our own server for years, as well as for several other customers, and we thought we were using authentication for all of those.

The code proceeds as follows:

    transport = session.getTransport("smtp");
    transport.connect(hostName, port, user, password);

According to the documentation here, JavaMail should be using authentication if we used this form of the connect() method. We aren't setting any properties, but if I'm reading that page properly, we don't need to.

We are using the mail.jar out of JBoss 4.2.1.GA.

What do we need to do differently?

Update: if I use the other method on that documentation page (setting mail.smtp.auth property and providing an Authenticator), authentication finally works. But what were we doing wrong with this method?

+2  A: 

Try props.put("mail.debug", "true"); for possible debug info which will give you more insight.

In addition, if this is a Windows box, have a look for firewall or anti-virus running: http://forums.sun.com/thread.jspa?threadID=590866

cherouvim
After trying this, it became obvious that Java simply doesn't do what the docs say. "Call the Transport connect method explicitly with username and password arguments" does not cause authentication. You MUST "Provide an Authenticator object when creating your mail Session and provide the username and password information during the Authenticator callback." Sucks to be us, I guess. I recoded it. Thanks, Sun...
skiphoppy
+1  A: 

I finally discovered that I was calling:

transport.send(message, message.getAllRecipients());

, which is a static method, instead of:

transport.sendMessage(message, message.getAllRecipients());

I think this is why it wasn't authenticating, so I think this is the real answer. Would've helped if I'd posted that piece of code, but I had no idea that was where the problem was. Can't for the life of me figure out why Transport.send() is a static method, but if you know, please tell me.

skiphoppy