File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Other JSE/JEE APIs and the fly likes Java mail with authentication! 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 "Java mail with authentication!" Watch "Java mail with authentication!" New topic

Java mail with authentication!

Cesar Tg.

Joined: Jul 05, 2004
Posts: 16

I am using java mail in a web application, i need to use authentication.
I've seen code on the web, in which is implemented a class like this and then is used when creating the session object,

private class SMTPAuthenticator extends javax.mail.Authenticator

public PasswordAuthentication getPasswordAuthentication()
String username = "user";
String password = "password";
return new PasswordAuthentication(username, password);

then ...

Properties props = new Properties();
props.put("", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");

Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props, auth);

i am actually getting the mail session from context, by using lookups:

Context initial = new InitialContext();
MailConfiguration mailConfig = (MailConfiguration)initial.lookup(.....

i don't have any information harcoded, everything is defined in Admin Console in Application Server (smtp server, protocols, ...)

my questions is: for authentication does it work the same way? i mean, should I define password and user in admin console? because i don't find any information on the web, or should I write code like the one presented? which i don't think is the best solution, becuase i need to hardcoded usr and password or what about if afterwards i connect to a server which
not require authentification to send emails..., i should change my code...

Any hints or link on the web will be appreciated!

Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

for authentication does it work the same way? i mean, should I define password and user in admin console?
That is what I would try. Do you not have access to a server where you can try it yourself? I expect you would have to restart the server after changing that configuration.
dema rogatkin
Ranch Hand

Joined: Oct 09, 2002
Posts: 294
Can somebody point to RFC number for authentication and SSL in mail? (It's for sending only)

Tough in space?, <a href="" target="_blank" rel="nofollow">Get J2EE servlet container under 150Kbytes here</a><br />Love your iPod and want it anywhere?<a href="" target="_blank" rel="nofollow">Check it here.</a><br /><a href="" target="_blank" rel="nofollow">Curious about generic in Java?</a><br /><a href="" target="_blank" rel="nofollow">Hate ant? Use bee.</a><br /><a href="" target="_blank" rel="nofollow">Need contacts anywhere?</a><br /><a href="" target="_blank" rel="nofollow">How to promote your business with a search engine</a>
I agree. Here's the link:
subject: Java mail with authentication!
It's not a secret anymore!