代码:
<%@page import="java.sql.*"%>
<%@ page import="java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","12345");
String lid =request.getParameter("lid");
Statement stmt = con.createStatement();
String u = "select * from leaveemp ";
ResultSet rs = stmt.executeQuery(u);
while(rs.next()){
String LID = rs.getString(1);
String Status = rs.getString("Status");
PreparedStatement pst ;
pst = con.prepareStatement("update leaveemp set Status='Rejected' where LID='"+lid+"'");
pst.executeUpdate();
%>
<%
String peid = "select * from leaveemp where LID='"+lid+"'";
Statement st = con.createStatement();
ResultSet rs1 = st.executeQuery(peid);
while(rs1.next()){
String pemid = rs1.getString("EID");
String pemaid = "select * from empinfo where EID='"+pemid+"'";
Statement stm = con.createStatement();
ResultSet rs2 = stm.executeQuery(pemaid);
while(rs2.next()){
String pemaidd = rs2.getString("EEMailID");
//Creating a result for getting status that message is delivered or not!
String result;
final String to = pemaidd;
final String subject = "Leave Status";
final String messg = "Leave Application with "+lid+" Rejected";
final String from = "[email protected]";
final String pass = "password";
String host = "smtp.gmail.com";
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.user", from);
props.put("mail.password", pass);
props.put("mail.port", "443");
Session mailSession = Session.getInstance(props, new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, pass);
}
});
try {
MimeMessage message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject(subject);
message.setText(messg);
Transport.send(message);
} catch (MessagingException mex) {
mex.printStackTrace();
result = "Error: unable to send mail....";
}
%>
<title>Sending Mail in JSP</title>
<h1><center><font color="blue">Sending Mail Using JSP</font></h1>
<b><center><font color="red"><% out.println(result);%></b>
</body>
</html>
<%}}}%>
当我在另一个页面上单击拒绝时编写代码以发送 gmail,然后将上面的代码作为一个页面运行。
但是 gmail 被发送了 15 次,但我只想发送一次。
连result变量都打印了15次
我该怎么做才能解决这个错误?
请告诉我如何改进代码以及如何解决这个错误。