下面是我用来保存餐厅表的JSP代码。我想在表预订成功时向用户发送电子邮件,但无法向用户发送电子邮件。我已经尝试了很多方法来解决它,但它也返回一条错误消息,电子邮件无法发送。
<%@page import="java.util.Date"%>
<%@ page import="java.util.*" %>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.mail.internet.*,javax.activation.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="booking.Bean.Login"%>
<%@page import="booking.DB.OrderDB"%>
<%@page import="booking.Bean.Order"%>
<%@page import="booking.Wrapper.Wrapper"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<link href="css/style.css" rel="stylesheet" type="text/css">
<title>Add Order JSP</title>
</head>
<body>
<div id="container">
<div id="header">
<jsp:include page="header.jsp" />
</div>
<div id="menu">
<ul>
<li class="menuitem"><a href="aboutC.jsp">About</a>
</li>
<li class="menuitem"><a href="clientsC.jsp">Menu</a>
</li>
<li class="menuitem"><a href="contactC.jsp">Contact Us</a>
</li>
<li class="menuitem"><a href="login.jsp">Logout</a>
</li>
</ul>
</div>
<div id="leftmenu">
<div id="leftmenu_top"></div>
<div id="leftmenu_main">
<jsp:include page="orderMenu.jsp" />
</div>
<div id="leftmenu_bottom"></div>
</div>
<div id="content">
<div id="content_top"></div>
<div id="content_main">
<h2>Order Status</h2>
<%
String OrderCustId = request.getParameter("OrderCustIDtxt");
String OrderDate = request.getParameter("OrderDatetxt");
String OrderTime = request.getParameter("OrderTimetxt");
String OrderSeat = request.getParameter("OrderSeattxt");
//char BStatus = request.getParameter("OrderStatustxt");
//String userId = request.getParameter("txtUserId");
String custName = request.getParameter("textCustName");
String cEmail = request.getParameter("textCustEmail");
String cPhoneNo = request.getParameter("textCustContactNo");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(OrderDate);
Order order = new Order();
order.getOrderId();
order.setCustId(OrderCustId);
order.setStaffId("2000");
order.setOrderDate(date);
order.setOrderTime(OrderTime);
order.setOrderSeat(OrderSeat);
order.setBStatus('A');
order.setOCustName(custName);
order.setcOEmail(cEmail);
order.setcOPhoneNo(cPhoneNo);
OrderDB orderDB = new OrderDB();
int seatAvaliable=orderDB.checkSeat(order);
if(seatAvaliable<35){
Wrapper wrapper = new Wrapper();
int status = wrapper.addOrder(order);
if (status > 0) {
out.print("<center><h5><font color='blue'>Successful Place Order</font></h5></center>");
%>
<br>
<table border="0" align="center">
<tr>
<th colspan="2" bgcolor="lightgrey">Display Details Added New
Order</th>
</tr>
<tr>
<td>Order ID:</td>
<td><%=order.getOrderId()%></td>
</tr>
<tr>
<td>Customer ID:</td>
<td><%=OrderCustId%></td>
</tr>
<tr>
<td>Order Date:</td>
<td><%=date%></td>
</tr>
<tr>
<td>Order Time:</td>
<td><%=OrderTime%></td>
</tr>
<tr>
<td>Order Seat:</td>
<td><%=OrderSeat%></td>
</tr>
<tr>
<td>Customer Name:</td>
<td><%=custName%></td>
</tr>
<tr>
<td>Customer Email:</td>
<td><%=cEmail%></td>
</tr>
<tr>
<td>Customer Contact No:</td>
<td><%=cPhoneNo%></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td></td>
<td><input type="button" value="Back"
onclick="history.go(-1)" /></td>
</tr>
</table>
<%
String result;
String to = cEmail; //ur client email
String from = "dasreg10@gmail.com"; //ur email address
String host = "smtp.gmail.com"; //for yahoo smtp.mail.yahoo.com == 465
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
properties.setProperty("mail.smtp.port","587");
Session mailSession = Session.getDefaultInstance(properties);
try{
MimeMessage message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
//This is your email subject
message.setSubject("L'eo Cafe Booking Success");
//This is the actual msg
message.setText(custName+" "+"Your table has been reserved at "+date+" "+OrderTime);
// Send message
Transport.send(message);
result = "Booking information sent successfully....";
}catch (MessagingException mex) {
mex.printStackTrace();
result = "Error: unable to send message....";
}
out.println("Result: " + result + "\n");
%>
<%
} else {
out.print("<center><h5><font color='red'>Failed to Place Order</font></h5></center>");
}
%>
<br>
<%
}
else{
out.print("<center><h5><font color='red'>Booking Full!!!</font></h5></center>");
}
%>
</div>
<div id="content_bottom"></div>
<jsp:include page="footer.jsp" />
</div>
</div>
</body>
</html>
以下是从系统获取的例外:
javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. ay5sm8087291pab.1
at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at org.apache.jsp.addOrderJSP_jsp._jspService(addOrderJSP_jsp.java:256)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
这是我最后一年的学术项目。
答案 0 :(得分:0)
您要连接的邮件服务器未运行或配置为拒绝此方案中的连接。您可能甚至没有在localhost上安装邮件服务器
您可以通过telnet到localhost上的端口25来确认。
启动邮件服务器或检查配置,或者使用其他主机。
答案 1 :(得分:0)
不确定执行MimeMessage message = new MimeMessage(mailSession);
是否会在MimeMessage中获取相应的smtp服务器。你应该手动设置它:
message.addHeader("mail.smtp.host", host);
message.addHeader(""mail.smtp.port","587");
并为每个标题执行此操作。或者,您可以创建传输实例,请参阅here
修改强>
看到你的踪迹,我想你想要连接到gmail smtp
。这样做,您需要将密码放在要避免的代码中。也许尝试安装本地smtp服务器?