java Javax.mail.* 包不存在 - 为什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5620553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javax.mail.* Package does not exist - Why?
提问by Peter Fleming
Im using a class called emailer to send an email from a java application,
I am using netbeans 6.9.1and 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.
我使用名为 emailer 的类从 Java 应用程序发送电子邮件,
我使用的是netbeans 6.9.1,我使用的是 J2SE,我下载了 javamail api 并将 jar 添加到 jarclasspath
并将其放入src
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
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("[email protected]", "[email protected]", "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." );
}
}
}
}
How to solve this?
如何解决这个问题?
回答by TonTon
Add javax.mail.jar and activation.jar to Go to your project -> Build path -> Configure build path -> Java build path -> libraries.
将 javax.mail.jar 和 activation.jar 添加到您的项目 -> 构建路径 -> 配置构建路径 -> Java 构建路径 -> 库。
回答by Sangeet Menon
You need to right click on the project name in the project tab the go to
您需要在项目选项卡中右键单击项目名称,然后转到
Properties-> Libraries -> Press Add Jar/Folder
属性-> 库-> 按添加 Jar/文件夹
... Browse and select your jar...and click OK...and
...浏览并选择您的罐子...然后单击确定...和
Clean and Build and re-run
清理并构建并重新运行