我有一个包含有用代码的 Java 文件,我想在我的 JSP 文件中调用该 Java 代码。我已经尝试过了,例如我正在使用一个 Java 文件,它成功地将电子邮件发送到一个邮件 ID。但是,如果我在 JSP 页面中调用它,它的运行不会出错,但不会发送电子邮件。
Java代码:
package com.me;
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 SSL {
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("prakash.d2222","**********");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("from@no-spam.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("prakash_d22@rediffmail.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler," +
"from core java");
Transport.send(message);
System.out.println("Doneit");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
我的 JSP 代码是:
<html>
<body>
<jsp:useBean id="link" scope="application" class = "com.me.SSL" />
<% out.println("ok"); %>
</body>
</html>
和汤姆猫文件夹配置是
webapps\root\web-inf
|
-classes\com\me\SSL.class
|
-lib\mail.jar