简体   繁体   中英

connection refused from mail server

I am using a .bns mail server to send mails. I have confirmed that the server is up and running and the port number 25 is open for connections. My firewall is off and I don't think "telnet mailrelay.bns 25" is supposed to get me connected to the mail server. I have compiled my code and ran on command line. I have also ran in another IDE environment. It is always giving me the same problem: connection refused. The code works on another station and it's sending the e-mails fine. The exact code was working on my station before a certain time. I'm pretty sure it's some configuration issue. However, I do not remember making any changes to the system settings or internet settings. So I'm having a hard time figuring out what's wrong. The following is my simple test code:

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();
    }
}

The following is the exact error message I get:

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
  1. Try connecting to port 25 via telnet from the box that is giving you problems
  2. Check firewall and iptable settings on the mail server
  3. View the related logs on the mail server

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM