【问题标题】:Connection refused: connect - Java mail API连接被拒绝:连接 - Java 邮件 API
【发布时间】:2016-02-21 04:26:16
【问题描述】:

我在使用 java 邮件 API 从公司 Outlook 发送电子邮件时收到以下错误。

javax.mail.MessagingException: Could not connect to SMTP host:       smtp.mycompany.net.au, port: 25;
 nested exception is:
java.net.ConnectException: Connection refused: connect

我可以使用我的机器相同的端口远程登录服务器。问题的根本原因可能是什么?

用于发送电子邮件的代码是 -

Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", mysmtpserver);
props.put("mail.smtp.port", myport);

Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
            });

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromEmailAddress));

message.setRecipients(Message.RecipientType.BCC, addressTo);
message.setSubject(emailSubject);

BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setHeader("Content-Type", "text/html;charset=UTF-8");
messageBodyPart.setHeader("Content-Transfer-Encoding", "quoted-printable"); 
messageBodyPart.setContent(emailBody, "text/html;charset=UTF-8");

Multipart multipart = new MimeMultipart();  
//part 1-add html part
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
String filename = reportFilePath.trim();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));                    
messageBodyPart.setFileName(filename);                  
multipart.addBodyPart(messageBodyPart);

感谢任何帮助解决它。

谢谢 利宾

【问题讨论】:

  • 这意味着你的端口是开放的,但它拒绝连接
  • 为了清楚起见,您在运行 JavaMail 应用程序的同一台机器上使用 telnet,并且连接到 smtp.mycompany.net.au 上的端口 25,对吗?您是否从服务器收到 SMTP 问候消息?
  • 您能否向我们展示代码以更好地了解您在哪里犯了错误。也许你没有准确地设置属性
  • 我在我的代码中使用以下属性 -Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "smtp.mycompany.net.au"); props.put("mail.smtp.port", "25");
  • 我能够使用相同的代码使用 gmail smpt 服务器发送消息。问题在我开始使用公司 smtp 服务器后开始出现。

标签: java email smtp jakarta-mail


【解决方案1】:

java.net.ConnectException: Connection denied: connect error throws 由于以下原因,您必须检查:

  1. 您的主机名或端口
  2. 您的属性代码写得准确
  3. 确保防火墙没有阻止端口
  4. 如果您的服务器需要密码,请提供密码

感谢您提供代码。请应用以下代码。它非常容易理解并且工作正常。 100% 测试

import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;   
import javax.mail.event.*;      
import java.net.*;
import javax.activation.*;

public class SendEmail extends ConfigProperties{

        public static void send(String from, String to, String subject, String content) throws AddressException, MessagingException{

        ErrorCheck ec   = new ErrorCheck();
        String error = "";

        try{
            error   = ec.error();

            String smtp = getPropVal("SMTP");
            String host = getPropVal("HOST");

            Properties props=new Properties();
                props.put(smtp,host);
            Session   session1  =  Session.getDefaultInstance(props,null);
            Message msg =new MimeMessage(session1);

            msg.setHeader("Content-Type", "text/plain; charset=UTF-8");
            msg.setHeader("Content-Transfer-Encoding", "quoted-printable");           

               msg.setFrom(new InternetAddress(from));
                   msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to,false));
               msg.setSubject(subject);
               msg.setContent(content,"text/html; charset=UTF-8");      

                Transport.send(msg); 

            }catch(Exception e){
            ec.errorMsg(error+"SendEmail.send()", e);
            }
        }
}

【讨论】:

  • 我们如何检查防火墙是否阻塞了端口? @Ghayel
猜你喜欢
  • 2012-01-03
  • 1970-01-01
  • 2017-08-08
  • 1970-01-01
  • 1970-01-01
  • 2011-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多