gpt4 book ai didi

Javamail - 为什么我无法让它工作?

转载 作者:行者123 更新时间:2023-12-01 14:07:24 25 4
gpt4 key购买 nike

我窃取了这段代码来测试使用 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." );
}
}
}
}

最佳答案

为了完整起见,这里是答案。

你的 Eclipse 正在告诉你

<Some Class> cannot be resolved to a type

这通常表明您的类路径不正确。你说

I downloaded the most recent javamail and put them in the jdk and jre lib folders, yet nothing changes

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

关于Javamail - 为什么我无法让它工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18795310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com