Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Java Mail Problem

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic