Receiving email with attachment in Java

Receiving email with attachment in Java

In this guide we will discuss about Receiving email with attachment in Java. As we receive the email, we can receive the attachment also by using Multipart and BodyPart classes found in JavaMail API.

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 in Java

import java.util.*;  
import javax.mail.*;  
import javax.mail.internet.*;  
import javax.activation.*;  
import java.io.*;  
  
class ReadAttachment{  
  public static void main(String [] args)throws Exception{  
     
   String host="mail.adglob.in";  
   final String user="[email protected]";  
   final String password="xxxxx";//change accordingly  
  
   Properties properties = System.getProperties();  
   properties.setProperty("mail.smtp.host",host );  
   properties.put("mail.smtp.auth", "true");  
  
   Session session = Session.getDefaultInstance(properties,  
    new javax.mail.Authenticator() {  
    protected PasswordAuthentication getPasswordAuthentication() {  
     return new PasswordAuthentication(user,password);  
    }  
   });  
        
     Store store = session.getStore("pop3");  
     store.connect(host,user,password);  
  
     Folder folder = store.getFolder("inbox");  
     folder.open(Folder.READ_WRITE);  
  
     Message[] message = folder.getMessages();  
  
  
  for (int a = 0; a < message.length; a++) {  
    System.out.println("-------------" + (a + 1) + "-----------");  
    System.out.println(message[a].getSentDate());  
  
    Multipart multipart = (Multipart) message[a].getContent();  
   
    for (int i = 0; i < multipart.getCount(); i++) {  
     BodyPart bodyPart = multipart.getBodyPart(i);  
     InputStream stream = bodyPart.getInputStream();  
     BufferedReader br = new BufferedReader(new InputStreamReader(stream));  
  
      while (br.ready()) {  
       System.out.println(br.readLine());  
      }  
     System.out.println();  
    }  
   System.out.println();  
  }  
  
  folder.close(true);  
  store.close();  
  }  
}  
Load the jar filec:\> set classpath=mail.jar;activation.jar;.;
compile the source filec:\> javac ReadAttachment.java
run byc:\> java ReadAttachment

Next Topic : Click Here

Leave a Reply