即時通訊使用一個名爲的emailer類從Java應用程序發送電子郵件,
我使用的NetBeans 6.9.1,我使用J2SE,我下載了JavaMail API和添加的jar到classpath
,也把它在netbeans的src
中。Javax.mail。*包不存在 - 爲什麼?
Netbeans拋出一個錯誤說Package javax.mail does not exist
,我不知道爲什麼?因爲我覺得我已經做了一切正確的,這裏是代碼
import java.util.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.*;
import javax.mail.internet.*;
/**
* Simple demonstration of using the javax.mail API.
*
* Run from the command line. Please edit the implementation
* to use correct email addresses and host name.
*/
public final class Emailer {
public static void main(String... aArguments){
Emailer emailer = new Emailer();
try {
emailer.sendEmail("[email protected]", "[email protected]", "Testing 1-2-3", "blah blah blah");
} catch (ClassNotFoundException ex) {
Logger.getLogger(Emailer.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void sendEmail(String aFromEmailAddr, String aToEmailAddr,
String aSubject, String aBody) throws ClassNotFoundException
{
Class.forName("javax.mail");
Session session = Session.getDefaultInstance(fMailServerConfig, null);
MimeMessage message = new MimeMessage(session);
try {
message.addRecipient(
Message.RecipientType.TO, new InternetAddress(aToEmailAddr)
);
message.setSubject(aSubject);
message.setText(aBody);
Transport.send(message);
}
catch (MessagingException ex){
System.err.println("Cannot send email. " + ex);
}
}
public static void refreshConfig() {
fMailServerConfig.clear();
fetchConfig();
}
private static Properties fMailServerConfig = new Properties();
static {
fetchConfig();
}
private static void fetchConfig() {
InputStream input = null;
try {
input = new FileInputStream("C:\\Temp\\MyMailServer.txt");
fMailServerConfig.load(input);
}
catch (IOException ex){
System.err.println("Cannot open and load mail server properties file.");
}
finally {
try {
if (input != null) input.close();
}
catch (IOException ex){
System.err.println("Cannot close mail server properties file.");
}
}
}
}
如何解決這個問題?
這聽起來像netbeans中的項目配置仍然沒有正確設置。 – 2011-04-11 11:25:19
將它添加到您的構建路徑? – 2011-04-11 11:25:34
您需要右鍵單擊項目選項卡中的項目名稱,然後轉到屬性 - >庫 - >按添加Jar/Folder ...瀏覽並選擇您的jar ...然後單擊確定...並構建並重新編譯。運行 – 2011-04-11 11:25:47