我想在用户输入错误密码两次以使用java / java脚本设置其他密码时向用户发送邮件。我用java和html尝试过这个 代码在这里帮助我做这个
//here how to add the function to send mail when user entered wrong password for two times. or redirect to java class file to send mail.//
function validation(){
var uname=document.ulogin.username.value;
var pass=document.ulogin.password.value;
if(uname===0){
alert("Enter username");
document.ulogin.username.focus();
return false;
}
if(pass===0){
alert("Enter password");
document.ulogin.password.focus();
return false;
}
var uword = hex_md5(document.getElementById(jfldid).value);
if (uword==cword[anum-1]) {
return true;
}
//here how to add the function to send mail when user entered wrong password for two times. or redirect to java class file to send mail.//
java for sending mail to user
package pack;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class MailUtil {
private String SMTP_HOST = "smtp.gmail.com";
private String FROM_ADDRESS = "musicdonz@gmail.com";
private String PASSWORD = "password123";
private String FROM_NAME = "Alert";
public boolean sendMail(String[] recipients, String[] bccRecipients, String subject, String message) {
try {
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", SMTP_HOST);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.debug", "false");
props.put("mail.smtp.ssl.enable", "true");
Session session = Session.getInstance(props, new SocialAuth());
Message msg = new MimeMessage(session);
InternetAddress from = new InternetAddress(FROM_ADDRESS, FROM_NAME);
msg.setFrom(from);
InternetAddress[] toAddresses = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
toAddresses[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, toAddresses);
InternetAddress[] bccAddresses = new InternetAddress[bccRecipients.length];
for (int j = 0; j < bccRecipients.length; j++) {
bccAddresses[j] = new InternetAddress(bccRecipients[j]);
}
msg.setRecipients(Message.RecipientType.BCC, bccAddresses);
msg.setSubject(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);
return true;
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(MailUtil.class.getName()).log(Level.SEVERE, null, ex);
return false;
} catch (MessagingException ex) {
Logger.getLogger(MailUtil.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
}
class SocialAuth extends Authenticator {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(FROM_ADDRESS, PASSWORD);
}
}
}
<form action="userid" name="name" onsubmit="return validation()">
<font style="color: black;font-size: 15px"> Enter UserName:</font><br></br>
<input type="text" name="uname" placeholder="Enter username"><input type="password" name="password" placeholder="Enter password"> </input><br></br>
<input type="submit" value="Submit" style="background-color: yellowgreen;color: #ffffff;width: 70px;"></input><span> </span>
<input type="reset" value="Reset" style="background-color: tomato;color: #ffffff;width: 70px;"></input><br></br>
New User <a href="register.jsp" style="color: cadetblue;font-size: 15px;">Register here</a>
</form>