Handle EML(Electronic mail) file with Java Mail

This Example shows you how to read any eml(Electronic mail) file using javamail api. eml is a simple email, all eml are stored in Folder objects. folders can contain folders or messages.

Handle EML(Electronic mail) file with Java Mail

Handle EML(Electronic mail) file with Java Mail

     

This Example shows you how to read any eml(Electronic mail) file using javamail api. eml is a simple email, all eml are stored in Folder objects. folders can contain folders or messages. The Folder class declares methods that fetch, append, copy and delete messages.
EML are saved email messages from Outlook or other Email application. These files having extension .eml .

EML.java


import java.io.*;
import java.util.*;

import javax.mail.*;
import javax.mail.internet.*;

public class EML {

  public static void main(String args[]) throws Exception {

  String host = "192.168.10.205";
  String from = "test@localhost";
  String to = "test@localhost";

  Properties props = System.getProperties();
  props.setProperty("mail.smtp.host", host);
  props.put("mail.transport.protocol""smtp");

  Session mailSession = Session.getDefaultInstance(props, null);

  File emlFile = new File("message.eml");
  InputStream source = new FileInputStream(emlFile);

  MimeMessage message = new MimeMessage(mailSession, source);

  System.out.println("Subject : " + message.getSubject());
  System.out.println("From : " + message.getFrom()[0]);
  System.out.println("--------------");
  System.out.println("Body : " + message.getContent());
  }
}

Output:

------------ Message ------------
Return-Path: <test@localhost>
Delivered-To: test@localhost
Received: from 192.168.10.111 ([192.168.10.111])
  by roseindi-iyo058 (JAMES SMTP Server 2.3.1with SMTP ID 18
  for <test@localhost>;
  Wed, 16 Jul 2008 14:33:32 +0000 (GMT)
Date: Wed, 16 Jul 2008 14:33:32 +0000 (GMT)
From: test@localhost
To: test@localhost
Message-ID: <6794265.0.1216195417823.JavaMail.komal@komal>
Subject: hi..!
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Download code