Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello..
I have the following code in a jsp page..

XML
<%@ page import="java.io.*,java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*,javax.activation.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%
   String result;
   // Recipient's email ID needs to be mentioned.
   String to = "vivekanandbandekar@gmail.com";

   // Sender's email ID needs to be mentioned
   String from = "hcmmca@gmail.com";

   // Assuming you are sending email from localhost
   String host = "localhost";

   // Get system properties object
   //Properties properties = System.getProperties();
    Properties properties = new Properties();
    properties.put("mail.smtp.host","425");
   // Setup mail server
   //properties.setProperty("mail.smtp.host", host);

   // Get the default Session object.
   Session mailSession = Session.getDefaultInstance(properties,null);

   try{
      // Create a default MimeMessage object.
      MimeMessage message = new MimeMessage(mailSession);
      // Set From: header field of the header.
      message.setFrom(new InternetAddress(from));
      // Set To: header field of the header.
      message.addRecipient(Message.RecipientType.TO,
                               new InternetAddress(to));
      // Set Subject: header field
      message.setSubject("This is the Subject Line!");
      // Now set the actual message
      message.setText("This is actual message");
      // Send message
      Transport.send(message);
      result = "Sent message successfully....";
   }catch (MessagingException mex) {
      mex.printStackTrace();
      result = "Error: unable to send message....";
   }
%>
<html>
<head>
<title>Send Email using JSP</title>
</head>
<body>
<center>
<h1>Send Email using JSP</h1>
</center>
<p align="center">
<%
   out.println("Result: " + result + "\n");
%>
</p>
</body>
</html>


i have set the classpath, added the necessary javax.activation.jar and mail.jar files. but still I am getting the following exception ..

javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.MessagingException: Could not connect to SMTP host: 425, port: 25;
nested exception is:
java.net.SocketException: Network is unreachable: connect

How to solve this problem?
Posted

1 solution

You host name is invalid. How can it be "425"? It should be a valid IP address or a domain host name correctly resolved by DNS.

And this piece of code reveals that you don't understand some basics, even though this declaration is not actually used in your code fragment (commented out):
Java
// Assuming you are sending email from localhost
   String host = "localhost";

No, in all protocols on top of TCP the address or host name of the client site is never used. The server part listens on some port, and the client part, to get connected, uses IP and port number of the server side and performs connection.

—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900