我正在使用 JavaMail 来自动化工作流程。我想使用 JavaMail 发送电子邮件,两封电子邮件均以 @business.com 结尾。这是一个片段:
String host = "smtp.fakename.com";
Properties props = new Properties();
props.put("mail.debug", "true");
props.put("mail.smtp.host", host);
try {
Session session = Session.getDefaultInstance(props, null);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("fakename@business.com"));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress("anotherfakename@business.com", "Name"));
msg.setSubject("Test E-Mail through Java");
Transport.send(msg);
System.out.println("Sent message successfully...");
}
我很困惑为什么它不起作用。我认为我不需要进行任何类型的身份验证,因为电子邮件是通过同一域发送的,但我也尝试过使用身份验证。我尝试过通过端口 25、110、143、465 和 587。我能够使用相同的代码并添加密码身份验证来通过 gmail 发送电子邮件。我看到的最常见错误是“无法连接到 SMTP 主机:...”。任何见解将不胜感激。
最佳答案
String host = "smtp.xyz.com";
final String username = "xyz@business.com";
final String password = "your password";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
// go to account setting -> smtp setting and get server name and port
props.put("mail.smtp.host", "your server name");
props.put("mail.smtp.port", "your server port");
props.put("mail.debug", "true");
try {
Session session = Session.getInstance(prop,
new javax.mail.Authenticator() {
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication(username, password);
}
});
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("xyz@business.com"));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("abc@business.com"));
msg.setSubject("Test E-Mail through Java");
Transport.send(msg,username,password);
System.out.println("Sent message successfully...");
}catch(Exception e){
}
你可以试试这个可能会有帮助!!!
关于java - 通过公司域发送 JavaMail 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24810655/