无法连接到SMTP主机:localhost,port:25;嵌套异常是:java.net.ConnectException:Connection refused:connect [英] Could not connect to SMTP host: localhost, port: 25; nested exception is: java.net.ConnectException: Connection refused: connect

查看:543
本文介绍了无法连接到SMTP主机:localhost,port:25;嵌套异常是:java.net.ConnectException:Connection refused:connect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在申请发送电子邮件从localhost在jsp&我发现错误,如无法连接到SMTP主机:localhost,port:25;嵌套异常是:java.net.ConnectException:Connection refused:connect
plz检查并给我解决方案或想法,如果你有。因为我使用下面的代码。
提前感谢您。

 <%@ page language =javaimport =javax.naming。 *,java.io。*,javax.mail。*,
javax.mail.internet。*,com.sun.mail.smtp。*%>

< html>
< head>
< title> Mail< / title>
< / head>

< body>

<%
try {
会话mailSession = Session.getInstance(System.getProperties());

传输传输=新的SMTPTransport(mailSession,新的URLName(localhost));

transport.connect(localhost,25,null,null);


MimeMessage m = new MimeMessage(mailSession);

m.setFrom(new InternetAddress(%><%request.getParameter(from)%><%));

地址[] 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(感谢发送邮件!);
}
catch(Exception e){

out.println(e.getMessage());
e.printStackTrace();
}
%>


< / body>

< / html>


解决方案

首先你必须确保有一个SMTP服务器收听25号港口。



要查看是否有服务,可以尝试使用TELNET客户端,例如:

  C:\> telnet localhost 25 

(默认情况下,telnet客户端在Windows的最新版本上被禁用,您必须添加/从控制面板启用Windows组件在Linux / UNIX中,通常telnet客户端默认存在。

  $ telnet localhost 25 

如果它等待很久,那么超时,这意味着你没有所需的SMTP服务成功连接你输入的东西,能够输入的东西,服务就在那里。



如果你没有服务,你可以使用这些:




  • 一个模拟SMTP服务器,将模仿实际SMTP服务器的行为,正如您使用Java一样,建议 Dumbster 假的SMTP服务器,甚至可以在JUnit测试(使用安装/拆卸/验证)中工作,或独立运行为单独的进程用于集成测试。

  • 如果您的主机是Windows,您可以尝试安装 Mercury电子邮件服务器(也附带WAMPP在你的本地,然后运行上面的代码。

  • 如果您的主机是Linux或UNIX,请尝试启用邮件服务,例如 Postfix

  • Java中另一台完整的SMTP服务器,例如 Apache James 邮件服务器。



如果您确定已经拥有该服务,可能是SMTP需要额外的安全性证书。
如果可以告诉我什么SMTP服务器在端口25上侦听,我可能能够告诉你更多。


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>

解决方案

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:

C:\> telnet localhost 25

(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.

$ telnet localhost 25

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:

  • A mock SMTP server that will mimic the behavior of actual SMTP server, as you are using Java, it is natural to suggest Dumbster fake SMTP server. This even can be made to work within JUnit tests (with setup/tear down/validation), or independently run as separate process for integration test.
  • If your host is Windows, you can try installing Mercury email server (also comes with WAMPP package from Apache Friends) on your local before running above code.
  • If your host is Linux or UNIX, try to enable the mail service such as Postfix,
  • Another full blown SMTP server in Java, such as Apache James mail server.

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.

这篇关于无法连接到SMTP主机:localhost,port:25;嵌套异常是:java.net.ConnectException:Connection refused:connect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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