gpt4 book ai didi

java - 连接被拒绝 : connect - Java mail API

转载 作者:太空宇宙 更新时间:2023-11-04 13:12:55 26 4
gpt4 key购买 nike

我在使用 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);

感谢解决该问题的任何帮助。

谢谢利宾

最佳答案

java.net.ConnectException:连接被拒绝:由于以下原因引发连接错误,您必须检查:

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

关于java - 连接被拒绝 : connect - Java mail API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33796935/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com