开发者

Javax.mail.* Package does not exist - Why?

开发者 https://www.devze.com 2023-02-24 07:07 出处:网络
Im using a class called emailer to send an email from a java application, I am using netbeans 6.9.1 and I am using J2SE, I downloaded the javamail api and added the jar to the classpath and also put i

Im using a class called emailer to send an email from a java application,

I am using netbeans 6.9.1 and I am using J2SE, I downloaded the javamail api and added the jar to the classpath and also put it in the src for netbeans.

Netbeans is throwing up an error saying Package javax.mail does not exist and I dont know why? As I feel I have done everything correct, here is the code

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 refr开发者_如何学JAVAeshConfig() {
    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." );
      }
    }
  }
}

How to solve this?


Add javax.mail.jar and activation.jar to Go to your project -> Build path -> Configure build path -> Java build path -> libraries.


You need to right click on the project name in the project tab the go to

Properties-> Libraries -> Press Add Jar/Folder

... Browse and select your jar...and click OK...and

Clean and Build and re-run

0

精彩评论

暂无评论...
验证码 换一张
取 消