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

时间:2011-04-11 11:22:19

标签: java javamail javax.mail

我正在使用一个名为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." );
      }
    }
  }
}

如何解决这个问题?

2 个答案:

答案 0 :(得分:3)

将javax.mail.jar和activation.jar添加到 转到您的项目 - >构建路径 - >配置构建路径 - > Java构建路径 - >库。

答案 1 :(得分:-1)

您需要右键单击项目选项卡中的项目名称,转到

  

属性 - >图书馆 - >按添加   JAR /文件夹

...浏览并选择你的罐子......然后点击OK ...和

  

清理并构建并重新运行

相关问题