Re: Smtp Server error with JavaMail

G

GaryM

Hi all,


I'm trying to utilize the JavaMail package and I've realized a class.
This is the constructor's code:


I am not sure why you are getting the exception. You have got it pretty
much right; however, I am doubtful of the way you are obtaining the
transport. Try the following code changes, which use the destination
address to automatically select the appropriate provider. If it still
does not work then it points to a classpath issue, however if mail.jar
is in the path, I don't see how it could be ...

public sendMail() throws AddressException, MessagingException {

String smtpServer = new String("mail.tin.it");
String userId = new String("USERID");
String password = new String("PASSWORD");

Properties props = new Properties();
props.put("mail.smtp.host", smtpServer);
props.put("mail.smtp.auth","true");
Session session = Session.getDefaultInstance(props, null);
// InternetAddress to = new InternetAddress("TO_ADDRESS");
Address to = new InternetAddress("TO_ADDRESS");
InternetAddress from = new InternetAddress("FROM_ADDRESS");
Message message= new MimeMessage(session);
message.setFrom(from);
message.setRecipient(Message.RecipientType.TO, to);
message.setSubject("Test JavaMail");
message.setContent("Ciao Mondo","text/plain");

// just for completeness you should do this
message.saveChanges();
Transport transport = null;
// transport = session.getTransport("smtp");
transport = session.getTransport(to);
transport.connect(smtpServer, userId, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
<snip>

Regards,

Gary
 
F

Federico

Thank to both Joseph and Gary, but it's still not working.

The smtp.jar is in the classpath, as the mail.jar and even the
activation.jar.

I've tried with the Gary's suggestions in the code, but it's still not
working.

What could be!??!?!?!?

Now is even changed the errore message:

DEBUG: getProvider() returning
javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun
Microsystems, Inc]
java.lang.reflect.InvocationTargetException
at
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at
java.lang.reflect.Constructor.newInstance(Constructor.java:274)
at javax.mail.Session.getService(Session.java:607)
at javax.mail.Session.getTransport(Session.java:541)
at javax.mail.Session.getTransport(Session.java:484)
at javax.mail.Session.getTransport(Session.java:464)
at sendMail.sendMail.<init>(sendMail.java:43)
at sendMail.simpleSendMessage.main(simpleSendMessage.java:50)
Caused by: java.lang.NoSuchMethodError:
javax.mail.Session.getDebugOut()Ljava/io/PrintStream;
at
com.sun.mail.smtp.SMTPTransport.<init>(SMTPTransport.java:72)
... 10 more
javax.mail.NoSuchProviderException: smtp
at javax.mail.Session.getService(Session.java:611)
at javax.mail.Session.getTransport(Session.java:541)
at javax.mail.Session.getTransport(Session.java:484)
at javax.mail.Session.getTransport(Session.java:464)
at sendMail.sendMail.<init>(sendMail.java:43)
at sendMail.simpleSendMessage.main(simpleSendMessage.java:50)
Exception in thread "main"
 
G

GaryM

Thank to both Joseph and Gary, but it's still not working.

The smtp.jar is in the classpath, as the mail.jar and even the
activation.jar.

I've tried with the Gary's suggestions in the code, but it's still
not working.

What could be!??!?!?!?

Although you get a javax.mail.NoSuchProviderException I think this is a
red herring as the first line is clearly from the
javamail.default.providers file in mail.jar. If you have this then you
have the SMTPTransport class (it is also invoked in the trace too). So
I no longer think it is a classpath issue.

I am beginning to suspect the SMTP AUTH. I cannot test this myself
easily, but I wonder if you can test it with a non-auth server using
the static method Transport.send(msg)?
 
G

GaryM

Although you get a javax.mail.NoSuchProviderException I think this
is a red herring as the first line is clearly from the
javamail.default.providers file in mail.jar. If you have this then
you have the SMTPTransport class (it is also invoked in the trace
too). So I no longer think it is a classpath issue.

I am beginning to suspect the SMTP AUTH. I cannot test this myself
easily, but I wonder if you can test it with a non-auth server
using the static method Transport.send(msg)?

Sorry to followup on my own post. I just did do a test with your code
on my SMTP AUTH enabled server and it does work.

You seem to be falling over when the Javamail determines the name of
the Transport provider from the javamail.default.providers and
reflection tries to instantiate that class. Therefore:

1. Can you check that you have the com.* hierarchy in your mail.jar and
the integrity of the jar is good?

2. Earlier you referred to "smtp.jar". Can you remove this (it is part
of mail.jar anyway)?

3. I'd still like to see if it works on a non AUTH server.


Here is my debug log:

DEBUG: JavaMail version 1.3
DEBUG: java.io.FileNotFoundException: C:\Program Files\Java\j2re1.4.1_
01\lib\javamail.providers (The system cannot find the file specified)
DEBUG: !anyLoaded
DEBUG: not loading resource: /META-INF/javamail.providers
DEBUG: successfully loaded resource: /META-
INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name:
{com.sun.mail.smtp.SMTPTransport=javax.mail.Provider
[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc],
com.sun.mail.imap.IMAPStore=javax.mail.Provider
[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc],
com.sun.mail.pop3.POP3Store=javax.mail.Provider
[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {imap=javax.mail.Provider
[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], pop3
=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun
Microsystems, Inc], smtp=javax.mail.Provider
[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
DEBUG: successfully loaded resource: /META-
INF/javamail.default.address.map
DEBUG: !anyLoaded
DEBUG: not loading resource: /META-INF/javamail.address.map
DEBUG: java.io.FileNotFoundException: C:\Program Files\Java\j2re1.4.1_
01\lib\javamail.address.map (The system cannot find the file specified)
DEBUG: getProvider() returning javax.mail.Provider
[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG: SMTPTransport trying to connect to host "xxxx", port 25

<SNIP>
 
F

Federico

I've checked everything (apart the integrity of the jar, how can you
check it?) and it's still not working. Perhaps it's the system.

Tomorrow I'll try on another system to see what happens

Anyway thanks for everything.

Fede
 
F

Federico

I've wrote the same code on another machine and it works perfectly

Really don't know what's happening on the machine where isn't working,
but I'm happy it works where I have to implement the application :)

Thank you for all of your suggestion.

Fede
 
S

Scott Yanoff

Federico said:
I've wrote the same code on another machine and it works perfectly

Really don't know what's happening on the machine where isn't working,
but I'm happy it works where I have to implement the application :)

I do not know the ins and outs of JavaMail, but does the machine where
this did not work on have access to the mail server you are using? Try
pinging the mail server hostname from that machine. I believe that
JavaMail connects to port 25 of the server, so perhaps you could try:

telnet machineName 25
EHLO

You should get some type of greeting message back after typing EHLO.

Cheers,
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top