# Java——用戶激活郵件工具類
我們經常遇到在網站或者軟件注冊新用戶時需要向我們的注冊郵箱發送一封激活郵件,然后我們去郵箱點擊激活連接后我們的用戶名才能登陸,其過程是當我們注冊成功后數據庫已經存入該用戶的相關信息,但是用戶狀態為不可用,所以這時候該用戶名是不能正常使用的。因此系統需要向我們的注冊郵箱發一封激活郵件,我們點擊激活連接后系統會將數據庫中用戶狀態字段更改為可用狀態,至此用戶激活成功,該用戶可以正常使用。下面是實現過程:
為了方便起見我們還是編寫一個發送郵箱工具類。
~~~
package cn.itcast.shop.utils;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
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;
/**
* 郵件發送工具類
* @author shx
*
*/
public class MailUitls {
/**
* 發送郵件的方法
* @param to :收件人
* @param code :激活碼
*/
public static void sendMail(String to,String code){
/**
* 1.獲得一個Session對象.
* 2.創建一個代表郵件的對象Message.
* 3.發送郵件Transport
*/
// 1.獲得連接對象
Properties props = new Properties();
props.setProperty("mail.host", "localhost");
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("service@shop.com", "111");
}
});
// 2.創建郵件對象:
Message message = new MimeMessage(session);
// 設置發件人:
try {
message.setFrom(new InternetAddress("service@shop.com"));
// 設置收件人:
message.addRecipient(RecipientType.TO, new InternetAddress(to));
// 抄送 CC 密送BCC
// 設置標題
message.setSubject("來自官方激活郵件");
// 設置郵件正文:
message.setContent("<h1>官方激活郵件!點下面鏈接完成激活操作!</h1><h3><a href='http://192.168.24.162:8080/shop/user_active.action?code="+code+"'>http://192.168.24.162:8080/shop/user_active.action?code="+code+"</a></h3>", "text/html;charset=UTF-8");
// 3.發送郵件:
Transport.send(message);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
sendMail("aaa@shop.com","11111111111111");
}
}
~~~
Action:
~~~
/**
*
* 用戶注冊
*/
public String regist(){
userService.save(user);
this.addActionMessage("注冊成功,情趣郵箱激活!");
return "msg";
}
/**
* 用戶激活的方法
*/
public String active() {
// 根據激活碼查詢用戶:
User existUser = userService.findByCode(user.getCode());
// 判斷
if (existUser == null) {
// 激活碼錯誤的
this.addActionMessage("激活失敗:激活碼錯誤!");
} else {
// 激活成功
// 修改用戶的狀態
existUser.setState(1);
existUser.setCode(null);
userService.update(existUser);
this.addActionMessage("激活成功:請去登錄!");
}
return "msg";
}
~~~
Service:
~~~
// 業務層完成用戶注冊代碼:
public void save(User user) {
// 將數據存入到數據庫
user.setState(0); // 0:代表用戶未激活. 1:代表用戶已經激活.
String code = UUIDUtils.getUUID()+UUIDUtils.getUUID();//調用隨機ID生成工具
user.setCode(code);
userDao.save(user);
// 發送激活郵件;
MailUitls.sendMail(user.getEmail(), code);
}
// 業務層根據激活碼查詢用戶
public User findByCode(String code) {
return userDao.findByCode(code);
}
// 修改用戶的狀態的方法
public void update(User existUser) {
userDao.update(existUser);
}
~~~
Dao:
~~~
public void save(User user) {
// TODO Auto-generated method stub
this.getHibernateTemplate().save(user);
}
// 根據激活碼查詢用戶
public User findByCode(String code) {
String hql = "from User where code = ?";
List<User> list = this.getHibernateTemplate().find(hql,code);
if(list != null && list.size() > 0){
return list.get(0);
}
return null;
}
// 修改用戶狀態的方法
public void update(User existUser) {
this.getHibernateTemplate().update(existUser);
}
~~~
注冊成功后激活之前數據庫截圖

激活之后數據庫截圖

因為我們只希望用戶激活一次,所以激活成功后將數據的code字段清空,state改為1。目前系統沒有上線,為了方便測試,我用的本地郵箱測試,僅支持局域網,需要外網的請參見博客[http://blog.csdn.net/huyuyang6688/article/details/48031347](http://http//blog.csdn.net/huyuyang6688/article/details/48031347),以上內容有不足之處請大家批評指正,謝謝!