Java Mail Authenticator
by, 04-25-2012 at 10:09 PM (1474 Views)
Benefit of the Authenticator class might be taken by the Mail APIs, just like java.net packages. Protected resources are being accessed by this class by using Username/Password. This resource might be simple file, server or anything else. In case of Java Mail, server would be the resource. In essence, Authenticator object gets passed to getInstance()/getDefaultInstance() method as a parameter which controls the Session object’s security aspect.
Authenticator could be used by 2 methods. First is that Authenticator could be subclasses separately and objects could be provided to getInstance()/getDefaultInstance() method. Second is that Authenticator is subclassed by same class which will make the mailing logic encapsulate. Former approach’s example as given below:
To make use of the other approach the code is:Java Code:Properties props = new Properties(); // fill props with any information Authenticator auth = new MyAuthenticator(); Session session = Session.getDefaultInstance(props, auth);
Java Code:Properties props = new Properties(); // fill props with any information Session session = Session.getDefaultInstance(props, this);