简体   繁体   English

来自 Java 程序的电子邮件不起作用

[英]Email from Java program not working

I wrote a java program for sending email using my company's email server.我编写了一个 java 程序,用于使用我公司的电子邮件服务器发送电子邮件。 The code snippet below:下面的代码片段:

      Properties props = System.getProperties();
      props.put("mail.smtps.host", "mail.xxx.com");
      props.put("mail.smtps.auth", "true");
      props.put("mail.smtps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
      props.put("mail.smtps.port", "888");
      props.put("mail.smtp.starttls.enable","true");
      Session session =
      Session.getInstance(props, null); Message msg = new
      MimeMessage(session); msg.setFrom(new
      InternetAddress("yyy@mydomain.com"));
      msg.setRecipients(Message.RecipientType.TO,
      InternetAddress.parse("yyy@mydomain.com", false));
      msg.setSubject("Good Morning " + System.currentTimeMillis());
      msg.setText("This is for Test"); 
      msg.setHeader("X-Mailer", "My program"); 
      msg.setSentDate(new Date()); 
      SMTPTransport t = (SMTPTransport) session.getTransport("smtps");
      t.connect("mail.xxx.com", "yyy@mydomain.com", "myPwd");
      t.sendMessage(msg, msg.getAllRecipients());
      System.out.println("Response: " + t.getLastServerResponse());
      t.close();

The email functionality is not working.电子邮件功能不起作用。 Getting the following error:得到以下错误:

Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: mail.xxx.com, port: 888;
  nested exception is:
    java.net.ConnectException: Connection refused: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1972)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642)
    at javax.mail.Service.connect(Service.java:295)
    at javax.mail.Service.connect(Service.java:176)
    at cab.mail.Distribution.main(Distribution.java:50)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:319)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:233)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1938)
    ... 4 more

Would like to know what is causing the problem.想知道是什么导致了问题。 I tested this code with gmail server and my personal gmail id, it works fine.我用 gmail 服务器和我的个人 gmail id 测试了这段代码,它工作正常。 Could not figure out what I am missing over here.无法弄清楚我在这里错过了什么。

The root cause:根本原因:

Caused by: java.net.ConnectException: Connection refused: connect

Make sure that your application has access to your mail server mail.xxx.com at port 888 and make sure that your port is open at the firewall.确保您的应用程序可以通过端口888访问您的邮件服务器mail.xxx.com ,并确保您的端口在防火墙上是打开的。

Some antivirus (for example ViruScan) by default allows SMTP only to selected programs (like Outlook) and its white-list doesn't include Java.某些防病毒软件(例如 ViruScan)默认只允许 SMTP 访问选定的程​​序(例如 Outlook),并且其白名单不包括 Java。 Check your antivirus settings.检查您的防病毒设置。 And, of course, also your firewall settings as suggested in another answer.当然,还有另一个答案中建议的防火墙设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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