Making JavaMail work

Discussion in 'Java' started by jesbox, Jun 8, 2010.

  1. jesbox

    jesbox Guest

    Dear all,

    I am trying to get JavaMail to work and have picked a library "Very
    Simple Java Mail" at http://code.google.com/p/vesijama/ that seems to
    provide a nice degree of abstraction. I would be grateful for advice
    on how to find out what goes wrong, e.g. why my program does not reach
    Internet. I run Java 1.6 on OSX 10.5.8 and Tomcat v6.0.

    The program is basically the sample program for the wrapper library at
    http://code.google.com/p/vesijama/source/browse/trunk/src/MailTest.java
    :
    public class TestMail {

    static { // normally you would do this in the log4j.xml
    final Logger rootLogger = Logger.getRootLogger();
    rootLogger.addAppender(new ConsoleAppender(new SimpleLayout()));
    rootLogger.setLevel(Level.INFO);
    }

    public static void main(String[] args) {
    testMail();
    }
    public static void testMail() throws MailException,
    UnknownHostException {
    final Email email = new Email();
    email.setFromAddress("myApplication", "");
    email.addRecipient("jesboxx", "",
    RecipientType.TO);
    email.setText("We should meet up!");
    email.setTextHTML("<b>We should meet up!</b>");
    email.setSubject("hey");
    sendMail(email);
    }

    private static void sendMail(final Email email) {
    final String host = System.getProperty("smtp.gmail.com") !=
    null ?
    System.getProperty("smtp.gmail.com") : "";
    final String username = System.getProperty("") !=
    null ?
    System.getProperty("") : "";
    final String password = System.getProperty("a_password") != null ?
    System.getProperty("a_password") : "";
    new Mailer(host, 25, username, password).sendMail(email);
    }
    }

    The stacktrace is then becomes :
    ERROR - Could not connect to SMTP host: localhost, port: 25
    javax.mail.MessagingException: Could not connect to SMTP host:
    localhost, port: 25;
    nested exception is:
    java.net.ConnectException: Connection refused
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:
    1545)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:
    453)
    at javax.mail.Service.connect(Service.java:313)
    at javax.mail.Service.connect(Service.java:172)
    at javax.mail.Service.connect(Service.java:121)
    at javax.mail.Transport.send0(Transport.java:190)
    at javax.mail.Transport.send(Transport.java:120)
    at org.codemonkey.vesijama.Mailer.sendMail(Mailer.java:174)
    at TestMail.sendMail(TestMail.java:56)
    at TestMail.testMail(TestMail.java:47)
    at TestMail.main(TestMail.java:33)
    Caused by: java.net.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:
    195)
     
    jesbox, Jun 8, 2010
    #1
    1. Advertising

  2. jesbox

    nogales Guest

    On 8 jun, 11:08, jesbox <> wrote:

    >                 new Mailer(host, 25, username, password).sendMail(email);


    > The stacktrace is then becomes :
    > ERROR - Could not connect to SMTP host: localhost, port: 25
    > javax.mail.MessagingException: Could not connect to SMTP host:
    > localhost, port: 25;
    >   nested exception is:
    >         java.net.ConnectException: Connection refused
    >         at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:
    > 1545)
    >         at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:
    > 453)
    >         at javax.mail.Service.connect(Service.java:313)
    >         at javax.mail.Service.connect(Service.java:172)
    >         at javax.mail.Service.connect(Service.java:121)
    >         at javax.mail.Transport.send0(Transport.java:190)
    >         at javax.mail.Transport.send(Transport.java:120)
    >         at org.codemonkey.vesijama.Mailer.sendMail(Mailer.java:174)
    >         at TestMail.sendMail(TestMail.java:56)
    >         at TestMail.testMail(TestMail.java:47)
    >         at TestMail.main(TestMail.java:33)
    > Caused by: java.net.ConnectException: Connection refused
    >         at java.net.PlainSocketImpl.socketConnect(Native Method)
    >         at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    >         at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:
    > 195)


    It seems that the variable 'host' is an empty String.
     
    nogales, Jun 8, 2010
    #2
    1. Advertising

  3. jesbox

    Lew Guest

    jesbox wrote:
    >> new Mailer(host, 25, username, password).sendMail(email);


    Dude, do not use TAB characters to indent Usenet posts.

    Use a maximum of four spaces (that's space characters) per indent level.

    >> The stacktrace is then becomes :
    >> ERROR - Could not connect to SMTP host: localhost, port: 25
    >> javax.mail.MessagingException: Could not connect to SMTP host:
    >> localhost, port: 25;


    nogales wrote:
    > It seems that the variable 'host' is an empty String.


    Is that why it's trying to mail via localhost?

    Which'd be fine if localhost were running a mail service on port 25.

    One wonders why in
    > final String host = System.getProperty("smtp.gmail.com") !=
    > null ?
    >
    > System.getProperty("smtp.gmail.com") : "";


    the lack of the "smtp.gmail.com" property is not logged, nor even a logger
    defined for the class, especially after all that superfluous effort to
    initialize the root logger.

    Don't allow preconditions (or other invariants) to fail silently.

    --
    Lew
     
    Lew, Jun 8, 2010
    #3
  4. On Tue, 08 Jun 2010 03:57:10 -0700, nogales wrote:

    >
    > It seems that the variable 'host' is an empty String.
    >

    Either that or there is no MTA listening on localhost:25


    --
    martin@ | Martin Gregorie
    gregorie. | Essex, UK
    org |
     
    Martin Gregorie, Jun 8, 2010
    #4
  5. jesbox

    Nigel Wade Guest

    On Tue, 08 Jun 2010 02:08:50 -0700, jesbox wrote:

    > Dear all,
    >
    > I am trying to get JavaMail to work and have picked a library "Very
    > Simple Java Mail" at http://code.google.com/p/vesijama/ that seems to
    > provide a nice degree of abstraction. I would be grateful for advice on
    > how to find out what goes wrong, e.g. why my program does not reach
    > Internet. I run Java 1.6 on OSX 10.5.8 and Tomcat v6.0.
    >



    > The stacktrace is then becomes :
    > ERROR - Could not connect to SMTP host: localhost, port: 25
    > javax.mail.MessagingException: Could not connect to SMTP host:
    > localhost, port: 25;
    > nested exception is:
    > java.net.ConnectException: Connection refused at
    > com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:
    > 1545)
    > at com.sun.mail.smtp.SMTPTransport.protocolConnect

    (SMTPTransport.java:

    That results from attempting to connect to an SMTP service running on
    your computer, and there is none running.

    Presumably the lookup for smtp.gmail.com in System properties fails.
    Hardly surprising really, why would it exist in the System properties? So
    host is set to an empty string. Not knowing what the
    org.codemonkey.vesijama.Mailer class does with the "host" argument if
    it's empty, I am guessing that it attempts a connection to localhost.
    Since you don't have a SMTP service running the connection fails with a
    MessagingException and you get a stack trace.

    I think you would have better success if you set the host, username and
    password variables to the values you attempt to lookup in System
    properties.

    --
    Nigel Wade
     
    Nigel Wade, Jun 8, 2010
    #5
  6. jesbox

    jesbox Guest

    Thanks for the wakeup, now I'm heading for the next trouble.

    It seems I need an https connection to get to gmail.
     
    jesbox, Jun 8, 2010
    #6
  7. jesbox

    Mike Amling Guest

    jesbox wrote:
    > private static void sendMail(final Email email) {
    > final String host = System.getProperty("smtp.gmail.com") !=
    > null ?
    > System.getProperty("smtp.gmail.com") : "";
    > ...
    > new Mailer(host, 25, username, password).sendMail(email);
    > }
    > }


    What value did you assign to system property smtp.gmail.com? The
    Mailer seems to be using "localhost". Did you specify
    -Dsmtp.gmail.com=localhost in the java command line? Or did the code
    invoke new Mailer("", ...)?

    >
    > The stacktrace is then becomes :
    > ERROR - Could not connect to SMTP host: localhost, port: 25
    > javax.mail.MessagingException: Could not connect to SMTP host:
    > localhost, port: 25;
    > nested exception is:
    > java.net.ConnectException: Connection refused
    > at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:
    > 1545)
    > at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:
    > 453)
    > at javax.mail.Service.connect(Service.java:313)
    > at javax.mail.Service.connect(Service.java:172)
    > at javax.mail.Service.connect(Service.java:121)
    > at javax.mail.Transport.send0(Transport.java:190)
    > at javax.mail.Transport.send(Transport.java:120)
    > at org.codemonkey.vesijama.Mailer.sendMail(Mailer.java:174)
    > at TestMail.sendMail(TestMail.java:56)
    > at TestMail.testMail(TestMail.java:47)
    > at TestMail.main(TestMail.java:33)
    > Caused by: java.net.ConnectException: Connection refused
    > at java.net.PlainSocketImpl.socketConnect(Native Method)
    > at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    > at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:
    > 195)


    --Mike Amling
     
    Mike Amling, Jun 8, 2010
    #7
  8. jesbox

    jesbox Guest

    Thanks for the tips. I have changed into hard-coded values, i.e.
    System.setProperty("mail.smtps.auth", "true"); // enable https
    new Mailer("smtp.gmail.com", 465, "",
    "a_password").sendMail(email);

    The exception I get is:
    org.codemonkey.vesijama.MailException: Generic error: Could not
    connect to SMTP host: smtp.gmail.com, port: 465, response: -1
    at org.codemonkey.vesijama.Mailer.sendMail(Mailer.java:180)
    at TestMail.sendMail(TestMail.java:57)
    at TestMail.testMail(TestMail.java:52)
    at TestMail.main(TestMail.java:33)
    Caused by: javax.mail.MessagingException: Could not connect to SMTP
    host: smtp.gmail.com, port: 465, response: -1
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:
    1533)
    at
    com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:
    453)
    at javax.mail.Service.connect(Service.java:313)
    at javax.mail.Service.connect(Service.java:172)
     
    jesbox, Jun 8, 2010
    #8
  9. jesbox

    Arne Vajhøj Guest

    On 08-06-2010 18:58, jesbox wrote:
    > Thanks for the tips. I have changed into hard-coded values, i.e.
    > System.setProperty("mail.smtps.auth", "true"); // enable https


    Not so relevant for your problem but you are using
    SMTP over SSL where HTTPS is HTTP over SSL.

    Arne
     
    Arne Vajhøj, Jun 9, 2010
    #9
  10. jesbox

    jesbox Guest

    On Jun 9, 1:30 am, Arne Vajhøj <> wrote:
    > On 08-06-2010 18:58, jesbox wrote:
    >
    > > Thanks for the tips. I have changed into hard-coded values, i.e.
    > >     System.setProperty("mail.smtps.auth", "true"); // enable https

    >
    > Not so relevant for your problem but you are using
    > SMTP over SSL where HTTPS is HTTP over SSL.
    >
    > Arne


    Right, I try to run SMTP over SSL. And, as you suggest, that insight
    does not bring me forward.
    Thanks for the enlightment anyway.
     
    jesbox, Jun 9, 2010
    #10
  11. jesbox

    Paul Cager Guest

    On Jun 8, 11:58 pm, jesbox <> wrote:
    > Thanks for the tips. I have changed into hard-coded values, i.e.
    >    System.setProperty("mail.smtps.auth", "true"); // enable https
    >    new Mailer("smtp.gmail.com", 465, "",
    > "a_password").sendMail(email);


    Sorry if this is a silly question, but are the username and password
    above just dummies because you (wisely) don't want to publish your
    real user/password on usenet? You _are_ passing in your own real Gmail
    user-id and password when you run your program?
     
    Paul Cager, Jun 9, 2010
    #11
  12. jesbox

    jesbox Guest

    On Jun 9, 12:48 pm, Paul Cager <> wrote:
    > On Jun 8, 11:58 pm, jesbox <> wrote:
    >
    > > Thanks for the tips. I have changed into hard-coded values, i.e.
    > >    System.setProperty("mail.smtps.auth", "true"); // enable https
    > >    new Mailer("smtp.gmail.com", 465, "",
    > > "a_password").sendMail(email);

    >
    > Sorry if this is a silly question, but are the username and password
    > above just dummies because you (wisely) don't want to publish your
    > real user/password on usenet? You _are_ passing in your own real Gmail
    > user-id and password when you run your program?


    No, they are dummy values. I did not do that mistake ;-)
    But I could have made it clear by calling it e.g. dummy_password or
    something.
     
    jesbox, Jun 9, 2010
    #12
  13. jesbox

    Roedy Green Guest

    On Tue, 8 Jun 2010 02:08:50 -0700 (PDT), jesbox <>
    wrote, quoted or indirectly quoted someone who said :

    >
    >I am trying to get JavaMail to work and have picked a library "Very
    >Simple Java Mail" at http://code.google.com/p/vesijama/ that seems to


    I used Sun's JavaMail implementation and it works without trouble. See
    http://mindprod.com/products.html#BULK for the source code.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    Have you ever noticed that any computer search in the movies, is always linear, with, for example, candidate fingerprints flashing up on the screen one after another? The public is still under the delusion that electronic files are microscopic filing cabinets made out of tiny wires or magnetic patches inside the computer. Most lay people are surprised that it is easy for a computer to file things simultaneously by a dozen different schemes, and that they can have any report printed in any number of different sorted orders. With physical files, they are limited to one ordering/access.
     
    Roedy Green, Jun 9, 2010
    #13
  14. On Wed, 09 Jun 2010 06:16:10 -0700, Roedy Green wrote:

    > On Tue, 8 Jun 2010 02:08:50 -0700 (PDT), jesbox <>
    > wrote, quoted or indirectly quoted someone who said :
    >
    >
    >>I am trying to get JavaMail to work and have picked a library "Very
    >>Simple Java Mail" at http://code.google.com/p/vesijama/ that seems to

    >
    > I used Sun's JavaMail implementation and it works without trouble. See
    > http://mindprod.com/products.html#BULK for the source code.


    Same here. The examples in Sun's Javamail API Design Specification are
    clear and useful and so are the JavaMail class specification. Read them
    in that order to get a good idea of how it all works. Using JavaMail
    directly doesn't add much complexity over an above the Google wrapper
    classes you're using and you may find you get more help if you use bare
    JavaMail, simply because more of us have used it than that Google
    package.


    --
    martin@ | Martin Gregorie
    gregorie. | Essex, UK
    org |
     
    Martin Gregorie, Jun 9, 2010
    #14
  15. jesbox

    Paul Cager Guest

    On Jun 9, 2:15 pm, jesbox <> wrote:
    > No, they are dummy values. I did not do that mistake ;-)
    > But I could have made it clear by calling it e.g. dummy_password or
    > something.


    Worth asking....

    As others have said, it might be less confusing to use JavaMail
    directly rather than via vesijama. But it could be interesting to use
    a TCP monitor (such as Wireshark, http://www.wireshark.org/download.html)
    to see exactly what is being sent to the mail server. To see it all in
    plain text you'd have to disable SSL, though.
     
    Paul Cager, Jun 9, 2010
    #15
  16. jesbox

    Arne Vajhøj Guest

    On 09-06-2010 04:23, jesbox wrote:
    > On Jun 9, 1:30 am, Arne Vajhøj<> wrote:
    >> On 08-06-2010 18:58, jesbox wrote:
    >>
    >>> Thanks for the tips. I have changed into hard-coded values, i.e.
    >>> System.setProperty("mail.smtps.auth", "true"); // enable https

    >>
    >> Not so relevant for your problem but you are using
    >> SMTP over SSL where HTTPS is HTTP over SSL.

    >
    > Right, I try to run SMTP over SSL. And, as you suggest, that insight
    > does not bring me forward.
    > Thanks for the enlightment anyway.


    I just wanted to avoid future confusion by other readers.

    Arne
     
    Arne Vajhøj, Jun 10, 2010
    #16
  17. jesbox

    Nigel Wade Guest

    On Tue, 08 Jun 2010 15:58:56 -0700, jesbox wrote:

    > Thanks for the tips. I have changed into hard-coded values, i.e.
    > System.setProperty("mail.smtps.auth", "true"); // enable https new
    > Mailer("smtp.gmail.com", 465, "",
    > "a_password").sendMail(email);
    >
    > The exception I get is:
    > org.codemonkey.vesijama.MailException: Generic error: Could not connect
    > to SMTP host: smtp.gmail.com, port: 465, response: -1
    > at org.codemonkey.vesijama.Mailer.sendMail(Mailer.java:180) at
    > TestMail.sendMail(TestMail.java:57) at
    > TestMail.testMail(TestMail.java:52) at TestMail.main(TestMail.java:33)
    > Caused by: javax.mail.MessagingException: Could not connect to SMTP
    > host: smtp.gmail.com, port: 465, response: -1
    > at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:
    > 1533)
    > at
    > com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java: 453)
    > at javax.mail.Service.connect(Service.java:313) at
    > javax.mail.Service.connect(Service.java:172)



    Do you know that smtps works with the vesijama Mailer class?

    In my limited understanding of JavaMail I believe that to use smtps
    requires the use of a different Transport. The contents of the Mailer
    class is a black box. If it doesn't select the correct Transport for the
    smtps protocol then the connection will definitely fail in the SSL
    handshake.

    I would use the bare JavaMail interface rather than some unknown wrapper
    class. At least you can then see exactly what's going on. If you google
    "gmail javamail" you should get some useful info. Very high up on the
    list of results is the JavaMail FAQ, and that has pointers to example
    code, and includes an example of smtps mail submission to Gmail.

    --
    Nigel Wade
     
    Nigel Wade, Jun 10, 2010
    #17
    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. Joseph Millar

    Re: Smtp Server error with JavaMail

    Joseph Millar, Jul 16, 2003, in forum: Java
    Replies:
    0
    Views:
    624
    Joseph Millar
    Jul 16, 2003
  2. GaryM
    Replies:
    8
    Views:
    19,751
    Scott Yanoff
    Jul 21, 2003
  3. Sudsy
    Replies:
    0
    Views:
    451
    Sudsy
    Jul 16, 2003
  4. Replies:
    5
    Views:
    1,881
    Andrew Thompson
    Jan 12, 2005
  5. Michal M
    Replies:
    0
    Views:
    573
    Michal M
    Apr 4, 2005
Loading...

Share This Page