logo

Receiving email in Java || email Pipe !

CATEGORY : Information Technology AUTHOR : Java

For receiving email Store and Folder classes are used in collaboration with MimeMessage, Session and Transport classes.

For better understanding of this example, learn the steps of sending email using JavaMail API first.

For sending the email using JavaMail API, you need to load the two jar files:

/*

To change this license header, choose License Headers in Project Properties.

To change this template file, choose Tools | Templates

and open the template in the editor.

*/

/**

  *

@author Anand

*/

import java.io.IOException;

import java.util.Properties;

import javax.mail.Folder;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.NoSuchProviderException;

import javax.mail.Session;

import com.sun.mail.pop3.POP3Store;

public class ReceiveMail{

public static void receiveEmail(String pop3Host, String storeType, String user, String password) {

   try {

    Properties properties = new Properties();

    properties.put("mail.pop3.host", pop3Host);

    Session emailSession = Session.getDefaultInstance(properties);

POP3Store emailStore = (POP3Store) emailSession.getStore(storeType);

    emailStore.connect(user, password);

Folder emailFolder = emailStore.getFolder("INBOX");

    emailFolder.open(Folder.READ_ONLY);

Message[] messages = emailFolder.getMessages();

    for (int i = 0; i < messages.length; i++) {

     Message message = messages[i];

     System.out.println("---------------------------------");

     System.out.println("Email Number " + (i + 1));

     System.out.println("Subject: " + message.getSubject());

     System.out.println("From: " + message.getFrom()[0]);

     System.out.println("Text: " + message.getContent().toString());

    }

emailFolder.close(false);

    emailStore.close();

} catch (NoSuchProviderException e) {e.printStackTrace();}

   catch (MessagingException e) {e.printStackTrace();}

   catch (IOException e) {e.printStackTrace();}

  }

public static void main(String[] args) {

String host = "mail.rdasoftech.com";//change accordingly

   String mailStoreType = "pop3";

   String username= "[email protected]";

   String password= "*";//change accordingly

receiveEmail(host, mailStoreType, username, password);

}

}

Customer Reviews

  • themes

    Price
    Value
    Quality

    themes

    Review byplaza

    Posted on 12/9/16

You're reviewing:

Joust Duffle Bag

Your Rating *
Value
Quality
Price

Related Books