英文:
java.lang.RuntimeException: com.sun.mail.util.MailConnectException: Couldn't connect to host error
问题
以下是翻译好的内容:
我正在尝试使用Java Mail API从我的公司Outlook发送电子邮件,但是我遇到了连接被拒绝的错误。我已经检查了主机信息和凭据,它们是有效的。我正在使用来自Maven的1.6.2版本的javax.mail。
编辑更新:现在我考虑了一下,因为我是从本地尝试这个操作的,我怀疑这是防火墙问题造成的。我只想听听那些以前遇到过这种情况的人的见解,以及他们是如何解决的。
import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import org.testng.annotations.Test;
public class Email {
@Test
public static void sendEmail(){
Properties props = new Properties();
props.put("mail.smtp.host", "SMTP.ABC.DEF.COM");
props.put("mail.smtp.socketFactory.port", "587");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "587");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("john.doe@abc.com", "xxxx");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("john.doe@abc.com"));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("john.doe@abc.com"));
message.setSubject("testing");
BodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.setText("This is message body");
Transport.send(message);
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
java.lang.RuntimeException: com.sun.mail.util.MailConnectException: 无法连接到主机,端口:
SMTP.ABC.DEF.COM,587;超时-1;
嵌套异常是:
java.net.ConnectException:连接被拒绝:连接
英文:
I am trying to send email from my company's outlook using Java Mail API and I am getting this connection refused error. I checked host information and credentials and they are valid. I am using 1.6.2 version of javax.mail from Maven.
Edit Update : Now I thought about it and since I am trying this from my local and I am suspecting this is because of the firewall issue. I just want some insights from people who have experienced this before and what did they do to resolve.
import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import org.testng.annotations.Test;
public class Email {
@Test
public static void sendEmail(){
Properties props = new Properties();
props.put("mail.smtp.host", "SMTP.ABC.DEF.COM");
props.put("mail.smtp.socketFactory.port", "587");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "587");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("john.doe@abc.com", "xxxx");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("john.doe@abc.com"));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("john.doe@abc.com"));
message.setSubject("testing");
BodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.setText("This is message body");
Transport.send(message);
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
java.lang.RuntimeException: com.sun.mail.util.MailConnectException: Couldn't connect to host, port:
SMTP.ABC.DEF.COM, 587; timeout -1;
nested exception is:
java.net.ConnectException: Connection refused: connect
专注分享java语言的经验与见解,让所有开发者获益!
评论