2013-09-22 45 views
1

我創建了一個用於發送郵件的java程序,並且導入了與程序相關的一些程序包。我已經downloded包javax.mailjavax.activation中,並把它給C:\ Program Files文件\的Java \ jdk1.7.0 \ JRE \ lib中\分機jar和No類定義發現錯誤

我編譯我的程序則編譯但是當我運行它拋出異常。 enter image description here

我無法理解爲什麼它拋出異常。

我的代碼在這裏。

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 = "[email protected]"; 

     // Sender's email ID needs to be mentioned 
     String from = "[email protected]"; 
     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){}  
    } 
} 
+0

你'java'也許你不覺得一個(我的意思是JDK **和**在同一臺機器上安裝JRE)。如果'C:\ Program Files \ Java \ jdk1.7.0 \ jre \ bin \ java SendMail'工作正常,那麼你有一個JDK和一個JRE的安裝 – 2013-09-22 17:23:54

+0

請在你的main方法中打印'java.home'系統屬性來知道什麼是您的程序正在使用的Java路徑?你的jre/lib/ext路徑可能不同。 –

回答

1

您需要下載JavaMail API包,放入類路徑,但要確保包括了mail.jar和,你可以找到到lib文件夾中所有的依賴庫。

要通過命令行設置類路徑:

java -cp "mail.jar;lib/*" SendEMail 
+1

在我看到*之前,我已經開始註冊*「否則,您可以將所需的每個jar文件放在'jre \ lib \ ext'文件夾中*。在甲骨文收購他們之前,Sun已經提出了幾年不好的建議(好的,很久以前)。建議對一個新手來說是一條錯誤的路,IMO。 –

+0

我把它放到jre \ lib \ ext文件夾中,但它不起作用。 –

+0

@AndrewThompson好的,我刪除了。我補充說,只是因爲提問者提到了這個程序。 – aleroot