我正在使用.bns邮件服务器发送邮件。我已确认服务器已启动并正在运行,端口号25已打开以进行连接。我的防火墙已关闭,我不认为“telnet mailrelay.bns 25”应该让我连接到邮件服务器。我编译了我的代码并在命令行上运行。我还在另一个IDE环境中运行。它总是给我同样的问题:连接被拒绝。该代码适用于另一个工作站,它正在发送电子邮件。在一段时间之前,确切的代码正在我的电台上工作。我很确定这是一些配置问题。但是,我不记得对系统设置或互联网设置进行任何更改。所以我很难搞清楚什么是错的。以下是我的简单测试代码:
private Logger logger = Logger.getLogger(this.getClass());
public static void main(String[] args)
{
String mailHost = "mailrelay.bns";
String mailFrom = "yyyy@yyy.com";
String mailTo = "xxxx@xxx.com";
String msg = "<html><body><p>hi</p></body></html>";
String subject = "MailUtilTest";
MailUtil mailUtil = new MailUtil();
mailUtil.sendMail2(msg, mailTo, subject, mailHost, mailFrom);
}
public void sendMail2(String msg, String mailTo, String subject, String mailHost, String mailFrom) {
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", mailHost);
Session session = Session.getDefaultInstance(properties);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(mailFrom));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));
message.setSubject(subject);
message.setText(msg);
Transport.send(message);
System.out.println("Message sent successfully...");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
以下是我收到的确切错误消息:
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: mailrelay.bns, 25; timeout -1;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
at javax.mail.Service.connect(Service.java:345)
at javax.mail.Service.connect(Service.java:226)
at javax.mail.Service.connect(Service.java:175)
at javax.mail.Transport.send0(Transport.java:253)
at javax.mail.Transport.send(Transport.java:124)
at com.scotiabank.sco.sched.commonutilities.MailUtil.sendMail2(MailUtil.java:76)
at com.scotiabank.sco.sched.commonutilities.MailUtilTest.sendMail2Test(MailUtilTest.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
at java.lang.reflect.Method.invoke(Method.java:611)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59)
at org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:98)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:79)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:87)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
at org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:88)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:381)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:243)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:230)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:377)
at java.net.Socket.connect(Socket.java:539)
at java.net.Socket.connect(Socket.java:488)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:297)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:229)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
... 30 more