我想创建一个注册页面,用户在该页面上提供电子邮件 ID 并点击提交按钮。
当用户点击提交按钮时,我想要做的是生成随机密码并将其发送到他的电子邮件 ID,然后他可以使用该密码登录。随机生成的密码和电子邮件 ID 存储在我的数据库 (My SQL)中。
我正在使用此代码发送邮件。该文件位于 Login/src/beanmail/SendMailExample.java
package beanmail;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.sun.mail.smtp.SMTPSSLTransport;
public class SendMailExample {
public static boolean sendMail(String mail, String pass) {
boolean flag=false;
String from = "******@gmail.com";
String to = mail;
String subject = "Your user name and password..";
String message = mail+","+pass;
SendMail sendMail = new SendMail(from, to, subject,message);
sendMail.send();
flag = true;
return flag;
}
}
class SendMail {
private String from;
private String to;
private String subject;
private String text;
public SendMail(String from, String to, String subject, String text){
this.from = from;
this.to = to;
this.subject = subject;
this.text = text;
}
public void send()
{
String host = "smtp.gmail.com";
String userid = "*******@gmail.com";
String password = "******";
try
{
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.setProperty("mail.transport.protocol", "smtps");
props.put("mail.smtp.user", userid);
props.put("mail.smtp.password", password);
props.put("mail.smtp.port", "465");
props.put("mail.smtps.auth", "true");
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
InternetAddress fromAddress = null;
InternetAddress toAddress = null;
try {
fromAddress = new InternetAddress(from);
toAddress = new InternetAddress(to);
} catch (AddressException e) {
e.printStackTrace();
}
message.setFrom(fromAddress);
message.setRecipient(RecipientType.TO, toAddress);
message.setSubject(subject);
message.setText(text);
//SMTPSSLTransport transport =(SMTPSSLTransport)session.getTransport("smtps");
Transport transport = session.getTransport("smtps");
transport.connect(host, userid, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
我在这里注册了新用户
<jsp:useBean id="mal" class="beanmail.SendMailExample"></jsp:useBean>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String mail = request.getParameter("mail");
double dd = Math.random();
int val = (int)(dd*100000);
String pass = String.valueOf(val);
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/java","root","root");
PreparedStatement ps = con.prepareStatement("insert into login values(?,?)");
ps.setString(1, mail);
ps.setString(2, pass);
ps.executeUpdate();
if(mal.sendMail(mail,pass))
{
out.println("Password is Send to your mail id");
}
}
catch(Exception e){}
%>
</body>
我使用此代码进行身份验证。
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Driver"%>
<%@page import="java.sql.Connection"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String mail = request.getParameter("mail");
String pass = request.getParameter("pass");
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/Login","root","root");
PreparedStatement ps = con.prepareStatement("select * from login");
ResultSet rs = ps.executeQuery();
while(rs.next())
{
if(mail.equals(rs.getString(1))&&pass.equals(rs.getString(2)))
{
response.sendRedirect("welcome.jsp");
}
}
out.print("Wrong User name nd Password");
}
catch(Exception e){}
%>
</body>
数据库未更新,邮件无法发送。我在哪里做 Worng。我的数据库的图像。在 Ubuntu 12.04 上使用 My SQL server 5.5。我注意到右下角的一些东西说只读。