JavaMail API - SPLessons

JavaMail Receive Email

Home > Lesson > Chapter 5
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

JavaMail Receive Email

JavaMail Receive Email

shape Introduction

JavaMail Receive Email, POP3 stands for post office protocol 3 is utilized to recieve an emails from server to the client machine. The store class and folder class are used with MineMessage in collaboration and transport, session is required to receive the email. Let's see steps of receiving email using JavaMail API first. Here the POP server is used to provide POP3 store object to receive the email.

shape Conceptual figure

Steps to receive the emails: For receiving the email the process involves session object, POP3 store object, folder object. First creating session object and through it access the email over POP3 server.

shape Examples

Let us see the example how to receive the email using javaMail API [c]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 javax.mail.Store; public class receivemail { public static void check(String host, String storeType, String user, String password) { try { //create properties field Properties properties = new Properties(); properties.put("mail.pop3.host", host); properties.put("mail.pop3.port", "995"); properties.put("mail.pop3.starttls.enable", "true"); Session emailSession = Session.getDefaultInstance(properties); //create the POP3 store object and connect with the pop server Store store = emailSession.getStore("pop3s"); store.connect(host, user, password); //create the folder object and open it Folder emailFolder = store.getFolder("INBOX"); emailFolder.open(Folder.READ_ONLY); // retrieve the messages from the folder in an array and print it Message[] messages = emailFolder.getMessages(); System.out.println("messages.length---" + messages.length); for (int i = 0, n = messages.length; i < n; 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()); } //close the store and folder objects emailFolder.close(false); store.close(); } catch (NoSuchProviderException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { String host = "pop.gmail.com";// change accordingly String mailStoreType = "pop3"; String username = "9saireddy@gmail.com";// change accordingly String password = "splessons";// change accordingly check(host, mailStoreType, username, password); } }[/c] Output:

Summary

shape Key Points

  • JavaMail Receive Email - The store class and folder class are used with MineMessage in collaboration and transport, session is required to receive the email.
  • JavaMail Receive Email - For receiving the email the process involves session object, POP3 store object, folder object.
  • JavaMail Receive Email - Close the store and folder objects.