How to Save Email Message to Disk using Java

In this tutorial, we will explore how to save Email message to disk using Java. You will see the code sample and steps to write Email message to disk in Java. The mentioned approach uses simple API calls which can be used in Java environments on Linux, macOS, or Windows operating systems.

Steps to Save Email Message to Disk using Java

  1. Configure your project by adding Aspose.Email JAR file from the Maven Repository
  2. Set up account information like username, password, mailbox URI
  3. Create Email client with IEWSClient class
  4. Get mailbox and iterate through each message
  5. Save each message to the disk as EML or MSG file

You will learn how to connect with a mailbox using the exchange client. In this example, we are creating an email client with IEWSClient Class to access the collection of all messages and saving each of them as a separate file. This simple process lets you save email to disk in Java as EML or MSG files. It is quick and easy implementation of how to write email message to disk in Java.

Code to Write Email Message to Disk in Java

import com.aspose.email.EWSClient;
import com.aspose.email.ExchangeMailboxInfo;
import com.aspose.email.ExchangeMessageInfo;
import com.aspose.email.ExchangeMessageInfoCollection;
import com.aspose.email.IEWSClient;
import com.aspose.email.License;
import com.aspose.email.system.NetworkCredential;
public class SaveEmailToDisk {
public static void main(String[] args) throws Exception { // main method for saving Email to disk in Java
// Set the license before writing email messages to disk
License AsposeSaveEmailinJavaLicense = new License();
AsposeSaveEmailinJavaLicense.setLicense("License.lic");
// Setup account information
String MailBoxUri = "https://outlook.office365.com/ews/exchange.asmx";
String Domain = "";
String Username = "username@outlook.com";
String Password = "userpassword";
NetworkCredential CredentiaDetails =
new NetworkCredential(Username, Password, Domain);
// Create email client
IEWSClient EmailClient = EWSClient.getEWSClient(MailBoxUri, CredentiaDetails);
try
{
// Get mail box
ExchangeMailboxInfo MailBoxInfo = EmailClient.getMailboxInfo();
// Get the list of all messages from your mailbox
ExchangeMessageInfoCollection MessagesCollection =
EmailClient.listMessages(EmailClient.getMailboxInfo().getInboxUri());
// Loop through all the messages with for loop
int MessageCount = 1;
for (ExchangeMessageInfo MessageInfo : MessagesCollection)
{
String strMessageURI = MessageInfo.getUniqueUri();
// Get message details
System.out.println("Subject: " + MessageInfo.getSubject());
System.out.println("From: " + MessageInfo.getFrom().toString());
System.out.println("To: " + MessageInfo.getTo().toString());
System.out.println("Message ID: " + MessageInfo.getMessageId());
System.out.println("Unique URI: " + MessageInfo.getUniqueUri());
// Save each message to the disk
EmailClient.saveMessage(strMessageURI, "Messsage_" + MessageCount + ".eml");
EmailClient.saveMessage(strMessageURI, "Messsage_" + MessageCount + ".msg");
MessageCount++;
}
}
catch (Exception ex)
{
System.out.println("Error: " + ex.getMessage());
}
}
}

In the previous topic, we focused on sending email examples and learned how to send Email using Java. This topic covers how using Java save Email Message to disk with simple steps.

 English