我希望使用html表单在jsp中发送电子邮件,以获取值,来自主题,正文等。但此代码工作不正确。请帮我。感谢
<%@ page import="java.io.*,java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*,javax.activation.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<link href="bootstrap.css" rel="stylesheet" type="text/css"/>
<script src="bootstrap.js" type="text/javascript"></script>
<link href="style.css" rel="stylesheet" type="text/css"/>
<script src="jquery-3.1.1.js" type="text/javascript"></script>
</head>
<body style="background-color:lightgreen">
<form class="form-group" style=" margin-right:200px; padding: 30px 0px 0 25px; background-color:white">
<p style="margin: -30px 0 10px 70px; font-size: 20px">Sending Email </p>
<input type="email" class="form-control" placeholder="To" required name="to" style="width:300px" /><br />
<input type="email" class="form-control" placeholder="From" required name="from" style="width:300px" /><br />
<input type="text" class="form-control" placeholder="subject:" required name="subject" style="width:300px" /><br />
<textarea placeholder="body" required name="body" cols="32" rows="7" style="border:1px gray solid;border-radius:5px;"></textarea><br />
<input type="submit" value="send" name="btn" style="width: 300px; height: 35px; border: 1px solid gainsboro; border-radius: 4px; background-color:limegreen; color: white"/><br /><br />
<%
String from = request.getParameter("from");
String to = request.getParameter("to");
String subject = request.getParameter("subject");
String bod = request.getParameter("body");
final String username = "muneeb";
final String password = "******";
// Get system properties object
String host = "smtp.gmail.com";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");
// Get the Session object.
Session session1 = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
// Create a default MimeMessage object.
Message message = new MimeMessage(session1);
// Set From: header field of the header.
message.setFrom(new InternetAddress(from));
// Set To: header field of the header.
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
// Set Subject: header field
message.setSubject(subject);
// Now set the actual message
message.setText(bod);
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException e) {
System.out.println(" faild message....");
}
%>
</div>
</div>
</body>
</html>
显示此错误:
org.apache.jasper.JasperException:处理发生异常 第132行的JSP页面/assignment3/send_email.jsp 129:消息消息=新的MimeMessage(session1); 130: 131://设置自头的标题字段。 132:message.setFrom(new InternetAddress(from)); 133: 134://设置为:标题的标题字段。 135:message.setRecipients(Message.RecipientType.TO,Stacktrace: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:584) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:481) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329) javax.servlet.http.HttpServlet.service(HttpServlet.java:742) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
答案 0 :(得分:1)
我认为如果你在Servlet中这样做会更好。
看那里我会给你一个基本的例子,它将从html表单获取参数并在servlet中工作,如:
`@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Servlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
RequestDispatcher requestDispatcher;
String from = request.getParameter("sender");
String password = request.getParameter("pass");
String to = request.getParameter("receiver");
String subject = request.getParameter("subject");
String message = request.getParameter("message");
Properties properties = System.getProperties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.port", "587");
if (from.contains("hotmail")) {
properties.put("mail.smtp.host", "smtp.live.com");
} else if (from.contains("gmail")) {
properties.put("mail.smtp.host", "smtp.gmail.com");
} else if (from.contains("yahoo")) {
properties.put("mail.smtp.host", "smtp.mail.yahoo.com");
} else {
out.println("<h2 style='color:red;'>Unknown domain name!!</h2><br>"
+ "<p>Please use 'Yahoo or Gmail or Hotmail'</p>");
}
Session messageSession = Session.getDefaultInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, password);
}
});
try {
MimeMessage mimeMessage = new MimeMessage(messageSession);
mimeMessage.setFrom(new InternetAddress(from));
mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
mimeMessage.setSubject(subject);
mimeMessage.setText(message);
Transport.send(mimeMessage);
requestDispatcher = request.getRequestDispatcher("index.html");
requestDispatcher.forward(request, response);
out.println("<h2 style='color:green;'>Message sended successfully.</h2><br>" + "<p>Welldone.</p>");
} catch (MessagingException e) {
requestDispatcher = request.getRequestDispatcher("index.html");
out.println("<h2 style='color:red;'>Fatal Error!!</h2><br>" + "<p>" + e.getMessage() + "</p>");
}
}
}
`
基本的HTML表单:
<body>
<h1 align="center">Send Email Form</h1>
<form class="cf" action="Servlet" method="post">
<div align="center">
<table>
<tr>
<td>From : </td><td><input type="email" name="sender" size="30px" class="input-email"
placeholder="Email address"></td>
</tr>
<tr>
<td>Password : </td><td><input type="password" name="pass" size="30px" class="input-email"
placeholder="Sender Password"></td>
</tr>
<tr>
<td>To : </td><td><input type="email" name="receiver" size="30px" class="input-email"
placeholder="Email address"></td>
</tr>
<tr>
<td>Subject : </td><td><input type="text" name="subject" size="30px" class="input-email"
placeholder="Subject"></td>
</tr>
<tr>
<td>Message : </td><td><textarea name="message" id="input-message"
class="text" placeholder="Message"></textarea></td>
</tr>
<tr>
<td><input type="submit" value="SEND" class="button">
</td>
<td><input type="reset" value="CLEAR" class="button">
</td>
</tr>
</table>
</div>
</form>
我希望这对你有所帮助。
答案 1 :(得分:0)
您的JSP将被执行以显示表单,它将尝试在用户有机会输入任何内容之前发送电子邮件。
最好在servlet中移动java代码。