1

我创建了一个用于发送邮件的 java 程序,并导入了一些与该程序相关的包。我已下载包javax.mailjavax.activation并将其放入C:\Program Files\Java\jdk1.7.0\jre\lib\ext

我编译我的程序然后它编译但是当我运行它时抛出异常。 在此处输入图像描述

我无法理解为什么它会抛出异常。

我的代码在这里。

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.net.*;

public class SendEmail
{

   public static void main(String [] args)
   {    
      // Recipient's email ID needs to be mentioned.
      String to = "rs.89.rj@gmail.com";

      // Sender's email ID needs to be mentioned
      String from = "manoj1990gupta@yahoo.in";
        try{
      // Assuming you are sending email from localhost
      String host = InetAddress.getLocalHost().toString();

      // Get system properties
      Properties properties = System.getProperties();

      // Setup mail server
      properties.setProperty("mail.smtp.host", 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();
      }}catch(Exception e){}    
   }
}
4

1 回答 1

1

您需要下载JavaMail API包并将其放入类路径中,但请确保包含 mail.jar 和您可以在 lib 文件夹中找到的所有依赖库。

从命令行设置类路径:

java -cp "mail.jar;lib/*" SendEMail
于 2013-09-22T17:26:11.600 回答