使用java邮件API通过struts发送电子邮件 [英] Sending an email through struts with java mail API
问题描述
我尝试从我的struts应用程序发送测试邮件。我有一个简单的jsp页面和对应该页面的动作,我下载了一个简单的代码来发送电子邮件,代码如下。
I trying to send a test mail from my struts application. i have a simple jsp page and the action corresponding to that page, i downloaded a simple code to send an email the code is as follows.
package java4s;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class mailtest {
public static void main(String[] args) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("snapshareadm@gmail.com","********");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("snapshareadm@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("justinpayyans@gmail.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler," +
"\n\n No spam to my email, please!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
此代码正在发送邮件。我把它改成了一个类,这样我就可以创建一个对象并从我的动作中调用该函数。如下所示
This code is working an its sending mails too. I've changed it to a class so that i can create an object and call the function from my action. Like follows
public class mailtest
{
void mailSend()
{
//Same code as above
}
}
但是当我在我的行动页面中创建这个类的对象时,它给我一个例外如下..
But when i create the object of this class in my action page it is giving me an exception as follows..
根本原因
java.lang.NoClassDefFoundError: javax/mail/MessagingException
java4s.mailsender.execute(mailsender.java:50) //on this line i've created object of the mailtest class
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
希望您理解,如果您需要,请发表评论以获得更多说明..
Hope you understand, please comment for more clarifications if you needed..
推荐答案
首先,修复所有这些常见错误。
我在猜您没有在Java EE应用程序服务器中运行; Java EE应用程序服务器包含JavaMail作为标准部分。
I'm guessing that you're not running in a Java EE application server; Java EE application servers include JavaMail as a standard part.
如果您只是在Tomcat中运行,则需要将JavaMail jar文件提供给您的应用程序,或者将它放在war文件的WEB-INF / lib目录中,或者将它放在 Tomcat的lib目录。
If you're just running in Tomcat, you'll need to make the JavaMail jar file available to your application, either by putting it in the WEB-INF/lib directory of your war file, or putting it in Tomcat's lib directory.
这篇关于使用java邮件API通过struts发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!