【发布时间】: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