使用MS exchange server从java发送邮件,并给出连接超时
我正在尝试从我的java web应用程序发送邮件,其中我的邮件服务器主机是exg6.exghost.com,但它的连接超时了。当我的主机是smtp.gmail.com时,同样的代码也可以工作 我的代码是使用MS exchange server从java发送邮件,并给出连接超时,java,email,jakarta-mail,exchange-server,Java,Email,Jakarta Mail,Exchange Server,我正在尝试从我的java web应用程序发送邮件,其中我的邮件服务器主机是exg6.exghost.com,但它的连接超时了。当我的主机是smtp.gmail.com时,同样的代码也可以工作 我的代码是 Properties props = new Properties(); props.put("mail.smtp.host", "exg6.exghost.com"); props.put("mail.transport.protocol", "smtp");
Properties props = new Properties();
props.put("mail.smtp.host", "exg6.exghost.com");
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "false");
Session session1 = Session.getInstance(props,
new javax.mail.Authenticator() {
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication(username,password);
}
});
try {
Message message = new MimeMessage(session1);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("xyz@gmail.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler," +
"\n\n No spam to my email, please!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
我还尝试了mail.smtp.auth到true,但连接再次超时
请提供帮助。有关详细信息,请参阅JavaMail常见问题解答
看起来您正在使用默认(纯文本)SMTP端口进行连接。您的服务器很可能只接受SMTP over SSL端口上的连接。添加以下内容:
props.put("mail.smtp.ssl.enable", "true");
主机exg6.exghost.com是否正在侦听正确的端口?我如何知道主机是否正在侦听正确的端口?我终于让它正常工作了。参考已接受的答案