如何使用JSP发送邮件? [英] How Do I Send A Mail Using Jsp?

查看:63
本文介绍了如何使用JSP发送邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好..

我在jsp页面中有以下代码..



Hello..
I have the following code in a jsp page..

<%@ 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设置了类路径,添加了必要的javax.activation.jar和mail.jar文件。但我仍然得到以下异常..



javax.mail.SendFailedException:发送失败;

嵌套异常是:

class javax.mail.MessagingException:无法连接到SMTP主机:425,端口:25;

嵌套异常是:

java.net.SocketException :网络无法访问:连接



如何解决这个问题?



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?

推荐答案

你的主机名无效。怎么会是425?它应该是由DNS正确解析的有效IP地址或域名主机名。



这段代码显示您不了解一些基础知识,即使此声明实际上并未在您的代码片段中使用(已注释掉):

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):
// Assuming you are sending email from localhost
   String host = "localhost";



否,在TCP之上的所有协议中,地址或主机名从未使用过客户端站点。服务器部分侦听某个端口和客户端部分,以便连接,使用服务器端的IP和端口号并执行连接。



-SA


这篇关于如何使用JSP发送邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆