TODAY JAVA SCHOOL

In java school, programming, design, computer general knowledge, web application, software, web services, social media, digital marketing, oops, concept of programming language, oops feature, console media, graphics medium, first programming, c, c ++ , Java, PHP, SQL, MySQL, HTML, HTML_5, J_query, JavaScript, Bootstrap, Framework, images with logos, examples, shared and explained.

https://www.amazon.in/b?node=26373545031&linkCode=ll2&tag=1234567801cdb-21&linkId=3b9882431b00409b44141e0344b35a15&language=en_IN&ref_=as_li_ss_tl

Breaking

Monday 27 April 2020

Receiving Email In Java | Receiving Email JavaMail

Receiving email in Java

For receiving email Store and Folder classes are used in collaboration with MimeMessage, Session and Transport classes.

For better understanding of this example, learn the steps of sending email using JavaMail API first.

For sending the email using JavaMail API, you need to load the two jar files:
  • mail.jar
  • activation.jar
download image receiving email in java

Fig: Receiving Email In Java


Steps for receiving the email using JavaMail API

There are 5 steps to receive the email using JavaMail API. They are:
1.    Get the session object
2.    create the POP3 store object and connect with the pop server
3.    create the folder object and open it
4.    retrieve the messages from the folder in an array and print it
5.    close the store and folder objects

Example of Receiving email in 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 ReceiveMail{  
    
   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.pop3.host", pop3Host);  
   Session emailSession = Session.getDefaultInstance(properties);  
     
   //2) create the POP3 store object and connect with the pop server  
   POP3Store emailStore = (POP3Store) emailSession.getStore(storeType);  
   emailStore.connect(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 = "mail.javatpoint.com";//change accordingly  
  String mailStoreType = "pop3";  
  String username= "dwivedicuberdairy.com";  
  String password= "xxxxx";//change accordingly  
  
  receiveEmail(host, mailStoreType, username, password);  
    
 }  
}     


No comments:

Post a Comment