我有用于邮件触发的程序。我具有从我的机器到SMTP服务器的邮件触发的访问权限。甚至我也可以远程登录并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 = "[email protected]";

      // Sender's email ID needs to be mentioned
      String from = "[email protected]";

      // 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/

10-11 15:52