We help IT Professionals succeed at work.

JavaMail Getting Structure of Email

suprapto45
suprapto45 asked
on
Medium Priority
398 Views
Last Modified: 2013-11-24
Hi Guys,

As we are debugging our email verification using JavaMail, can anyone tell me how to get the *complete* structure of the email including the header, body and etc of the email

Thanks
David
Comment
Watch Question

Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
You can use the mail API itself.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Do you mean you are debugging in Eclipse and you want to see it in a watch?

Author

Commented:
Not really mayank,

The problem is that it seems like there is few client that uses POP3 cannot receive the mail while other can. Thus, I would like to see what is the complete strcuture of the email such as header, MIME version and etc.

Thanks
David
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Are you able to use the getAllHeaders () method to see the headers?
Hi,
this is an working example, in the working thread class , you can find out how can you retreive header and other info.

public class MyMail {
   
    private String userName = "username";

    private String password = "passwd";

    private String host = "pop3.company.com";

    private static Session session;

    private static Store store;

    MyMail()
    {

    }

    public static void main(String[] args)
    {
        MyMail mailClient = new MyMail();
        mailClient.setupConnection();
        mailClient.newMails();
    }

    private void setupConnection()
    {
        try
        {
            session = Session.getInstance(new Properties());
            store = session.getStore("pop3");
            store.connect(host, userName, password);
        }
        catch (NoSuchProviderException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (MessagingException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public static Session getSession()
    {
        return session;
    }

    public static Store getStore()
    {
        return store;
    }
   
    private void newMails() {
        new Thread(new WorkerThread()).start();
    }    
}

package mail;


import java.util.Date;

import javax.mail.Address;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;

public class WorkerThread implements Runnable {
   
    private Session session;

    private Store store;
   
    private Folder folder;
   
    public void run()
    {

        session = MyMail.getSession();
        store = MyMail.getStore();

        while (true)
        {
            try
            {
                Thread.currentThread().sleep(5000);
                folder = store.getFolder("INBOX");
                folder.open(Folder.READ_WRITE);
                int count = folder.getMessageCount();
                for (int i = 1; i <= count; i++)
                {
                    // Get  a message by its sequence number
                    Message m = folder.getMessage(i);

                    // Get some headers
                    Date date = m.getSentDate();
                    Address[] from = m.getFrom();
                    String subj = m.getSubject();
                    String mimeType = m.getContentType();
                    System.out.println(date + "\t" + from[0] + "\t" + subj
                            + "\t" + mimeType);
                }
            }
            catch (InterruptedException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (MessagingException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
   
}

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Thanks JavaOptimizer

But I need the complete email that is sent from my SMTP Server. Similar as below.

Received: XXXXXX
Received: XXXXXX
Thread-Topic: XXXXXXXXXX
thread-index: AcZLz8MurtiACurkS3KPkEPKwffxJQ==
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.326
Importance: normal
Content-Class: urn:content-classes:message
Received: from XXXXXX
Content-Type: text/html;
      charset="iso-8859-1"
Priority: normal
Content-Transfer-Encoding: 7bit
Message-ID: XXXXXXX
Date: Mon, 20 Mar 2006 11:34:48 +0800 (GMT+08:00)
From: XXXXXX
To: XXXXX
Subject: XXXXX
MIME-Version: 1.0
Return-Path: XXXXX
X-OriginalArrivalTime: 20 Mar 2006 03:38:32.0750 (UTC) FILETIME=[C2F09CE0:01C64BCF]

blah blah blah


Any idea?

David


Author

Commented:
Hi mayank,

>>"Are you able to use the getAllHeaders () method to see the headers?"
I am not sure whether the "complete" structure of email is only consisting header and the body. Is there any method to return the whole structure?

David
Principal Technologist
CERTIFIED EXPERT
Commented:
Try getAllHeaders () and getContent (), hopefully together they should give you everything.

Author

Commented:
Thanks

Will try it out guys :)

David

Author

Commented:
Thx Guys

David
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.