java.lang.RuntimeException: com.sun.mail.util.MailConnectException: 无法连接到主机错误

huangapple 未分类评论47阅读模式
英文:

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.COM587超时-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

huangapple
  • 本文由 发表于 2020年5月29日 12:38:25
  • 转载请务必保留本文链接:https://java.coder-hub.com/62078839.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定