Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Making JavaMail work

Reply
Thread Tools

Making JavaMail work

 
 
jesbox
Guest
Posts: n/a
 
      06-08-2010
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", "(E-Mail Removed)");
email.addRecipient("jesboxx", "(E-Mail 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 Removed)") !=
null ?
System.getProperty("(E-Mail 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(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)

 
Reply With Quote
 
 
 
 
nogales
Guest
Posts: n/a
 
      06-08-2010
On 8 jun, 11:08, jesbox <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      06-08-2010
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
 
Reply With Quote
 
Martin Gregorie
Guest
Posts: n/a
 
      06-08-2010
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 |
 
Reply With Quote
 
Nigel Wade
Guest
Posts: n/a
 
      06-08-2010
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

 
Reply With Quote
 
jesbox
Guest
Posts: n/a
 
      06-08-2010
Thanks for the wakeup, now I'm heading for the next trouble.

It seems I need an https connection to get to gmail.
 
Reply With Quote
 
Mike Amling
Guest
Posts: n/a
 
      06-08-2010
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
 
Reply With Quote
 
jesbox
Guest
Posts: n/a
 
      06-08-2010
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 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.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)
 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      06-08-2010
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
 
Reply With Quote
 
jesbox
Guest
Posts: n/a
 
      06-09-2010
On Jun 9, 1:30*am, Arne Vajh°j <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: A fresh new approach to making making online Fakename Computer Information 0 11-18-2005 02:10 AM
pop3s + javamail 1.3.2ea - does it really work? Michal M Java 0 04-04-2005 07:47 AM
Making new Flavors : Making a custom transferhandler for and drop applications ebby83@gmail.com Java 5 01-12-2005 11:10 AM
Re: Smtp Server error with JavaMail GaryM Java 8 07-21-2003 02:54 PM
Re: Smtp Server error with JavaMail Joseph Millar Java 0 07-16-2003 06:18 PM



Advertisments