无法连接到 SMTP 主机:localhost,端口:25;嵌套异常是:java.net.ConnectException:连接被拒绝:连接
我正在做从 jsp & 中的本地主机发送电子邮件的应用程序我发现错误,如无法连接到 SMTP 主机:本地主机,端口:25;嵌套异常是:java.net.ConnectException:连接被拒绝:连接 如果您有,请检查并给我解决方案或想法。为此,我正在使用下面的代码。 预先感谢您。
<%@ 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>
I'm doing application for send email from localhost in jsp & i found error like Could not connect to SMTP host: localhost, port: 25; nested exception is: java.net.ConnectException: Connection refused: connect
plz check and give me solution or idea if you have .for that i'm using below code .
Thanking you in advance.
<%@ 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先你要确保有一个SMTP服务器在监听25端口。
要查看是否有该服务,你可以尝试使用TELNET客户端,例如:
(telnet客户端在Windows的最新版本中默认是禁用的,你有从控制面板添加/启用 Windows 组件通常默认情况下存在 telnet 客户端,
如果等待很长时间然后超时,则意味着您没有所需的 SMTP 服务。并且能够输入一些内容,
如果您没有该服务,您可以使用这些:
如果您确定已经拥有该服务,则 SMTP 可能需要额外的安全凭据。
如果您能告诉我哪个 SMTP 服务器在端口 25 上侦听,我也许可以告诉您更多信息。
First you have to ensure that there is a SMTP server listening on port 25.
To look whether you have the service, you can try using TELNET client, such as:
(telnet client by default is disabled on most recent versions of Windows, you have to add/enable the Windows component from Control Panel. In Linux/UNIX usually telnet client is there by default.
If it waits for long then time out, that means you don't have the required SMTP service. If successfully connected you enter something and able to type something, the service is there.
If you don't have the service, you can use these:
If you are sure that you already have the service, may be the SMTP requires additional security credentials.
If you can tell me what SMTP server listening on port 25 I may be able to tell you more.
CentOS 6 和其他支持 IPv6 的服务器平台上的邮件服务器可能绑定到 IPv6 本地主机 (::1),而不是 IPv4 本地主机 (127.0.0.1)。
典型症状:
如果发生这种情况,请确保
/etc/hosts
中没有两个具有不同 IP 地址的localhost
条目,如下(糟糕的)示例:为了避免混淆,请确保您只有一个
localhost
条目,最好是 IPv4 地址,如下所示:The mail server on CentOS 6 and other IPv6 capable server platforms may be bound to IPv6 localhost (::1) instead of IPv4 localhost (127.0.0.1).
Typical symptoms:
If this happens, make sure that you don't have two entries for
localhost
in/etc/hosts
with different IP addresses, like this (bad) example:To avoid confusion, make sure you only have one entry for
localhost
, preferably an IPv4 address, like this: