1
我試圖從我的struts應用程序發送測試郵件。我有一個簡單的jsp頁面和相應的頁面動作,我下載了一個簡單的代碼發送一封郵件,代碼如下。通過帶有java郵件API的struts發送電子郵件
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("[email protected]","********");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("[email protected]"));
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);
}
}
}
此代碼也在發送郵件。我已經將它更改爲一個類,以便我可以創建一個對象並從我的操作中調用該函數。像如下
public class mailtest
{
void mailSend()
{
//Same code as above
}
}
但是,當我在我的操作頁面創建該類的對象是給我一個例外如下..
根源
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)
希望你能理解,請如果您需要更多的說明意見。
我已將mail.jar添加到我的課程目錄中。現在它的工作。謝謝。 – 2014-11-09 15:16:18