Problem with reading the contents, from and to adresses while using javamail pop3

Discussion in 'Java' started by Nikhil, Apr 15, 2007.

  1. Nikhil

    Nikhil Guest

    Hey,
    I'm downloading mail from a google account using pop3.
    The dowloading is going well(at least for some messages).
    When I display the contents of the message or the sender and receiver
    addresses it shows some wierd thing that at least I cannot
    understand....

    Following is my program:


    import java.util.*;
    import java.io.*;
    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.activation.*;
    import com.sun.mail.pop3.POP3Message;


    public class PopTest2
    {

    private static final String SMTP_HOST_NAME = "smtp.gmail.com";
    private static final String SMTP_PORT = "465";
    private static final String emailMsgTxt = "Test Message
    Contents";
    private static final String emailSubjectTxt = "A test from
    gmail";
    private static final String emailFromAddress =
    "";
    private static final String SSL_FACTORY =
    "javax.net.ssl.SSLSocketFactory";
    //private static final String[] sendTo = { ""};


    public static void main(String argv[])
    {
    AccountAuthentication auth = new AccountAuthentication();

    String host = "pop.gmail.com";
    String user = "myemailid";
    String password = "mypassword";
    String port = "995";

    Properties props = new Properties();

    //props.put("mail.debug", "true");
    props.put("mail.pop3.host", host);
    props.put("mail.pop3.user", user);
    props.put("mail.pop3.port", port);
    props.put("mail.pop3.starttls.enable","true");
    props.put("mail.pop3.auth", "true");

    props.put("mail.pop3.socketFactory.port", port);
    props.put("mail.pop3.socketFactory.class",
    "javax.net.ssl.SSLSocketFactory");
    props.put("mail.pop3.socketFactory.fallback", "false");

    Session session = Session.getDefaultInstance(props, auth);

    try
    {
    Store store = session.getStore("pop3");

    try
    {
    store.connect(host, user, password);

    }
    catch (MessagingException ex)
    {
    ex.printStackTrace();
    }

    try
    {
    if(store.isConnected())
    {

    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_WRITE);

    Message[] msg = folder.getMessages();
    //Calendar c = new Calendar();

    int len = folder.getMessageCount();
    Date d=new Date();
    Date msgDate;

    System.out.println("Message Count: " + len);
    Part p;

    for(int i = 0; i < len; i++)
    {
    Flags msgFlags = msg.getFlags();
    //msgDate=msg.getReceivedDate();
    Flags.Flag[] sf = msgFlags.getSystemFlags();
    {
    p=msg;
    String subject = msg.getSubject();
    System.out.println("\nSEEN message");
    System.out.println("\nSubject[" + i +
    "]:---------------> " + subject);
    System.out.println("\nFrom-------------------
    >"+msg.getFrom());

    System.out.println("\nTo--------------------->"+
    (msg.getRecipients(Message.RecipientType.TO)).toString());
    //System.out.println("From soft------------->" +
    "");
    System.out.println("Content:----------------->
    \n"+msg.getContent());
    }

    }
    }
    else
    {
    System.out.println("Unable to open the Inbox.");
    }

    }
    catch (MessagingException ex)
    {
    ex.printStackTrace();
    }
    catch(IOException ex)
    {
    ex.printStackTrace();
    }


    }
    catch (NoSuchProviderException ex)
    {
    ex.printStackTrace();
    }
    }
    }


    And following is the output :(of only one of the many mails of the
    account)

    SEEN message

    Subject[9]:---------------> Re: Student project

    From------------------->[Ljavax.mail.internet.InternetAddress;@13582d

    To--------------------->[Ljavax.mail.internet.InternetAddress;@21b6d
    Content:----------------->
    javax.mail.internet.MimeMultipart@56a499


    Now I can assure you that the message does not contain
    "javax.mail.internet.MimeMultipart@56a499".

    How do I get the mail ids of sender and receiver and the content?
    Thanx for having a look....
    Nikhil, Apr 15, 2007
    #1
    1. Advertising

  2. Nikhil

    Lew Guest

    Re: Problem with reading the contents, from and to adresses whileusing javamail pop3

    Nikhil wrote:
    > public class PopTest2
    > {
    >
    > private static final String SMTP_HOST_NAME = "smtp.gmail.com";


    Your post would be much easier to follow if you did not embed TAB characters
    in it.

    ....
    > Message[] msg = folder.getMessages();

    ....
    > System.out.println("\nFrom-------------------> "+msg.getFrom());

    ....
    > And following is the output :(of only one of the many mails of the
    > account)

    ,,,
    > From------------------->[Ljavax.mail.internet.InternetAddress;@13582d


    Well, of course we don't have the definition of Message in front of us, since
    you didn't post an SSCCE, but dollars to doughnuts getFrom() doesn't return a
    String. In fact, I will bet good money it returns a
    javax.mail.internet.InternetAddress.

    What is the type of Message.getFrom()?
    (Hint: javax.mail.internet.InternetAddress)

    What is the default output from toString() if one doesn't override it?
    (Hint:
    <http://java.sun.com/javase/6/docs/api/java/lang/Object.html#toString()>

    --
    Lew
    Lew, Apr 15, 2007
    #2
    1. Advertising

  3. Nikhil

    Nikhil Guest

    On Apr 15, 6:30 pm, Lew <> wrote:
    > Nikhil wrote:
    > > public class PopTest2
    > > {

    >
    > > private static final String SMTP_HOST_NAME = "smtp.gmail.com";

    >
    > Your post would be much easier to follow if you did not embed TAB characters
    > in it.
    >
    > ...
    >
    > > Message[] msg = folder.getMessages();

    > ...
    > > System.out.println("\nFrom-------------------> "+msg.getFrom());

    > ...
    > > And following is the output :(of only one of the many mails of the
    > > account)

    > ,,,
    > > From------------------->[Ljavax.mail.internet.InternetAddress;@13582d

    >
    > Well, of course we don't have the definition of Message in front of us, since
    > you didn't post an SSCCE, but dollars to doughnuts getFrom() doesn't return a
    > String. In fact, I will bet good money it returns a
    > javax.mail.internet.InternetAddress.
    >
    > What is the type of Message.getFrom()?
    > (Hint: javax.mail.internet.InternetAddress)
    >
    > What is the default output from toString() if one doesn't override it?
    > (Hint:
    > <http://java.sun.com/javase/6/docs/api/java/lang/Object.html#toString()>
    >
    > --
    > Lew


    Thanks for replying....

    Actually the getFrom method returns an array of type
    javax.mail.internet.InternetAddress .
    So now when i wrote the following statement, it printed the correct
    address.

    (msg.getRecipients(Message.RecipientType.TO))[0].toString()


    But still I am not able to read the contents of the Messages whose
    content type is MimeMultipart.
    So how do I read contents of these messages?
    Nikhil, Apr 15, 2007
    #3
  4. Nikhil

    Lars Enderin Guest

    Re: Problem with reading the contents, from and to adresses whileusing javamail pop3

    Nikhil skrev:
    > On Apr 15, 6:30 pm, Lew <> wrote:
    >> Nikhil wrote:
    >>> public class PopTest2
    >>> {
    >>> private static final String SMTP_HOST_NAME = "smtp.gmail.com";

    >> Your post would be much easier to follow if you did not embed TAB characters
    >> in it.
    >>
    >> ...
    >>
    >>> Message[] msg = folder.getMessages();

    >> ...
    >>> System.out.println("\nFrom-------------------> "+msg.getFrom());

    >> ...
    >>> And following is the output :(of only one of the many mails of the
    >>> account)

    >> ,,,
    >>> From------------------->[Ljavax.mail.internet.InternetAddress;@13582d

    >> Well, of course we don't have the definition of Message in front of us, since
    >> you didn't post an SSCCE, but dollars to doughnuts getFrom() doesn't return a
    >> String. In fact, I will bet good money it returns a
    >> javax.mail.internet.InternetAddress.
    >>
    >> What is the type of Message.getFrom()?
    >> (Hint: javax.mail.internet.InternetAddress)
    >>
    >> What is the default output from toString() if one doesn't override it?
    >> (Hint:
    >> <http://java.sun.com/javase/6/docs/api/java/lang/Object.html#toString()>
    >>
    >> --
    >> Lew

    >
    > Thanks for replying....
    >
    > Actually the getFrom method returns an array of type
    > javax.mail.internet.InternetAddress .
    > So now when i wrote the following statement, it printed the correct
    > address.
    >
    > (msg.getRecipients(Message.RecipientType.TO))[0].toString()
    >
    >
    > But still I am not able to read the contents of the Messages whose
    > content type is MimeMultipart.
    > So how do I read contents of these messages?
    >

    You should be able to figure that out from the API documentation. Note
    that different MessageParts may have different representations of the
    actual message. One part may be text/html (or application/html), another
    may be text/plain. It's all spelled out in the headers and the class
    hierarchy. Note also that text may be encoded in, e g, Quoted Printable
    or Base64. The header fields may also be encoded, if they contain
    non-ascii characters.
    Lars Enderin, Apr 15, 2007
    #4
  5. Re: Problem with reading the contents, from and to adresses whileusing javamail pop3

    Nikhil wrote:
    >
    > But still I am not able to read the contents of the Messages whose
    > content type is MimeMultipart.
    > So how do I read contents of these messages?
    >

    Look at the example code in the JavaMail API Design Specification. While
    you're at it, download the API documentation as well: the
    Message.getFrom() method returns javax.mail.Address objects, which are
    not the same as javax.mail.internet.InternetAddress object, which you'd
    know if you'd looked at the API documentation.


    --
    martin@ | Martin Gregorie
    gregorie. | Essex, UK
    org |
    Martin Gregorie, Apr 15, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel Albisser
    Replies:
    1
    Views:
    1,054
    GaryM
    Apr 7, 2004
  2. Charter
    Replies:
    2
    Views:
    37,245
    Roedy Green
    Sep 20, 2005
  3. Adam Messer
    Replies:
    0
    Views:
    1,468
    Adam Messer
    Sep 20, 2005
  4. Sebastian Becker

    Reading memory-adresses from files

    Sebastian Becker, Jan 11, 2005, in forum: C++
    Replies:
    4
    Views:
    833
    Sebastian Becker
    Jan 12, 2005
  5. moni
    Replies:
    7
    Views:
    1,683
    Greg R. Broderick
    Oct 6, 2007
Loading...

Share This Page