Skip to Main Content
JavaMail API
book

JavaMail API

by Elliotte Rusty Harold
July 2013
Intermediate to advanced content levelIntermediate to advanced
95 pages
2h 15m
English
O'Reilly Media, Inc.
Content preview from JavaMail API

Chapter 4. Password Authentication

Hardcoding passwords in source code, as Examples 2-1 and 3-1 do, is a very bad idea to say the least. If a password is required, you should ask the user for it at runtime. Furthermore, when the user types the password, it should not be displayed on the screen. Ideally, it should not even be transmitted in clear text across the network, although in fact many current clients and servers do exactly that.

When you start a mail session, the JavaMail API allows you to provide a javax.mail.Authenticator object that it can use to get the username and password. Authenticator is an abstract class:

public abstract class Authenticator extends Object

When the provider needs to know a username or password, it calls back to the getPasswordAuthentication() method in a user-defined subclass of Authenticator. This returns a PasswordAuthentication object containing this information:

protected PasswordAuthentication getPasswordAuthentication()

Tip

These two classes are almost exactly the same as the java.net.Authenticator and java.net.PasswordAuthentication classes. Everything you know about java.net.Authenticator and java.net.PasswordAuthentication is true of javax.mail.Authenticator and javax.mail.PasswordAuthentication. The only thing you have to watch out for is that if you import both java.net.* and javax.mail.* in a class, your source code will have to use fully qualified names like java.net.Authenticator instead of short names like Authenticator.

To add runtime password ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Scripting in Java: Integrating with Groovy and JavaScript

Scripting in Java: Integrating with Groovy and JavaScript

Kishori Sharan
Spring Boot 2.0 Projects

Spring Boot 2.0 Projects

Mohamed Shazin Sadakath

Publisher Resources

ISBN: 9781449367237Errata Page