Menu



How to receive mail in java


Two Jar file needed for recieve mail

Here we using 1 files for recieve mail in Java:

recieve-mail.java

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 recievemail{
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.studentstutorial.com";//change accordingly
String mailStoreType = "pop3";
String username= "info@studentstutorial.com";
String password= "password";//Put Your password here
receiveEmail(host, mailStoreType, username, password);
}
}