我正在使用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主机:..."。任何见解将不胜感激。
答案 0 :(得分:1)
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){
}
您可以尝试这样做,可能会有所帮助!
答案 1 :(得分:0)
是否需要进行身份验证以及要连接的主机和端口取决于内部邮件服务器的配置。您应该向系统管理员询问配置邮件客户端以连接到邮件服务器所需的配置信息。
答案 2 :(得分:0)
String host = "smtp.fakename.com";
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", "server name");
props.put("mail.smtp.port", "port");
props.put("mail.debug", "true");
try {
Session session = Session.getDefaultInstance(props, null);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("fakename@business.com"));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("anotherfakename@business.com"));
msg.setSubject("Test E-Mail through Java");
Transport.send(msg);
System.out.println("Sent message successfully...");
}catch(Exception e){
}