我使用的是myeclipse7,在这里我添加了javaEE5库,我将mail.jar从外部添加到应用程序中,它从javaEE5库中调用javaee.jar。它没有使用mail.jar.If我删除了javaEE5库,但我需要javaEE5库。如何使用java EE 5库的javaee.jar发送邮件?
如果需要话,下面是我的sed邮件的hava代码
String d_email = "email@gmail.com",
d_password = "pass",
d_host = "smtp.gmail.com",
d_port = "465",
m_to = "email@gmail.com",
m_subject = "Testing",
m_text = "This is the testing email.";
public Main()
{
Properties props = new Properties();
props.put("mail.smtp.user", d_email);
props.put("mail.smtp.host", d_host);
props.put("mail.smtp.port", d_port);
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.socketFactory.port", d_port);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
SecurityManager security = System.getSecurityManager();
try
{
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
session.setDebug(true);
MimeMessage msg = new MimeMessage(session);
msg.setText(m_text);
msg.setSubject(m_subject);
msg.setFrom(new InternetAddress(d_email));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));
Transport.send(msg);
System.out.println("Mail Sent");
}
catch (Exception mex)
{
mex.printStackTrace();
}
}
public static void main(String[] args)
{
Main blah = new Main();
}
private class SMTPAuthenticator extends javax.mail.Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(d_email, d_password);
}
}错误:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
at javax.mail.Session.loadProvidersFromStream(Session.java:928)
at javax.mail.Session.access$000(Session.java:174)
at javax.mail.Session$1.load(Session.java:870)
at javax.mail.Session.loadResource(Session.java:1084)
at javax.mail.Session.loadProviders(Session.java:889)
at javax.mail.Session.<init>(Session.java:210)
at javax.mail.Session.getInstance(Session.java:232)
at com.ctn.origin.connection.Main.<init>(Main.java:35)
at com.ctn.origin.connection.Main.main(Main.java:55)发布于 2011-06-02 01:44:03
您应该手动添加容器特定的库,如javaee.jar,而不是手动添加到您的项目中。如果您在不同制造商/版本的容器上部署和运行项目,则只会导致运行时问题。javaee.jar是Glassfish特有的。由于Glassfish本身已经附带了mail.jar,这表明您的目标运行时根本不是Glassfish。可能是Tomcat或者JBoss之类的。
如果您这样做是为了克服项目编译错误,那么您应该以不同的方式解决这个问题。我不做MyEclipse,但我相信这与Eclipse非常相似,所以这个基于Eclipse的答案应该也适用于MyEclipse :右键单击项目,选择属性,转到目标运行时部分,然后从列表中选择目标运行时(您想要使用的servletcontainer )。这样,Eclipse将以正确的方式自动将其库包含在项目的构建路径中。
另请参阅:
https://stackoverflow.com/questions/6205359
复制相似问题