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:
Caused by: java.net.UnknownHostException: smtp
... 4 more
My Code snippet as follows:
Properties props = new Properties();
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@example.com"));
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".
Joined: Feb 22, 2008
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
I used IP address also instead of this mail server name.But same error raised.Kindly help me.
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!