Last active
December 10, 2017 17:57
-
-
Save NurElHuda/bb8437f6df06e7f392441f3a472318ba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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. | |
*/ | |
package zero; | |
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; | |
/** | |
* | |
* @author Nur | |
*/ | |
public class CheckingMails { | |
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"); | |
properties.put( "mail.pop3s.ssl.trust", host); | |
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 = 10; 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 = "[email protected]";// change accordingly | |
String password = "Password";// change accordingly | |
check(host, mailStoreType, username, password); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment