aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes Java Mail Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Java Mail Problem" Watch "Java Mail Problem" New topic
Author

Java Mail Problem

Shanmugha Rajeshwaran
Greenhorn

Joined: Feb 22, 2008
Posts: 2
Hi All,

I am getting the following error while executing the mail program in java.

Exception in thread "main" javax.mail.MessagingException: Unknown SMTP host: smtp;
nested exception is:
java.net.UnknownHostException: smtp
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1704)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:525)
at javax.mail.Service.connect(Service.java:291)
at javax.mail.Service.connect(Service.java:172)
at lang.SimpleMail.main(SimpleMail.java:38)
Caused by: java.net.UnknownHostException: smtp
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:177)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:525)
at java.net.Socket.connect(Socket.java:475)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:284)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:227)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1672)
... 4 more

My Code snippet as follows:
Properties props = new Properties();

String username = "user";
String password = "password";
String host = "smtp";
String server = "mail.server.com";

props.setProperty("mail.transport.protocol", host);
props.setProperty("mail.host", server);
props.setProperty("mail.user", username);
props.setProperty("mail.password", password);

props.put("mail.smtp.auth", "true");
props.put("mail.smtp.", "true");
props.put("mail.smtp.port", "25");
props.put("mail.smtp.socketFactory.fallback", "false");

Session mailSession = Session.getDefaultInstance(props,null);
Transport transport = mailSession.getTransport();

MimeMessage message = new MimeMessage(mailSession);
message.setSubject("Testing JAVAMAIL PLAIN");
message.setContent("This is a test","text/plain");
message.addRecipient(Message.RecipientType.TO, new InternetAddress("email@server.com"));

transport.connect(host, username, password);
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
transport.close();

Kindly help me to come out of this problem.

Thanks in advance.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hello Shanmugha,

welcome to the JavaRanch

The cause of this exception seems to be a DNS problem. The host running this program can't resolve the name of the SMTP host "smtp". You could try to use the full qualified name like "smtp.your-domain.com" or the IP address instead. On the command line (on the host running this application) you can simply use the "ping" tool to ping this host. There you shoud experience problems too, if you use the hostname "smtp".

Marco
Shanmugha Rajeshwaran
Greenhorn

Joined: Feb 22, 2008
Posts: 2
Hi Marco,

Thanks for your Reply.

Even if i give smtp.mailserver.com also it throwing the following error:

Exception in thread "main" javax.mail.NoSuchProviderException: No provider for smtp.mail.gmail.com
at javax.mail.Session.getProvider(Session.java:460)
at javax.mail.Session.getTransport(Session.java:655)
at javax.mail.Session.getTransport(Session.java:636)
at javax.mail.Session.getTransport(Session.java:622)
at lang.SimpleMail.main(SimpleMail.java:31)

I used IP address also instead of this mail server name.But same error raised.Kindly help me.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282



Oops, sorry. I didn't look carefully enough at your sample code. There are more things wrong or confused with your properties!

First, what you call and use as "host" is not a host but the transport protocol for mail delivery. So "smtp" was correct but you shoud rename the variable in your code as it simply doesn't make sense to name it "host".

This means that what I called "host" is in your code "server". You should take care that you set server to a valid hostname of a real SMTP server which must allow you to send mails through it.

Usually public mail providers like Gmail won't relay arbitrary mails for you! They can receive arbitrary mails but they won't forward them! Most SMTP servers of a free mail provider will only allow you to send mails with authentication and only with the sender address of your own mail account. If that's not enough you will probably have to pay to get access to a public mail gateway or you will have to run your own SMTP server which doesn't work quite well with a home IP address

I hope that makes the problem a little bit clearer!

Marco

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Mail Problem