简体   繁体   English

Javamail-为什么我无法使它正常工作?

[英]Javamail - Why can't I get it to work?

I stole this code to test about emailing using java. 我偷了这段代码以测试有关使用Java发送电子邮件的信息。 Javamail is required, obviously. 显然,需要Javamail。 For some reason, I can't get javax.mail to implement. 由于某种原因,我无法实现javax.mail。 I downloaded the most recent javamail and put them in the jdk and jre lib folders, yet nothing changes. 我下载了最新的javamail并将它们放在jdk和jre lib文件夹中,但是没有任何变化。 Please and thank you! 谢谢,麻烦您了!

 //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." );
      }
    }
  }
}

Just for completeness, here's the answer. 仅出于完整性考虑,这就是答案。

Your Eclipse is telling you 您的Eclipse告诉您

<Some Class> cannot be resolved to a type

This is usually an indication that your classpath is not correct. 这通常表明您的类路径不正确。 You said 你说

I downloaded the most recent javamail and put them in the jdk and jre lib folders, yet nothing changes 我下载了最新的javamail并将它们放在jdk和jre lib文件夹中,但没有任何更改

Don't do this. 不要这样 Take the javamail.jar and use it on your application Build Path . javamail.jar并在您的应用程序Build Path上使用它。 To do so, drag and drop the jar into your project, right-click it and select Build Path > Add to build path . 为此,将jar拖放到您的项目中,右键单击它,然后选择Build Path > Add to build path

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM