效果图
点击链接就可以跳转到要跳转的页面啦 ,注意 QQ 邮箱会发送到垃圾箱
代码
Dao 层
public class UserDao {
public int add(User user) throws SQLException {
String sql="insert into user values(?,?,?,?,?,?,?,?,?,?)";
QueryRunner runner=new QueryRunner(DataSourceUtils.getDataSource());
return runner.update(sql, user.getUid(),
user.getUsername(), user.getPassword(), user.getName(),
user.getEmail(), user.getTelephone(), user.getBirthday(), user.getSex(), user.getState(),
user.getCode());
}
}
Service 层
public class UserService {
private UserDao ud=new UserDao();
public boolean add(User user) {
boolean flag=false;
try {
int a=ud.add(user);
if (a>0) {
flag=true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
}
Servlet 层
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import com.yinhe.bean.User;
import com.yinhe.service.UserService;
import com.yinhe.utils.MailUtils;
import com.yinhe.utils.UUIDutils;
public class UserServlet extends BaseServlet {
private UserService us=new UserService();
public void add(HttpServletRequest req, HttpServletResponse resp) throws IllegalAccessException, InvocationTargetException, IOException {
// 封装user对象
User user=new User();
Map<String, String[]> properties=req.getParameterMap();
// 自定义类型转换器
// 参数1:类型转换器
// 参数2:要转换至的类型
ConvertUtils.register(new Converter() {
@Override
// 参数一:要转换成的类型 arg0
// 参数二:要转换的对象 arg1
public Object convert(Class arg0, Object arg1) {
// TODO Auto-generated method stub
// 将字符串转data
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
try {
return sdf.parse(arg1.toString());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}, Date.class);
// 封装属性
BeanUtils.populate(user, properties);
user.setUid(UUIDutils.getUUID());
boolean res=us.add(user);
//页面提醒
String emailMsg = "恭喜您注册成功,请点击下面的连接进行激活账户"
+ "<a href='http://localhost:8080/ShopStore/login.jsp'>"+"http://localhost:8080/ShopStore/</a>";
if (res) {
try {
MailUtils.sendMail(user.getEmail(), emailMsg);
resp.sendRedirect("/ShopStore/registerSuccess.jsp");
} catch (Exception e) {
// TODO Auto-generated catch block
resp.sendRedirect("/ShopStore/registerFail.jsp");
}
}else{
resp.sendRedirect("/ShopStore/login.jsp");
}
}
}
要成功发送邮件,就必须要用 MailUtils 这个工具类,可以说是 Javamail 的核心,还需要一个 jar 包哦 cd52f0e18a0e44049cf665bbb0ca1d4c-mail.jar
工具类代码
import java.util.Properties;
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.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
public class MailUtils {
public static void sendMail(String email, String emailMsg)
throws AddressException, MessagingException {
// 1.创建一个程序与邮件服务器会话对象 Session
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "SMTP");
props.setProperty("mail.host", "smtp.163.com");
props.setProperty("mail.smtp.auth", "true");// 指定验证为true
// 创建验证器
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("你的邮箱@163.com", "安全码");
}
};
Session session = Session.getInstance(props, auth);
// 2.创建一个Message,它相当于是邮件内容
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("你的邮箱@163.com")); // 设置发送者
message.setRecipient(RecipientType.TO, new InternetAddress(email)); // 设置发送方式与接收者
message.setSubject("用户激活");
// message.setText("这是一封激活邮件,请<a href='#'>点击</a>");
message.setContent(emailMsg, "text/html;charset=utf-8");
// 3.创建 Transport用于将邮件发送
Transport.send(message);
}
}
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于