How can I read Eml mail message with attachment


I need example to read eml mail message which internally has attachment
Requirement is to remove the attachment as well

Hi Sachin,

Thanks for using Aspose.Email for Java.

Aspose.Email for Java supports rich features to handle mail messages and its relevant data.

Following sample reads an email, accesses its attachments, removes them and then also shows how to add new attachment. You can read more about this here . Please feel free to write us if you have any other queries.

public static void main(String[] args)
// Base folder for reading and writing files
String strBaseFolder = “D:\Data\Aspose\resources\”;

//Initialize and Load an existing EML file by specifying the MessageFormat
MailMessage message = MailMessage.load(strBaseFolder + “AnEmail.eml”, MessageFormat.getEml());

//Initialize AttachmentCollection object with MailMessage Attachments
AttachmentCollection attachments = message.getAttachments();

System.out.println("Attachment Count: " + attachments.size());

//Check if AttachmentCollection size is greater than 0
if(attachments.size() > 0)
//Remove Attachment from index location 0

System.out.println("Attachment Count: " + attachments.size());


//Add a PDF file as Attachment to the message
message.addAttachment(new Attachment(strBaseFolder + “Blank.PDF”));
System.out.println("Attachment Count: " + attachments.size());

//Save the Email message to disk by specifying the EML MailMessageSaveType + “message.eml”, MailMessageSaveType.getEmlFormat());

I want to get “To” from Eml file, but it does not showing anything.
I am using this code :

MailMessage eml = MailMessage.load("C:\\Users\\DELL\\Desktop\\Export (2).eml");
	System.out.println("TO :"+eml.getTo());

File Attached : Export (2).zip (193.2 KB)
when i open this file in thunderbird, it is showing To field.


We were unable to read TO email address from the file that you provided. We created a new file by using the code snippet below. We were able to read the TO email address from the generated file. We have attached the sample file for your convenience.

MailMessage msg = new MailMessage("", "", "Test Subject", "Test Message Body");
Attachment attachment = new Attachment("TxtAttachmnet.txt");
msg.addAttachment(attachment);"testEml.eml", EmlSaveOptions.getDefaultEml());
MailMessage eml = MailMessage.load("testEml.eml");
System.out.println("TO :" + eml.getTo()); (606 Bytes)
Please test the attached EML file using the latest version of Aspose.Email for Java API. Please share the code that to used to generate Export (2).eml so that we can investigate this further.

I have already shared Export(2).zip eml fileExport (2).zip (193.2 KB).
And I am Using java api.
Please Check Export(2).zip file to extract “To” from this file.


We apologize for the misunderstanding. You can notice in our earlier reply that code was in Java because we have tested the scenario with Java API. We have mistakenly typed .NET instead of Java while suggesting you the API and it has been corrected now as well.

We were unable to read the “To Email Address” from that file you shared and generated a new file to test with the API. The file which we have shared with you in our previous reply was read by the API without any issue using the same code snippet shared earlier.

Furthermore, we have logged an issue as EMAILJAVA-34441 in our issue tracking system for your specific file. We will update you as soon as additional information is available.

The issues you have found earlier (filed as EMAILJAVA-34441) have been fixed in this update. This message was posted using BugNotificationTool from Downloads module by MuzammilKhan