Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using Java Mail Could not connect to SMTP Host

 
Udaya Bascar
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i wrote a sample program to send a mail to my yahoo id
the program compiled perfectly when running i got this
error Could not connect to SMTP Host
What may be the mistake in this code
thanx n advance
bye
baskar
the sample progam and its error is below
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class SendMail
{
public static void main (String args[]) throws Exception
{
String host = "mail.yahoo.com";
String from = "baskar_rub@yahoo.com";
String to = "baskar_rub@yahoo.com";
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
Session session = Session.getDefaultInstance(props,null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
System.out.println(" setting subject and message ");
message.setSubject("hi ");
message.setText("how r u");
Transport.send(message);
System.out.println(" sent the message ");
System.out.println(" thanks ");
}
}

error on running
*** *** *** *** *** *** *** *** *** *** *** *** setting subject and message
Exception in thread "main" javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.MessagingException: Could not connect to SMTP host: mai
l.yahoo.com, port: 25;
nested exception is:
java.net.ConnectException: Connection timed out: connect
at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at SendMail.main(SendMail.java:23)
 
Nayanjyoti Talukdar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm not sure abt the problem.
I think, u need to specify the SMTP server port number.
Or by any chance, are u behind the proxy server?if that is the case, then u need to connect to proxy server first..that in turn talks to SMTP server.
I never used JavaMail API, but I wrote a SMTP client.
I'm saying just based on that experience.
-----------
Nayan.
[ January 08, 2003: Message edited by: Nayanjyoti Talukdar ]
 
Udaya Bascar
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am not behind proxy
i am working in server machine
how can i know the port no for mail servers
what do u mean by SMTP
could u describe it detailedly
bye
baskar
 
Udaya Bascar
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what do u mean by SMTP Client
 
Nayanjyoti Talukdar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The reserved port for SMTP server is 25. I think, u need to specify the number in u'r application. There are some default port numbers assigned to some of the common services like SMTP,FTP,TELNET,HTTP etc. As for example,to connect to the webserver, u have to specify the default port number for HTTP, i.e 80.
Hope, this helps u.
---------
Nayan.
 
Nayanjyoti Talukdar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
SMTP client is like u'r JavaMail.
SMTP client connects to the SMTP server and sends some messages( basically commands like to,from,messages etc). This part of forwarding messages take place through a protocol called SMTP(Short Mail Transfer Protocol). On the receiving side, different protocols are used to download mails from mail server. Protocols like POP3, IMAP are used for downloading.
------------
Nayan.
 
Udaya Bascar
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
thanx for ur valuable information
i solved the issue by changing the
Before
String host="mail.yahoo.com";
After
String host="202.88.152.9"; // which is the
// SMTP Address of My ISP
bye
baskar
 
Udaya Bascar
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i couldnt run this same program
on a proxy machine
where internet is connected thru winproxy
waiting for some useful info
bye
baskar
 
Nayanjyoti Talukdar
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
That means,u'r JavaMail application takes the default port number for SMTP. I thought, that could be one reason of failure. But I didn't understand, why do u need to give the ISP address as the host? If that is the case, working behind the proxy and trying to connect to yahoo mail server will be a big overhead.
------------
Nayan
 
code maestro
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had the same problem. It's the setting in your virus scan software. It blocks your port 25, this is to avoid mass emailing. enable this port and that should take care of it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic