?
Solved

JavaMail Getting Structure of Email

Posted on 2006-03-19
10
Medium Priority
?
377 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
0
Comment
Question by:suprapto45
  • 5
  • 4
10 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 16233419
You can use the mail API itself.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16233424
Do you mean you are debugging in Eclipse and you want to see it in a watch?
0
 
LVL 16

Author Comment

by:suprapto45
ID: 16233466
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Mayank S
ID: 16233650
Are you able to use the getAllHeaders () method to see the headers?
0
 
LVL 3

Assisted Solution

by:javaoptimizer
javaoptimizer earned 600 total points
ID: 16233733
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
 
LVL 16

Author Comment

by:suprapto45
ID: 16233827
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
 
LVL 16

Author Comment

by:suprapto45
ID: 16233834
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
 
LVL 30

Accepted Solution

by:
Mayank S earned 600 total points
ID: 16233868
Try getAllHeaders () and getContent (), hopefully together they should give you everything.
0
 
LVL 16

Author Comment

by:suprapto45
ID: 16242915
Thanks

Will try it out guys :)

David
0
 
LVL 16

Author Comment

by:suprapto45
ID: 16244472
Thx Guys

David
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month14 days, 10 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question