簡體   English   中英

Javax.mail。*包不存在 - 為什么?

[英]Javax.mail.* Package does not exist - Why?

我使用一個名為emailer的類從java應用程序發送電子郵件,
我正在使用netbeans 6.9.1並且我正在使用J2SE,我下載了javamail api並將jar添加到classpath ,並將它放在src用於netbeans。

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("fromblah@blah.com", "toblah@blah.com", "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." );
      }
    }
  }
}

怎么解決這個?

添加javax.mail.jar和activation.jar以轉到項目 - >構建路徑 - >配置構建路徑 - > Java構建路徑 - >庫。

您需要右鍵單擊項目選項卡中的項目名稱

屬性 - >庫 - >按添加Jar /文件夾

...瀏覽並選擇你的罐子......然后點擊OK ...和

清理並構建並重新運行

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM