Home Javamail Java Mail Mail Authenticator Example



Java Mail Mail Authenticator Example
Posted on: December 19, 2008 at 12:00 AM
This Example shows you how to use Authenticator using javamail api. Authenticator class object that knows how to obtain authentication for a network connection.

Java Mail Mail Authenticator Example

     

This Example shows you how to use Authenticator using javamail api. Authenticator class object that knows how to obtain authentication for a network connection. getPasswordAuthentication method return PasswordAuthentication class object. The class PasswordAuthentication is information about username and password.

In the code given below we have extended Authenticator class that represents an object that knows how to authenticate network connection.

Method description that are used in this program...
getPasswordAuthentication() method is called when password authorization is needed.

MailAuthenticator.java

import java.util.*;
import javax.mail.*;

public class MailAuthenticator extends Authenticator {

  private String username,  password;

  MailAuthenticator(String username, String password) {
  this.username = username;
  this.password = password;
  }

  public void readMail() throws Exception {

  // Get system properties
  Properties properties = System.getProperties();

  // Get the default Session object.
  Session session = Session.getDefaultInstance(properties, this);
  URLName url = new URLName("pop3://test@192.168.10.205");

  // Get a Store object for the given URLName.
  Store store = session.getStore(url);
  store.connect();

  //Create a Folder object corresponding to the given name.
  Folder folder = store.getFolder("inbox");

  // Open the Folder.
  folder.open(Folder.READ_ONLY);

  Message[] message = folder.getMessages();

  // Display message.
  for (int i = 0; i < message.length; i++) {
  System.out.println("------------ Message " + (i + 1) + " ------------");
  System.out.println("SentDate : " + message[i].getSentDate());
  System.out.println("From : " + message[i].getFrom()[0]);
  System.out.println("Subject : " + message[i].getSubject());
  System.out.println("Content : " + message[i].getContent());
  }
  folder.close(true);
  store.close();
  }

  protected PasswordAuthentication getPasswordAuthentication() {
  return new PasswordAuthentication(username, password);
  }

  public static void main(String args[]) throws Exception {
  new MailAuthenticator("test", "test").readMail();
  }
} 

Output:

------------ Message 1 ------------
SentDate : Tue Jul 15 18:44:41 IST 2008
From : test@localhost
Subject : hi..!
Content : Hi ......

------------ Message 3 ------------
SentDate : Tue Jul 15 17:57:32 IST 2008
From : test
Subject : Resistance is futile.
Content : Resistance is futile. You will be assimilated!

------------ Message 3 ------------
SentDate : Mon Jul 14 21:10:35 IST 2008
From : test@localhost
Subject : hi..!
Content : Hi this is komal choudhary......

Download code

Related Tags for Java Mail Mail Authenticator Example:
javacapiauthenticationclassormformnetworkobjectioconnectionmailmethoduserformatgetpasswordwordnamereturnusingthisaiconnectshowforexamplejavamailworktousernameexaminformationwssheilauthuseinnormpassinfoasmntoutnetcanetjclpimehowobjcattorxaxampsurnetwatkishainfmplandvatwssrdrdathshoswavabatiauthenticatoraphatinforminformatjeicaicapleplndono


More Tutorials from this section

Ask Questions?    Discuss: Java Mail Mail Authenticator Example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.