在servlet 中写的方法
/**
* 发送激活邮件
* @param recipients
* @param subject
* @param html
* @return
*/
public static boolean sendMail(String recipients(收件人), String subject(主题), String html(内容)) {
try {
// 1.创建配置信息类
Properties props = new Properties();
props.put("mail.smtp.port", 25);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.163.com");
// 2.创建连接
Session session = Session.getDefaultInstance(props, null);
session.setDebug(false); // 是否调试,true为启用调试,会在控制台打印调试信息,改为false不启用
Transport transport = session.getTransport("smtp");
transport.connect("smtp.163.com", "发件箱", "密码");
//3.设置邮件信息
MimeMessage msg = new MimeMessage(session);
// 主题
msg.setSubject(subject);
// 时间
msg.setSentDate(new Date());
// 发件人
msg.setFrom(new InternetAddress("发件箱"));
// 收件人
msg.setRecipients(Message.RecipientType.TO, recipients);
msg.setContent(html, "text/html;charset=UTF-8"); // html字符格式
msg.saveChanges();
transport.sendMessage(msg, msg.getAllRecipients());
// 4.关闭Transport连接
transport.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 注册新用户
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
public void zhuce(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String code=(String) req.getSession().getAttribute("code");
String getcode=req.getParameter("yanzhengma");
String name=req.getParameter("name");
String pwd=req.getParameter("pwd");
String email=req.getParameter("email");
User user=new User();
if(getcode.equalsIgnoreCase(code)){
user.setUname(name);
user.setPwd(jiami(pwd) );
user.setEmail(email);
//激活码,获取一个十位的激活码
String jihuoma="";
String str="abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLLMNOPQRSTUVWXYZ0123456789";
for(int i=1;i<=10;i++){
int a=(int) Math.floor(Math.random()*(str.length()-1-0+1));
jihuoma+=str.charAt(a);
}
user.setJihuoma(jihuoma);
user.setStatu(1);
int rst=us.addUser(user);
if(rst!=0){
String title="论坛帐号激活验证";
String jihuo="请点击以下链接进行激活: