DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Access & Read Embedded Email Attachments from an Existing Message in Android Apps

David Zondray user avatar by
David Zondray
·
May. 21, 14 · · Code Snippet
Like (0)
Save
Tweet
682 Views

Join the DZone community and get the full member experience.

Join For Free

This technical tip shows how developers can read embedded email attachments from an existing email message inside Android Applications. Sometimes, we get emails that have other emails embedded in them as an attachment. These embedded emails are complete messages with their own recipient list, subject, body and attachments. Furthermore, each of these messages can contain embedded messages in turn. Using Aspose.Email, developers can access each embedded message as an individual message. This article shows an example of how, using recursive functionality.

Please perform the following sequence of steps:

  • Create an instance of the MailMessage class.
  • Load the existing email message using the load() method exposed by the MailMessage class and by specifying the MessageFormat.
  • Call the Recursive method by passing the instance of the MailMessage class as parameter.
  • Iterate over the attachment collection of the MailMessage instance.
  • Replace the invalid chars from attachment names and restrict the name to 50 chars.
  • Save the attachment to disk using the save() method exposed by the Attachment class.
//Programming Sample for reading embedded email attachments from an existing email message

public static void ReadEmbeddedAttachments()
{
    // Base folder to load and save files used in this demo
    private static String strBaseFolder = Environment.getExternalStorageDirectory().getPath();
    strBaseFolder = strBaseFolder + "/";

    try
    {
        System.out.print("Reading message with embedded messages....");

        MailMessage message = MailMessage.load(strBaseFolder + "/embedded.msg", MessageFormat.getMsg());
        ParseMessage(message);

        System.out.println("Success");
    }
    catch (Exception ex)
    {
        System.out.println(ex.getMessage());
    }
}

private static void ParseMessage(MailMessage message)
{
    System.out.println("Subject: " + message.getSubject());
    System.out.println("Extracting attachments....");
    for (int i = 0; i < message.getAttachments().size(); i++)
    {
        Attachment att = (Attachment) message.getAttachments().get(i);
        System.out.println("Attachment Name: " + att.getName());

        // Get the name of attachment. If msg subject contains characters like :, /, \ etc., replace with space
        // because windows cannot save files with these characters
        // also save first 50 characters as file name to avoid long file names
        String attFileName = att.getName().replace(".eml", "").replace(":", " ").replace("\\", " ").replace("/", " ").replace("?", "");
        if (attFileName.length() > 50)
        {
            attFileName = attFileName.substring(0, 50);
        }
        String attExt = (att.getName().substring(att.getName().lastIndexOf("."), att.getName().lastIndexOf(".") + 4));

        // Save the attachment to disk
        att.save(strBaseFolder + attFileName + attExt);

        // Check if it is an orphaned text attachment file (ATT00001.txt....) and of type eml
        if ((attExt.equals(".eml")) || (att.getContentType().getMediaType().equals("text/plain") && att.getName().contains(".txt") == true && att.getName().contains("ATT") == true))
        {
            // Try to load this text file in MailMessage
            MailMessage attMsg = MailMessage.load(strBaseFolder + "/" + attFileName + attExt, MessageFormat.getEml());
            // Call the function recursively to parse this message and attachments
            ParseMessage(attMsg);
        }
    }
}
Android (robot) app

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Password Authentication: How to Correctly Do It
  • What Is URL Rewriting? | Java Servlets
  • How to Submit a Post to DZone
  • Don't Underestimate Documentation

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo