0
我想調用一個jsp頁面上的java函數。該功能應該按需發送郵件。從jsp文件調用java函數
這是Java代碼:
package s;
import java.io.UnsupportedEncodingException;
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.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMail {
public static void send() {
final String username = "";
final String password = "";
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
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);
}
}
}
,我調用它在JSP頁面上:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@import page="s.SendMail"%>
<%
s.SendMail.send();
%>
<html>
<head>
</head>
</html>
它不會編譯和Eclipse拋出一個異常:org.apache .jasper.JasperException 我做錯了什麼? 感謝您的幫助!
由於您導入's.SendMail',您不需要以's'作爲前綴:'SendMail.send()'就足夠了。如果這不起作用,那麼不知道。 – fge
它沒有工作...我有同樣的問題 – user2445729
看看[這個問題](http://stackoverflow.com/questions/239147/how-do-you-import-classes-in-jsp) – Quirin