aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes Using Java Mail Could not connect to SMTP Host Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Using Java Mail Could not connect to SMTP Host" Watch "Using Java Mail Could not connect to SMTP Host" New topic
Author

Using Java Mail Could not connect to SMTP Host

Udaya Bascar
Ranch Hand

Joined: Jun 18, 2002
Posts: 32
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

Joined: Feb 12, 2002
Posts: 71
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

Joined: Jun 18, 2002
Posts: 32
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

Joined: Jun 18, 2002
Posts: 32
what do u mean by SMTP Client
Nayanjyoti Talukdar
Ranch Hand

Joined: Feb 12, 2002
Posts: 71
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

Joined: Feb 12, 2002
Posts: 71
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

Joined: Jun 18, 2002
Posts: 32
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

Joined: Jun 18, 2002
Posts: 32
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

Joined: Feb 12, 2002
Posts: 71
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

Joined: May 25, 2005
Posts: 1
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Using Java Mail Could not connect to SMTP Host