JavaMail Getting Structure of Email

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
LVL 16
suprapto45Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mayank SAssociate Director - Product EngineeringCommented:
You can use the mail API itself.
0
Mayank SAssociate Director - Product EngineeringCommented:
Do you mean you are debugging in Eclipse and you want to see it in a watch?
0
suprapto45Author 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
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Mayank SAssociate Director - Product EngineeringCommented:
Are you able to use the getAllHeaders () method to see the headers?
0
javaoptimizerCommented:
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();
            }
        }
    }
   
}

0
suprapto45Author 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


0
suprapto45Author 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
0
Mayank SAssociate Director - Product EngineeringCommented:
Try getAllHeaders () and getContent (), hopefully together they should give you everything.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
suprapto45Author Commented:
Thanks

Will try it out guys :)

David
0
suprapto45Author Commented:
Thx Guys

David
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.