Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.IOException;
- import java.util.Properties;
- import javax.mail.*;
- public class Main{
- public static void receiveEmail(String pop3Host, String storeType,
- String user, String password) {
- try {
- //1) get the session object
- Properties properties = new Properties();
- properties.put("mail.imap.host", "imap.gmail.com");
- properties.put("mail.imap.port", "993");
- properties.put("mail.imap.ssl.enable", "true");
- Session emailSession = Session.getInstance(properties);
- Store emailStore = emailSession.getStore("imaps");
- emailStore.connect(pop3Host,993,user, password);
- //3) create the folder object and open it
- Folder emailFolder = emailStore.getFolder("INBOX");
- emailFolder.open(Folder.READ_ONLY);
- //4) retrieve the messages from the folder in an array and print it
- 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());
- }
- //5) close the store and folder objects
- 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 = "imap.gmail.com";//change accordingly
- String mailStoreType = "pop3s";
- String username= "****";
- String password= "****";//change accordingly
- receiveEmail(host, mailStoreType, username, password);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement