Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Making JavaMail work (http://www.velocityreviews.com/forums/t725025-making-javamail-work.html)

jesbox 06-08-2010 09:08 AM

Making JavaMail work
 
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/so.../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", "foo.bar@somemail.com");
email.addRecipient("jesboxx", "jesboxx@gmail.com",
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("myEmail@gmail.com") !=
null ?
System.getProperty("myEmail@gmail.com") : "";
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(SMTPTra nsport.java:
1545)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SM TPTransport.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.jav a: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(PlainSoc ketImpl.java:
195)


nogales 06-08-2010 10:57 AM

Re: Making JavaMail work
 
On 8 jun, 11:08, jesbox <jesb...@gmail.com> 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(SMTPTra nsport.java:
> 1545)
> * * * * at com.sun.mail.smtp.SMTPTransport.protocolConnect(SM TPTransport.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.jav a: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(PlainSoc ketImpl.java:
> 195)


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

Lew 06-08-2010 11:36 AM

Re: Making JavaMail work
 
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

Martin Gregorie 06-08-2010 11:38 AM

Re: Making JavaMail work
 
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 |

Nigel Wade 06-08-2010 02:01 PM

Re: Making JavaMail work
 
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(SMTPTra nsport.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


jesbox 06-08-2010 02:04 PM

Re: Making JavaMail work
 
Thanks for the wakeup, now I'm heading for the next trouble.

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

Mike Amling 06-08-2010 02:27 PM

Re: Making JavaMail work
 
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(SMTPTra nsport.java:
> 1545)
> at com.sun.mail.smtp.SMTPTransport.protocolConnect(SM TPTransport.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.jav a: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(PlainSoc ketImpl.java:
> 195)


--Mike Amling

jesbox 06-08-2010 10:58 PM

Re: Making JavaMail work
 
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, "myEmail@gmail.com",
"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.jav a: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(SMTPTra nsport.java:
1533)
at
com.sun.mail.smtp.SMTPTransport.protocolConnect(SM TPTransport.java:
453)
at javax.mail.Service.connect(Service.java:313)
at javax.mail.Service.connect(Service.java:172)

Arne Vajh°j 06-08-2010 11:30 PM

Re: Making JavaMail work
 
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

jesbox 06-09-2010 08:23 AM

Re: Making JavaMail work
 
On Jun 9, 1:30*am, Arne Vajh°j <a...@vajhoej.dk> 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.


All times are GMT. The time now is 11:54 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.