Javamail-为什么可以';我不能让它工作吗?

Javamail-为什么可以';我不能让它工作吗?,java,email,jakarta-mail,Java,Email,Jakarta Mail,我偷了这段代码来测试如何使用java发送电子邮件。显然,Javamail是必需的。由于某些原因,我无法实现javax.mail。我下载了最新的javamail并将它们放在jdk和jre lib文件夹中,但没有任何变化。请,谢谢 //A class which uses this file to send an email : import java.util.*; import java.io.*; import javax.mail.*; import javax.mail.intern

我偷了这段代码来测试如何使用java发送电子邮件。显然,Javamail是必需的。由于某些原因,我无法实现javax.mail。我下载了最新的javamail并将它们放在jdk和jre lib文件夹中,但没有任何变化。请,谢谢

 //A class which uses this file to send an email : 

import java.util.*;
import java.io.*;
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();
    //the domains of these email addresses should be valid,
    //or the example will fail:
    emailer.sendEmail(
      "sean_chili@yahoo.com", "clevelanm@sou.edu",
       "Testing 1-2-3", "blah blah blah"
    );
   }

  /**
  * Send a single email.
  */
  public void sendEmail(
    String aFromEmailAddr, String aToEmailAddr,
    String aSubject, String aBody
  ){
    //Here, no Authenticator argument is used (it is null).
    //Authenticators are used to prompt the user for user
    //name and password.
    Session session = Session.getDefaultInstance( fMailServerConfig, null );
    MimeMessage message = new MimeMessage( session );
    try {
      //the "from" address may be set in code, or set in the
      //config file under "mail.from" ; here, the latter style is used
      //message.setFrom( new InternetAddress(aFromEmailAddr) );
      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);
    }
  }

  /**
  * Allows the config to be refreshed at runtime, instead of
  * requiring a restart.
  */
  public static void refreshConfig() {
    fMailServerConfig.clear();
    fetchConfig();
  }

  // PRIVATE //

  private static Properties fMailServerConfig = new Properties();

  static {
    fetchConfig();
  }

  /**
  * Open a specific text file containing mail server
  * parameters, and populate a corresponding Properties object.
  */
  private static void fetchConfig() {
    InputStream input = null;
    try {
      //If possible, one should try to avoid hard-coding a path in this
      //manner; in a web application, one should place such a file in
      //WEB-INF, and access it using ServletContext.getResourceAsStream.
      //Another alternative is Class.getResourceAsStream.
      //This file contains the javax.mail config properties mentioned above.
      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." );
      }
    }
  }
}

为了完整起见,以下是答案

你的日食告诉你

<Some Class> cannot be resolved to a type
无法解析为类型
这通常表示类路径不正确。你说

我下载了最新的javamail,并将它们放在jdk和jre中 lib文件夹,但没有任何更改


不要这样做。获取
javamail.jar
并在应用程序
构建路径上使用它。为此,将
jar
拖放到项目中,右键单击它并选择
buildpath>addtobuildpath

什么不起作用?不能让它工作并不能告诉我们任何事情。获取要做什么、如何做?以下是我得到的错误:我假设如果我添加javamail,那么它应该解决这些问题。不要将javamail jar放在jdk和jre文件夹中。把它放在你的应用程序类路径上。这就是让我困惑的地方。因为我正在使用eclipse,我是否将它与我的类一起放在我的工作区中?在eclipse中,您可以将它放在项目中,右键单击它并选择
buildpath>addtobuildpath
Perfect!非常感谢你!这个答案也在第二部分。