我正在申请从jsp发送来自localhost的电子邮件,我发现错误如无法连接到SMTP主机:localhost,port:25; 嵌套异常是:java.net.ConnectException:连接被拒绝:连接 plz检查并给我解决方案或想法,如果你有.for我正在使用下面的代码.提前感谢你.
<%@ page language="java" import="javax.naming.*,java.io.*,javax.mail.*,
javax.mail.internet.*,com.sun.mail.smtp.*"%>
<html>
<head>
<title>Mail</title>
</head>
<body>
<%
try{
Session mailSession = Session.getInstance(System.getProperties());
Transport transport = new SMTPTransport(mailSession,new URLName("localhost"));
transport.connect("localhost",25,null,null);
MimeMessage m = new MimeMessage(mailSession);
m.setFrom(new InternetAddress(%><%request.getParameter("from")%><%));
Address[] toAddr = new InternetAddress[] {
new InternetAddress(%><%request.getParameter("to")%><%)
};
m.setRecipients(javax.mail.Message.RecipientType.TO, toAddr );
m.setSubject(%><%request.getParameter("subject")%><%);
m.setSentDate(new java.util.Date());
m.setContent(%><%request.getParameter("description")%><%, "text/plain");
transport.sendMessage(m,m.getAllRecipients());
transport.close();
out.println("Thanks for sending mail!");
}
catch(Exception e){
out.println(e.getMessage());
e.printStackTrace();
}
%>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
Dan*_*iar 14
首先,您必须确保在端口25上侦听SMTP服务器.
要查看您是否拥有该服务,您可以尝试使用TELNET客户端,例如:
C:\> telnet localhost 25
Run Code Online (Sandbox Code Playgroud)
(默认情况下,telnet客户端在最新版本的Windows上禁用,您必须从控制面板添加/启用Windows组件.在Linux/UNIX中,默认情况下通常会有telnet客户端.
$ telnet localhost 25
Run Code Online (Sandbox Code Playgroud)
如果等待很长时间然后超时,则表示您没有所需的SMTP服务.如果成功连接,您输入内容并能够输入内容,服务就在那里.
如果您没有该服务,可以使用以下方法:
如果您确定已经拥有该服务,则SMTP可能需要其他安全凭据.如果您可以告诉我在端口25上侦听的SMTP服务器,我可能会告诉您更多信息.
| 归档时间: |
|
| 查看次数: |
144016 次 |
| 最近记录: |