0% found this document useful (0 votes)
14 views

Email Receive

The code shows how to receive emails from a POP3 email server in Java. It connects to the email server using the provided host, username and password, then opens the inbox folder and loops through all messages to print the subject, sender and content of each email.

Uploaded by

D NARENDRAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Email Receive

The code shows how to receive emails from a POP3 email server in Java. It connects to the email server using the provided host, username and password, then opens the inbox folder and loops through all messages to print the subject, sender and content of each email.

Uploaded by

D NARENDRAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

//Receiving email 

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 = "sankaracollege.edu.in";
  String mailStoreType = "pop3";  
  String username= "[email protected]";  
  String password= "xxxxx";
  
  receiveEmail(host, mailStoreType, username, password);  
  
 }  
}  

You might also like