728x90 AdSpace

Sunday, 8 September 2013

Email Receiver with Attachment

In this tutorials we are going to see how to receive the email with attachment using java program.I used gmail SMTP server for receive email, you can use any SMTP server(provided by your host provider).The javax.mail.*; package used for receiving email, for that add latest version of mail.jar into your build path, I used javamail-1.4.7 version.Follow the simple steps for create simple email application for receiving email with attachment using java
Create Java project called EmailReceiver in your eclipse IDE and then create package called com.javatutorialscorner.email.receiver under EmailReceiver project then create Java class called EmailReceiver .java under com.javatutorialscorner.email.receiver package.
EmailReceiver .java
package com.javatutorialscorner.email.receiver;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;

import com.sun.mail.pop3.POP3Store;

public class EmailReceiver {

* @param args
public static void main(String[] args) {
// TODO Auto-generated method stub

String host = "smtp.gmail.com";
String mailStoreType = "pop3";
String username = "yourmailid@gmail.com";
String password = "yourpassword";

try {
Properties receiverConfig = new Properties();
receiverConfig.put("mail.pop3.host", host);
Session session = Session.getDefaultInstance(receiverConfig);

POP3Store pop3Store = (POP3Store) session.getStore(mailStoreType);
pop3Store.connect(username, password);

Folder inbox = pop3Store.getFolder("INBOX");

Message[] messages = inbox.getMessages();
for (int i = 0; i < messages.length; i++) {
Multipart multipart = (Multipart) messages[i].getContent();

for (int j = 0; j < multipart.getCount(); j++) {
BodyPart body = multipart.getBodyPart(j);
InputStream messageStream = body.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(messageStream));

while (br.ready()) {


} catch (NoSuchProviderException e) {
} catch (MessagingException e) {
} catch (IOException e) {


In this example POP3 protocol is used to receive email by java program.The Property is used to configure the POP3 configuration, The smtp.gmail.com is used as POP3 server.The session object contain information like host,port,username,etc..,The getDefaultInstance() method used to get session object.The POP3Store is used to connect POP3 server and The Folder class used to get INBOX folder .The getMessages() method used to get all message from inbox .The getContent() method is used to get Multipart content , multipart message converted as input stream and print it in console.

Now you can run the application and see the email message in your console.

Note : its shows output as HTML text String
Annamalai Thangaraj

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments


Post a Comment

Item Reviewed: Email Receiver with Attachment Rating: 5 Reviewed By: Annamalai Thangaraj