JavaMail API Receiving email with attachment

As we receive the email, we can receive the attachment also by using Multipart and BodyPart classes.
For better understanding of this example, learn the steps of sending email using JavaMail API first.

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

Example of receiving email with attachment using JavaMail API
import java.util.*;  
import javax.mail.*;  
import javax.mail.internet.*;  
import javax.activation.*;  
import java.io.*; 
public class ReadMailAttachment{  
  
 public static void receiveEmail(String pop3Host, String storeType,  
  final String user, final String password) {  
  try {  
   //1) get the session object  
   Properties properties = new Properties();  
   properties.put("mail.store.protocol", "imaps");  
   //Session emailSession = Session.getDefaultInstance(properties);  
    Session emailSession = Session.getDefaultInstance(properties,
   new javax.mail.Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
     return new PasswordAuthentication(user,password);
    }
   }); 
   //2) create the POP3 store object and connect with the pop server  
   Store emailStore = emailSession.getStore("imaps");
 emailStore.connect("imap.gmail.com","dineshonjava@gmail.com", password);
    
  
   //3) create the folder object and open it  
   Folder emailFolder = emailStore.getFolder("INBOX");  
   emailFolder.open(Folder.READ_WRITE);  
 // folder.open(Folder.READ_WRITE);  
   //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());  
 System.out.println("Sent Date: " +message.getSentDate()); 
     
  Multipart multipart = (Multipart) message.getContent();  
   
    for (int j = 0; j < multipart.getCount(); j++) {  
     BodyPart bodyPart = multipart.getBodyPart(j);  
     InputStream stream = bodyPart.getInputStream();  
     BufferedReader br = new BufferedReader(new InputStreamReader(stream));  
  
      while (br.ready()) {  
       System.out.println(br.readLine());  
      }  
     System.out.println();  
    }  
 }
   //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 = "smtp.gmail.com";//change accordingly  
  String mailStoreType = "pop3";  
  final String username= "dineshonjava@gmail.com";  
  final String password= "******";//change accordingly  
  
  receiveEmail(host, mailStoreType, username, password);  
  
 }  
}  





<<Previous <<   || Index ||   >>Next >>









No comments:

Post a Comment