gpt4 book ai didi

java - 使用jsp发送电子邮件

转载 作者:行者123 更新时间:2023-11-30 06:42:59 25 4
gpt4 key购买 nike

我想使用 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: An exception occurred processing JSP page /assignment3/send_email.jsp at line 132 129: Message message = new MimeMessage(session1); 130: 131: // Set From: header field of the header. 132: message.setFrom(new InternetAddress(from)); 133: 134: // Set To: header field of the header. 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)

最佳答案

我认为如果在 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>

希望这对您有帮助。

关于java - 使用jsp发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44111622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com