我正在尝试从 Microsoft Exchange 服务器发送电子邮件,但似乎无法正常工作。
这是我的代码:
private static void sendEmail(){
host = "mail.tzr-019.co.il";
Properties props = System.getProperties();
props.setProperty("mail.smtps.host", host);
props.setProperty("mail.smtp.port", "25");
props.setProperty("mail.smtps.auth", "true");
Session session = Session.getDefaultInstance(props);
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress,user));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(TO));
message.setSubject("This is the Subject Line!");
message.setText("This is actual message");
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (Exception mex) {
mex.printStackTrace();
}
}
我收到错误消息:
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
at javax.mail.Service.connect(Service.java:345)
at javax.mail.Service.connect(Service.java:226)
at javax.mail.Service.connect(Service.java:175)
at javax.mail.Transport.send0(Transport.java:253)
at javax.mail.Transport.send(Transport.java:124)
有任何想法吗?