Meaningless Drivel is fun!*
The moose likes Other JSE/JEE APIs and the fly likes Sending Mail via SMTP server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Sending Mail via SMTP server" Watch "Sending Mail via SMTP server" New topic
Author

Sending Mail via SMTP server

Mithun Pissay
Greenhorn

Joined: Dec 12, 2009
Posts: 6
Hi guys,

I m having problem with sending mail with POP3 server, the funny thing is when I try to run the same block of code via public static void main() it runs fine, but when it is ran through a batch flow throws error @
transport.connect(hostName, userName, password);

saying....

DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "Smtp.mail.apac.microsoftonline.com", port 25, isSSL false

javax.mail.MessagingException: Could not connect to SMTP host: Smtp.mail.apac.microsoftonline.com, port: 25;
nested exception is:
java.net.ConnectException: Connection refused: connect

One more thing to notice here is even though I set POP3 port to ["mail.smtp.port", "587"] in the session properties, its using port number as 25,
and when I explicitly ask it to connect via port over 587

i.e transport.connect(hostName, 587, userName, password);

it throws exception as...

DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "Smtp.mail.apac.microsoftonline.com", port 587, isSSL false
220 smtp.mail.apac.microsoftonline.com Microsoft ESMTP MAIL Service ready at Tue, 14 Jun 2011 03:07:08 -0700
DEBUG SMTP: connected to host "Smtp.mail.apac.microsoftonline.com", port: 587

javax.mail.AuthenticationFailedException: No authentication mechansims supported by both server and client

I have no idea for what and why its causing so

Please help me out,
thanks and Regards
Mithun


Mithun Pissay
Greenhorn

Joined: Dec 12, 2009
Posts: 6
and one more thing for you info,

Though i m setting "mail.smtp.auth","true" in session properties the SMTP DEBUG is showing as

DEBUG SMTP: useEhlo true, useAuth false

!!!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42033
    
  64
You can't send email via pop3; you need to use an SMTP server.


Ping & DNS - my free Android networking tools app
Mithun Pissay
Greenhorn

Joined: Dec 12, 2009
Posts: 6
Hey Ulf, sorry for confusion indeed mail is sent over SMTP protocol, and have used SMTP for sending mail... and if you had noticed my post i m using Smtp.mail.apac.microsoftonline.com host as well.
I apologize in heat of the situation and deadline my Post header was misleading...
but the issue is still open please provide your insight on the problem.

And gonna change the Post Subject/header so others wont get mislead.

Thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42033
    
  64
How are you providing the authentication details?
Mithun Pissay
Greenhorn

Joined: Dec 12, 2009
Posts: 6
Hi Ulf, FYI
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", "Smtp.mail.apac.microsoftonline.com");
props.setProperty("mail.smtp.port", "587");
props.setProperty("mail.smtp.auth","true");
props.put("mail.smtp.starttls.enable","true");
...

Session mailSession = Session.getDefaultInstance(props, null);
SMTPTransport transport = (SMTPTransport) mailSession.getTransport("smtp");
transport.connect("Smtp.mail.apac.microsoftonline.com", userEmailId, userEmailPwd);
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));

Getting java mail Session with passing authenticator as null later part in the code SMTPTransport would connect and authenticate along with userEmailId and password.

Further, for your information, after you brought up authentication perspective, i tried getting session with passing an authenticator object with userEmailId and password set in the auth object, And the result was same with above test case (referring to initial post) execution.
Mithun Pissay
Greenhorn

Joined: Dec 12, 2009
Posts: 6
Hi guys, thanks for your time and brain stroming over my posted issue,

I found out the issue was with not using System properties (i.e System.getProperties() ), even though I was setting designated port (587) into java.util.Properties it was taking default port 25 for sending mail via SMTP as it was reading from the system properties for getting port and it had not been set while getting mail Session project.

But one thing is still bugging me is why with all the session properties set into java.util.Properties object for getting mail Session was working when called independently and not working when the batch flow took over the call to send mail,
and let me tell you i was not using properties object got from System.getProperties() call before any where. is it the API designed so to read from System.getProperties() properties ? I am using standard mail.jar i.e java extended API

If any of you guys know what exactly the reason please do reply to this post.

Thank you again all of you guys for supporting
 
GeeCON Prague 2014
 
subject: Sending Mail via SMTP server