邮件触发错误

时间:2015-06-25 07:23:04

标签: java email smtp

我有邮件触发程序。我有从我的机器到SMTP服务器的邮件触发器的访问权限。即使我能够telnet并ping到10.242.175.70/25。但是当我在我的机器上运行程序时,我面临如下错误。请帮我解决这个问题,也让我知道我做错了什么。我提供了activatio.jar和mail.jar,如果我遗漏了任何其他jar,那么也让我知道。

enter image description here

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();
      }
   }
}

1 个答案:

答案 0 :(得分:1)

结帐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");