Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: JavaMail - How do I get a specific Header of an Email?

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JavaMail - How do I get a specific Header of an Email?

    Hi to All,
    I'm trying to extract the Message-ID from an Email and I found this code online which gives me the entire header information:

    package bgmailproc;
    import java.util.*;
    import javax.mail.*;
     
    public class msgshow {
        public static void main(String[] args) throws Exception {
            Properties props = System.getProperties();
            props.setProperty("mail.pop3.port", "995");
            Session session = Session.getInstance(props, null);
            Store store = session.getStore("imaps");
     
            store.connect("pop.gmail.com", "myusername", "mypassword");
            Folder inbox = store.getFolder("INBOX");
            inbox.open(Folder.READ_ONLY);
            //System.out.print(inbox.getMessageCount());
     
     
            for (int i = 1;i <= 10; i++){
     
     
                System.out.println(h.getName() + ": " + h.getValue());
                inbox.getMessage(i).getHeader("Message-Id");
     
                Enumeration headers = inbox.getMessage(i).getAllHeaders();
     
                while (headers.hasMoreElements()) {
                    Header h = (Header) headers.nextElement();
                    System.out.println(h.getName() + ":" + h.getValue());
                }
            }
            inbox.close(true);
        }
    }

    Question: How do I get a specific Header of an Email? I tried using this code:
    System.out.println(inbox.getMessage(i).getHeader(" Message-ID"));

    But this gives me an output of:
    [Ljava.lang.String;@1b26af3
    [Ljava.lang.String;@8b819f
    [Ljava.lang.String;@eb017e
    [Ljava.lang.String;@120a47e
    [Ljava.lang.String;@f73c1
    [Ljava.lang.String;@789144
    [Ljava.lang.String;@1893efe
    [Ljava.lang.String;@186c6b2
    [Ljava.lang.String;@15ee671
    [Ljava.lang.String;@16b13c7


    Hope you can help me.
    Thanks in advance.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: JavaMail - How do I get a specific Header of an Email?

    Hello gsanchezbiz,

    Welcome to the Java Programming Forums.
    Sorry for the late reply on this. I have compiled the code and it seems to work.

    Just to clarify, you are looking to extract all of the Message-ID's? For example:

    Message-ID:<CC3DADE7B22D134FB3F84E34B91F84800143A420@dghdc 001.dghmoney.local>

    I've had a play around with it and you should be able to do something like this:

    import java.util.*;
    import javax.mail.*;
     
    public class msgshow {
     
        public static void main(String[] args) throws Exception {
            Properties props = System.getProperties();
            props.setProperty("mail.pop3.port", "995");
            Session session = Session.getInstance(props, null);
            Store store = session.getStore("imaps");
     
            store.connect("pop.gmail.com", "username", "password");
            Folder inbox = store.getFolder("INBOX");
            inbox.open(Folder.READ_ONLY);
            //System.out.print(inbox.getMessageCount()); 
     
            for (int i = 1;i <= 10; i++){
     
                 //System.out.println(h.getName() + ": " + h.getValue());
     
            	inbox.getMessage(i).getHeader("Message-Id"); 
                Enumeration headers = inbox.getMessage(i).getAllHeaders();
     
                while (headers.hasMoreElements()) {
                    Header h = (Header) headers.nextElement();
                    //System.out.println(h.getName() + ":" + h.getValue());
                    //System.out.println("");                
                    String mID = h.getName();                
                    if(mID.contains("Message-ID")){
                    	System.out.println(h.getName() + ":" + h.getValue());
                    }
                }
            }
            inbox.close(true);
        }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JavaMail - How do I get a specific Header of an Email?

    Hi,

    Thanks for the reply.

    Your solution really worked. But, is there a way to prevent looping through all of then elements of Header?
    I'm looking at using getHeader() function.

    inbox.getMessage(i).getHeader("Message-Id");

  4. #4
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JavaMail - How do I get a specific Header of an Email?

    Hi,

    I figured it out how to do it.
    MimeMessage m = (MimeMessage) inbox.getMessage(i);
    System.out.println(m.getMessageID());

    Though using code disregards Message-ID "X-TM-IMSS-Message-ID", it's fine and just what I need.

    Thanks once again.

  5. #5
    Junior Member
    Join Date
    Dec 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JavaMail - How do I get a specific Header of an Email?

    Quote Originally Posted by JavaPF View Post
    Hello gsanchezbiz,

    Welcome to the Java Programming Forums.
    Sorry for the late reply on this. I have compiled the code and it seems to work.

    Just to clarify, you are looking to extract all of the Message-ID's? For example:

    Message-ID:<CC3DADE7B22D134FB3F84E34B91F84800143A420@dghdc 001.dghmoney.local>

    I've had a play around with it and you should be able to do something like this:

    import java.util.*;
    import javax.mail.*;
     
    public class msgshow {
     
        public static void main(String[] args) throws Exception {
            Properties props = System.getProperties();
            props.setProperty("mail.pop3.port", "995");
            Session session = Session.getInstance(props, null);
            Store store = session.getStore("imaps");
     
            store.connect("pop.gmail.com", "username", "password");
            Folder inbox = store.getFolder("INBOX");
            inbox.open(Folder.READ_ONLY);
            //System.out.print(inbox.getMessageCount()); 
     
            for (int i = 1;i <= 10; i++){
     
                 //System.out.println(h.getName() + ": " + h.getValue());
     
            	inbox.getMessage(i).getHeader("Message-Id"); 
                Enumeration headers = inbox.getMessage(i).getAllHeaders();
     
                while (headers.hasMoreElements()) {
                    Header h = (Header) headers.nextElement();
                    //System.out.println(h.getName() + ":" + h.getValue());
                    //System.out.println("");                
                    String mID = h.getName();                
                    if(mID.contains("Message-ID")){
                    	System.out.println(h.getName() + ":" + h.getValue());
                    }
                }
            }
            inbox.close(true);
        }
    }
    Thank you very much for this code, this is exactly what I was looking for... Although, for some reason there were a couple of emails where it couldn't find the Message-ID header even though I manually found it on those .eml files. After playing a bit with it, I noticed that for those emails it detected the header as Message-Id and I have no idea why, because on the file it has a capital D... So I just modified the line like this: if(mID.contains("Message-ID") || mID.contains("Message-Id"))

Similar Threads

  1. How to bind soap header using jax-rpc
    By jadeite100 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 5th, 2010, 02:54 PM
  2. JavaMail and Gmail
    By khushbu in forum Java Servlet
    Replies: 2
    Last Post: May 21st, 2010, 01:11 AM
  3. Problem with JavaMail... HELP! :(
    By danmcm88 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 18th, 2010, 04:11 PM
  4. Javamail
    By johniem in forum Java Theory & Questions
    Replies: 1
    Last Post: February 3rd, 2010, 07:42 AM

Tags for this Thread