我有邮件触发程序。我拥有从我的机器到 SMTP 服务器的邮件触发器的访问权限。即使我也能够 telnet 并 ping 到 10.242.175.70/25 。但是当我在机器上运行程序时,我遇到如下错误。请帮助我解决这个问题,并让我知道我做错了什么。我已经提供了 activatio.jar 和 mail.jar,如果我缺少任何其他 jar,请也告诉我。
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendEmail
{
public static void main(String [] args)
{
// Recipient's email ID needs to be mentioned.
String to = "mohit.darmwal@cognizant.com";
// Sender's email ID needs to be mentioned
String from = "mohit.darmwal@cognizant.com";
// Assuming you are sending email from localhost
String host = " 10.243.33.234";//"localhost";//"192.168.195.68";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("10.242.175.70", host);
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
// Set Subject: header field
message.setSubject("This is the Subject Line!");
// Now set the actual message
message.setText("This is actual message");
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
最佳答案
结账 JavaMail API供引用要设置哪些属性以及如何填充它们。
本例中的具体错误是主机名设置错误:
// Setup mail server
//Does not work this way:
//properties.setProperty("10.242.175.70", host);
//Use correct property name
properties.setProperty("mail.smtp.host", "10.242.175.70");
关于java - 邮件触发时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31043789/