我有一个JSP代码。我尝试发送smtp电子邮件。它有一个错误:
Transport.send(消息);
有人能帮助我吗?
这是代码:
<%@ page import="java.io.*,java.util.*,javax.mail.*, javax.mail.Service"%>
<%@ 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 = "email@gmx.com";
// Sender's email ID needs to be mentioned
String from = "email@gmx.com";
// Assuming you are sending email from localhost
String host = "mail.gmx.com";
// Get system properties object
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", host);
// Get the default Session object.
Session mailSession = Session.getDefaultInstance(properties);
// 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....";
%>
<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>
PS。我在类路径中使用mail.jar和activation.jar。我将非常感谢你的帮助。
以下是我收到的错误消息:
org.apache.jasper.JasperException: An exception occurred processing JSP page /mail.jsp
at line 37
34: // Now set the actual message
35: message.setText("This is actual message");
36: // Send message
37: Transport.send(message);
38: result = "Sent message successfully....";
39:
40: %>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.ja
va:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:401)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
com.ish.system.runtime.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilt er.java:67)
root cause
javax.servlet.ServletException: javax.mail.MessagingException: Could not connect to
SMTP host: mail.gmx.com, port: 25;
nested exception is:
java.net.SocketException: Permission denied: connect
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.jav a:862)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java: 791)
org.apache.jsp.mail_jsp._jspService(mail_jsp.java:124)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
com.ish.system.runtime.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilt er.java:67)
root cause
javax.mail.MessagingException: Could not connect to SMTP host: mail.gmx.com, port: 25;
nested exception is:
java.net.SocketException: Permission denied: connect
com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
javax.mail.Service.connect(Service.java:275)
javax.mail.Service.connect(Service.java:156)
javax.mail.Service.connect(Service.java:105)
javax.mail.Transport.send0(Transport.java:168)
javax.mail.Transport.send(Transport.java:98)
org.apache.jsp.mail_jsp._jspService(mail_jsp.java:98)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
com.ish.system.runtime.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilt er.java:67)
root cause
java.net.SocketException: Permission denied: connect
java.net.PlainSocketImpl.socketConnect(Native Method)
java.net.PlainSocketImpl.doConnect(Unknown Source)
java.net.PlainSocketImpl.connectToAddress(Unknown Source)
java.net.PlainSocketImpl.connect(Unknown Source)
java.net.SocksSocketImpl.connect(Unknown Source)
java.net.Socket.connect(Unknown Source)
java.net.Socket.connect(Unknown Source)
com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:232)
com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1250)
com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
javax.mail.Service.connect(Service.java:275)
javax.mail.Service.connect(Service.java:156)
javax.mail.Service.connect(Service.java:105)
javax.mail.Transport.send0(Transport.java:168)
javax.mail.Transport.send(Transport.java:98)
org.apache.jsp.mail_jsp._jspService(mail_jsp.java:98)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
com.ish.system.runtime.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilt er.java:67)
答案 0 :(得分:1)
javax.mail.MessagingException: Could not connect to SMTP host: mail.gmx.com, port: 25; nested exception is: java.net.SocketException: Permission denied: connect
这是一个相当低级别的问题。执行当前Java运行时环境的操作系统平台指定用户不允许打开与给定主机的网络连接。
这可能有很多可能的原因,具体取决于用户的权限,对其的更高级别控制程度,防火墙设置,使用的操作系统等。至少,问题不在你的Java代码中(免责声明:我还没有读过它,整个异常已经是答案了,所以你的Java代码可能仍然存在一些无关的错误)
我会提到一些常见的原因,你明白应该在哪里寻找解决方案:
将Web应用程序部署到第三方主机,该主机不允许您的Web应用程序连接除第三方主机本身以外的任何主机。解决方案:升级帐户,以便您可以使用主机自己的邮件服务器(或者您可能已经拥有它而无需升级),或迁移到其他主机。
不允许特定于操作系统平台的非管理员用户帐户建立任何网络连接。管理员帐户必须相应地更改用户的帐户设置。
操作系统平台安装了防火墙,配置过于严格。尝试完全禁用防火墙并重新测试以排除其被怀疑,然后在必要时重新配置防火墙。