Can't connect with JavaMail even though Thunderbird works

Discussion in 'Java' started by Gary, Oct 5, 2010.

  1. Gary

    Gary Guest

    I am trying to connect to our own mail server (Ubuntu, dovecot,
    postfix) and, started by connecting to GMail as a test. I finally got
    that to work and modified the code to work with our own server. Even
    though Thunderbird connects regularly using port 993 with unencrypted
    authentication, when i try to connect with JavaMail there is no
    response from the server and the code just waits for return from the
    store.connect method. As a test, I turned off firewalls at both client
    and server ends which did not help. Here is the core of the code:

    **********************
    Properties props = new Properties();

    session = Session.getDefaultInstance(props, null);

    Store store = session.getStore("imap");

    store.connect(serverName, 993, userName, password);
    ************************

    If I try connecting with imaps, I get a "PKIX path building failed:"
    exception since I have not imported the required certificate. But
    using imaps should not be necessary because Thunderbird does
    unencrypted authentication and does not have a certificate either, so
    it seems like I should be able to get JavaMail to work without a
    certificate the way Thunderbird works without a certificate. I am
    really only interested in using JavaMail for authentication and not
    actually reading email so if I can just make the original connection
    and authenticate username and password, I don't really care about
    anything beyond that - at least, not now.

    Any advice or suggestions on how to proceed or what to look at would
    be appreciated.

    Thank you,
    Gary Whitten
    Gary, Oct 5, 2010
    #1
    1. Advertising

  2. Gary wrote:

    > If I try connecting with imaps, I get a "PKIX path building failed:"
    > exception since I have not imported the required certificate. But
    > using imaps should not be necessary because Thunderbird does
    > unencrypted authentication and does not have a certificate either, so
    > it seems like I should be able to get JavaMail to work without a
    > certificate the way Thunderbird works without a certificate. I am
    > really only interested in using JavaMail for authentication and not
    > actually reading email so if I can just make the original connection
    > and authenticate username and password, I don't really care about
    > anything beyond that - at least, not now.
    >
    > Any advice or suggestions on how to proceed or what to look at would
    > be appreciated.


    To learn what is different, run Wireshark and compare the traces
    you receive when connecting using Thunderbird and your program.


    Regards, Lothar
    --
    Lothar Kimmeringer E-Mail:
    PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

    Always remember: The answer is forty-two, there can only be wrong
    questions!
    Lothar Kimmeringer, Oct 6, 2010
    #2
    1. Advertising

  3. Gary

    Nigel Wade Guest

    On 05/10/10 21:50, Gary wrote:
    > I am trying to connect to our own mail server (Ubuntu, dovecot,
    > postfix) and, started by connecting to GMail as a test. I finally got
    > that to work and modified the code to work with our own server. Even
    > though Thunderbird connects regularly using port 993 with unencrypted
    > authentication,


    I would be surprised if Thunderbird was attempting unencrypted
    connections on port 993, or that your IMAP server was accepting them.
    You would have to manually tweak a few things to get unencrypted IMAP on
    port 993. The default would be SSL on that port as port 993 is defined
    by IANA as the port for imaps, i.e. IMAP using SSL on connect. Are you
    sure you are not referring to the Thunderbird option "Use secure
    authentication", which is something else entirely. If SSL/TLS is
    selected in the connection security drop-box then Thunderbird is using SSL.

    If you want to use unencrypted IMAP the usual port is 143. That should
    be the default in JavaMail if you don't specify the server port.

    > when i try to connect with JavaMail there is no
    > response from the server and the code just waits for return from the
    > store.connect method. As a test, I turned off firewalls at both client
    > and server ends which did not help. Here is the core of the code:
    >
    > **********************
    > Properties props = new Properties();
    >
    > session = Session.getDefaultInstance(props, null);
    >
    > Store store = session.getStore("imap");
    >
    > store.connect(serverName, 993, userName, password);
    > ************************
    >
    > If I try connecting with imaps, I get a "PKIX path building failed:"
    > exception since I have not imported the required certificate. But
    > using imaps should not be necessary because Thunderbird does
    > unencrypted authentication and does not have a certificate either, so
    > it seems like I should be able to get JavaMail to work without a
    > certificate the way Thunderbird works without a certificate.


    The client only needs a cert. if the cert. used by the server is signed
    by an unknown CA, usually due to using a self-signed cert. or defining
    your own CA. In that case both Thunderbird and Java would require the CA
    cert in order to verify the server cert. The server cert. is sent by the
    server during the initial SSL handshake.


    --
    Nigel Wade
    Nigel Wade, Oct 6, 2010
    #3
  4. Gary

    Gary Guest

    On Oct 6, 5:32 am, Nigel Wade <> wrote:
    > On 05/10/10 21:50, Gary wrote:
    >
    > > I am trying toconnectto our own mail server (Ubuntu, dovecot,
    > > postfix) and, started by connecting to GMail as a test. I finally got
    > > that to work and modified the code to work with our own server.Even
    > >thoughThunderbirdconnects regularly using port 993 with unencrypted
    > > authentication,

    >
    > I would be surprised ifThunderbirdwas attempting unencrypted
    > connections on port 993, or that your IMAP server was accepting them.
    > You would have to manually tweak a few things to get unencrypted IMAP on
    > port 993. The default would be SSL on that port as port 993 is defined
    > by IANA as the port for imaps, i.e. IMAP using SSL onconnect. Are you
    > sure you are not referring to theThunderbirdoption "Use secure
    > authentication", which is something else entirely. If SSL/TLS is
    > selected in the connection security drop-box thenThunderbirdis using SSL.
    >
    > If you want to use unencrypted IMAP the usual port is 143. That should
    > be the default inJavaMailif you don't specify the server port.
    >
    >
    >
    > > when i try toconnectwithJavaMailthere is no
    > > response from the server and the code just waits for return from the
    > > store.connectmethod. As a test, I turned off firewalls at both client
    > > and server ends which did not help. Here is the core of the code:

    >
    > > **********************
    > > Properties props = new Properties();

    >
    > > session = Session.getDefaultInstance(props, null);

    >
    > > Store store = session.getStore("imap");

    >
    > > store.connect(serverName, 993, userName, password);
    > > ************************

    >
    > > If I try connecting with imaps, I get a "PKIX path building failed:"
    > > exception since I have not imported the required certificate. But
    > > using imaps should not be necessary becauseThunderbirddoes
    > > unencrypted authentication and does not have a certificate either, so
    > > it seems like I should be able to getJavaMailto work without a
    > > certificate the wayThunderbirdworkswithout a certificate.

    >
    > The client only needs a cert. if the cert. used by the server is signed
    > by an unknown CA, usually due to using a self-signed cert. or defining
    > your own CA. In that case bothThunderbirdand Java would require the CA
    > cert in order to verify the server cert. The server cert. is sent by the
    > server during the initial SSL handshake.
    >
    > --
    > Nigel Wade


    Thank you both for the replies.

    Nigel, I think you have cleared up some confusion for me. Yes, you are
    right, I WAS assuming that "Use secure authentication" meant that the
    authentication was not using SSL (which I always thought was a little
    weird). OK - so now I am down to working with the cert. although I
    still don't understand the difference between how Thunderbird handles
    the cert (I don't have to do anything) and how JavaMail handles is (I
    do have to do something), but that is relatively minor. I think I have
    what I need to get this working.

    Thanks,
    Gary
    Gary, Oct 7, 2010
    #4
  5. On Tue, 05 Oct 2010 13:50:37 -0700, Gary wrote:

    > I am trying to connect to our own mail server (Ubuntu, dovecot, postfix)
    > and, started by connecting to GMail as a test. I finally got that to
    > work and modified the code to work with our own server. Even though
    > Thunderbird connects regularly using port 993 with unencrypted
    > authentication, when i try to connect with JavaMail there is no response
    > from the server and the code just waits for return from the
    > store.connect method. As a test, I turned off firewalls at both client
    > and server ends which did not help. Here is the core of the code:
    >

    I'm using it to retrieve mail for POP3 from Dovecot and to deliver mail
    to Postfix via SMTP with no problems at all. The Javamail client runs on
    the same box as the mail system but is using hostname:port rather than
    localhost to make the connection. Here is the guts of the code I use to
    make the POP connection:

    if (disableTOP)
    props.setProperty("mail.pop3.disabletop", "true");

    session = Session.getDefaultInstance(props);
    messageSource = protocol + ":" + host + "," + user + "," +
    password;
    url = new URLName(messageSource);
    store = session.getStore(url);
    store.connect(host, user, password);
    folder = store.getDefaultFolder();

    Needless to say this has been stripped back to the bare essentials: in
    the interests of emphasising the steps needed to get to the point where
    messages can be read I've left out try/catch blocks and error handling.
    I do not set any other POP-related properties.

    I found out all I needed to know by reading the Javamail documents "API
    design specification" and "API documentation" and using the example code
    in the API design specification.


    --
    martin@ | Martin Gregorie
    gregorie. | Essex, UK
    org |
    Martin Gregorie, Oct 8, 2010
    #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. =?Utf-8?B?ZGh1cndpdHo=?=
    Replies:
    1
    Views:
    456
    Teemu Keiski
    Apr 10, 2004
  2. craig dicker
    Replies:
    9
    Views:
    675
    Juan T. Llibre
    Jul 7, 2005
  3. Paul
    Replies:
    1
    Views:
    415
    Adam Maass
    Aug 14, 2003
  4. Phi!
    Replies:
    1
    Views:
    178
  5. Gene Wirchenko
    Replies:
    42
    Views:
    1,436
    Kev Warren
    May 21, 2012
Loading...

Share This Page