Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JavaMail: How to tell if SMTP server requires authentication

843834Jan 5 2009 — edited Jan 7 2009
I am writing an application that sends notification emails. I have a configuration screen for the user to specify the SMTP hostname and optionally a username and password, and I want to validate the settings. Here is the code I am using to do this:
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
if (mailUsername != null || mailPassword != null)
    props.put("mail.smtp.auth", "true");
Session session = Session.getInstance(props, null);
transport = session.getTransport();
transport.connect(mailHostname, mailUsername, mailPassword);
transport.close();
This works if the user enters a username and password (whether they are valid or not), or if the username and password are empty but the SMTP server does not require authentication. However, if the server requires authentication and the username and password are empty, the call to transport.connect() succeeds, but the user will get an error later on when the app tries to actually send an email. I want to query the SMTP server to find out if authentication is required (not just supported), and inform the user when they are configuring the email settings. Is there any way to do this? I suppose I could try sending a test email to a dummy address, but I was hoping there would be a cleaner way.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 4 2009
Added on Jan 5 2009
2 comments
552 views