Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java mail with authentication!

 
Cesar Tg.
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

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("mail.smtp.host", 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!

Thanks!
Csar.
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can somebody point to RFC number for authentication and SSL in mail? (It's for sending only)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic