Making JavaMail work

J

jesbox

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", "(e-mail address removed)");
email.addRecipient("jesboxx", "(e-mail address removed)",
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("(e-mail address removed)") !=
null ?
System.getProperty("(e-mail address removed)") : "";
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)
 
N

nogales

                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.
 
L

Lew

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

Use a maximum of four spaces (that's space characters) per indent level.
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.
 
N

Nigel Wade

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.
 
J

jesbox

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

It seems I need an https connection to get to gmail.
 
M

Mike Amling

jesbox said:
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
 
J

jesbox

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, "(e-mail address removed)",
"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)
 
A

Arne Vajhøj

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
 
J

jesbox

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.
 
P

Paul Cager

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, "(e-mail address removed)",
"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?
 
J

jesbox

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.
 
M

Martin Gregorie

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.
 
P

Paul Cager

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.
 
A

Arne Vajhøj

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
 
N

Nigel Wade

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, "(e-mail address removed)",
"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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top