通过JSP发送电子邮件 [英] Send email through JSP
本文介绍了通过JSP发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="java.util.*" %>
<%
String from="abc@gmail.com";
String to="xyz@gmail.com";
try{
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.from(from);
client.to(to);
PrintStream message = client.startMessage();
message.println("To: " + to);
message.println("Subject: Sending email from JSP!");
message.println("This was sent from a JSP page!");
message.println();
message.println("Cool beans! :-)");
message.println();
message.println();
client.closeServer();
}
catch (IOException e){
System.out.println("ERROR SENDING EMAIL:"+e);
}
%>
</body>
</html>
我正在使用NetBean IDE 6.9.1.
我已将上面的代码写到SendMail.jsp文件中.
我添加了Java EE 6 API库.
它给出错误SmtpClient类未找到.
请帮我解决!!!
编译后出现错误消息
I am using netbean IDE 6.9.1.
I have written above code into a SendMail.jsp file.
I have added Java EE 6 API Liabrary.
it gives error SmtpClient class not found.
Plz help me to solve it!!!
Error message after compilation
ompiling 2 source files to C:\Documents and Settings\amitd\My Documents\NetBeansProjects\JMail\build\generated\classes
C:\Documents and Settings\amitd\My Documents\NetBeansProjects\JMail\build\generated\src\org\apache\jsp\SendMail2_jsp.java:69: cannot find symbol
symbol : class SmtpClient
location: class org.apache.jsp.SendMail2_jsp
SmtpClient client = new SmtpClient("smtp.gmail.com");
C:\Documents and Settings\amitd\My Documents\NetBeansProjects\JMail\build\generated\src\org\apache\jsp\SendMail2_jsp.java:69: cannot find symbol
symbol : class SmtpClient
location: class org.apache.jsp.SendMail2_jsp
SmtpClient client = new SmtpClient("smtp.gmail.com");
C:\Documents and Settings\amitd\My Documents\NetBeansProjects\JMail\build\generated\src\org\apache\jsp\SendMail2_jsp.java:72: cannot find symbol
symbol : class PrintStream
location: class org.apache.jsp.SendMail2_jsp
PrintStream message = client.startMessage();
C:\Documents and Settings\amitd\My Documents\NetBeansProjects\JMail\build\generated\src\org\apache\jsp\SendMail2_jsp.java:82: cannot find symbol
symbol : class IOException
location: class org.apache.jsp.SendMail2_jsp
catch (IOException e){
4 errors
C:\Documents and Settings\amitd\My Documents\NetBeansProjects\JMail\nbproject\build-impl.xml:597: The following error occurred while executing this line:
C:\Documents and Settings\amitd\My Documents\NetBeansProjects\JMail\nbproject\build-impl.xml:589: The following error occurred while executing this line:
C:\Documents and Settings\amitd\My Documents\NetBeansProjects\JMail\nbproject\build-impl.xml:285: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 6 seconds)
推荐答案
<%--
Document : SendMail
Created on : Oct 11, 2010, 5:50:27 PM
Author : amitd
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.io.*"%>
<%@ page import="sun.net.smtp.SmtpClient"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
/**
*
* @author amitd
*/
try
{
SmtpClient client = new SmtpClient("smtp.gmail.com");
String strfrom=request.getParameter("from");
String strto=request .getParameter("to");
String strsubject=request.getParameter("subject");
String strmessage=request.getParameter("message");
client.from(strfrom);
client.to(strto);
PrintStream message = client.startMessage();
message.println("To: " + strto);
message.println("Subject: "+strsubject);
message.println();
message.println(strmessage);
client.closeServer();
}
catch (IOException e)
{
System.out.println("ERROR SENDING EMAIL:"+e);
}
%>
<h1>Email Send !</h1>
<h2>URL = <%=request.getRequestURL()%> </h2>
<h2>From = <%=request.getParameter("from")%> </h2>
<h2>To = <%=request.getParameter("to")%> </h2>
<h2>Subject = <%=request.getParameter("subject")%> </h2>
<h2>Message = <%=request.getParameter("message")%> </h2>
</body>
</html>
这篇关于通过JSP发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文