This week's book giveaway is in the
Agile and other Processes
forum.
We're giving away four copies of
The Mikado Method
and have Ola Ellnestam and Daniel Brolund on-line!
See
this thread
for details.
A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
Win a copy of
The Mikado Method
this week in the
Agile and other Processes
forum!
JavaRanch
»
Java Forums
»
Java
»
Servlets
Author
IO Exception :Connection timed out
varkala prabhakar
Ranch Hand
Joined: Sep 08, 2001
Posts: 54
posted
Jan 04, 2002 23:28:00
0
hai,
I have a mail
servlet
and when i try to run that servlet i get this error:,kindly tell me how to correct the error.
thanx
prabhakar
--------------------------- IO Exception Exception: java.net.ConnectException: Connection timed out -------------------------
code:
import javax.servlet.*; import javax.servlet.http.*; import java.net.*; import java.io.*; import java.util.*; import javax.mail.*; import javax.activation.*; import java.applet.*; import javax.swing.text.*; public class SendMail extends HttpServlet { public void init(ServletConfig config) throws ServletException { super.init(config); } public void doPost(HttpServletRequest request, HttpServletResponse response ) throws ServletException,IOException { response.setContentType("text/html"); PrintWriter out=response.getWriter(); String to = request.getParameter("toaddress"); String from = request.getParameter("fromaddress"); String caption = request.getParameter("heading"); String message = request.getParameter("message"); try { sendMail(to,from,caption,message) ; } catch(UnknownHostException uhe) { /* Traitement de UnknownHostException */ out.println("<html><head><title>Prabhakar Mail UnknownHost Exception</title></head><body>"); out.println("<b> UnknownHost Exception</b><p>"); out.println("Exception:\t"+uhe); out.println("</body>"); out.println("</html>"); //out.close(); } catch(ProtocolException pe) { /* Traitement du ProtocolException */ out.println("<html><head><title>Prabhakar Mail Protocol Exception</title></head><body>"); out.println("<b> Protocol Exception</b><p>"); out.println("Exception:\t"+pe); out.println("</body>"); out.println("</html>"); //out.close(); } catch(IOException ioe) { /* Traitement de IOException */ out.println("<html><head><title>Prabhakar Mail IO Exception</title></head><body>"); out.println("<b> IO Exception</b><p>"); out.println("Exception:\t"+ioe); out.println("</body>"); out.println("</html>"); out.close(); } out.close(); } // End of doGet public void sendMail(String to_address, // Destinataire du message String from_address, // Emeteur du message String sSu, // Sujet du message String sMess) // Message throws IOException, ProtocolException, UnknownHostException { Socket socket; // Le Socket DataInputStream in; // Le stream de lecture du Socket PrintStream out; // Le stream d'ecriture du Socket String host; // Identification du poste String str; // Pour la lecture de donnees // Identification du poste // host peut etre force a <a href="http://www.ibm.com" target="_blank" rel="nofollow">www.ibm.com</a> ou autre en cas de probleme host = InetAddress.getLocalHost().toString() ; // Ouverture du socket (connection au mailServer) // et des streams de lecture et d'ecriture // socket = new Socket(getDocumentBase().getHost(), 25); // socket = new Socket(url.getHost(), 25); socket = new Socket("mail.yahoo.com",25); in = new DataInputStream(socket.getInputStream()); out = new PrintStream(socket.getOutputStream()); // lecture du message initial str = in.readLine(); if (!str.startsWith("220")) throw new ProtocolException(str); while (str.indexOf('-') == 3) { str = in.readLine(); if (!str.startsWith("220")) throw new ProtocolException(str); } // fin message initial // Dialogue avec les Serveur de mail // Envoie de HELO au serveur SMTP out.println( "HELO " + host ); out.flush() ; str = in.readLine(); if (!str.startsWith("250")) throw new ProtocolException(str); // On est connecte au serveur de Mail... // Envoie du Mail out.println( "MAIL FROM: " + from_address ); out.flush() ; str = in.readLine(); if (!str.startsWith("250")) throw new ProtocolException(str); // A qui envoie t on cela out.println( "RCPT TO: " + to_address ); out.flush() ; str = in.readLine(); if (!str.startsWith("250")) throw new ProtocolException(str); // Est on pret a envoyer les donnees out.println( "DATA" ); out.flush() ; str = in.readLine(); if (!str.startsWith("354")) throw new ProtocolException(str); // Emmeteur - Destinataire - Sujet out.println("From: " + from_address); out.println("To: " + to_address); out.println( "Subject: " + sSu + "\n" ); out.flush() ; out.println("Comment: Unauthenticated sender"); out.println("X-Mailer: Simple tSmtp"); out.println(""); out.println( sMess ) ; out.println(".") ; out.flush() ; str = in.readLine(); if (!str.startsWith("250")) throw new ProtocolException(str); out.println("QUIT"); out.flush(); in.close() ; socket.close() ; return ; } } // end of class
Added code UBB tags
-Mike C
[ January 07, 2002: Message edited by: Mike Curwen ]
Mike Curwen
Ranch Hand
Joined: Feb 20, 2001
Posts: 3695
I like...
posted
Jan 07, 2002 09:33:00
0
Hi varkala,
Could you provide a bit more detail in your stack trace? It's helpful to see the full stack trace when diagnosing a problem.
There are a few things I will comment on:
You seem to be accessing mail.yahoo.com on port 25. Does Yahoo provide smtp service on this port?
Is there a reason you're not using JavaMail API to send/receive emails? It hides some of the Sockets and Streams details, and is a bit easier to use.
I agree. Here's the link:
http://aspose.com/file-tools
subject: IO Exception :Connection timed out
Similar Threads
Code to send subject & CC in mail system
What's problem with this SSL Socket??
Please Help, why this servlet is not working on mycgiserver
Exception in thread "main" javax.mail.AuthenticationFailedException:
Can we attach multiple email addresses through spring email
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter