Java——使用者啟用郵件工具類,java郵件工具類

來源:互聯網
上載者:User

Java——使用者啟用郵件工具類,java郵件工具類

  我們經常遇到在網站或者軟體註冊新使用者時需要向我們的註冊郵箱發送一封啟用郵件,然後我們去郵箱點擊啟用串連後我們的使用者名稱才能登陸,其過程是當我們註冊成功後資料庫已經存入該使用者的相關資訊,但是使用者狀態為不可用,所以這時候該使用者名稱是不能正常使用的。因此系統需要向我們的註冊郵箱發一封啟用郵件,我們點擊啟用串連後系統會將資料庫中使用者狀態欄位更改為可用狀態,至此使用者啟用成功,該使用者可以正常使用。下面是實現過程:

  為了方便起見我們還是編寫一個發送郵箱工具類。

<span style="font-family:KaiTi_GB2312;font-size:18px;color:#333333;">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() {@Overrideprotected 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");}}</span>
Action:

<span style="font-family:KaiTi_GB2312;font-size:18px;color:#333333;">/** *  * 使用者註冊 */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";}</span>

Service:

<span style="font-family:KaiTi_GB2312;font-size:18px;color:#333333;">// 業務層完成使用者註冊代碼: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);}</span>

Dao:

<span style="font-family:KaiTi_GB2312;font-size:18px;color:#333333;">public void save(User user) {// TODO Auto-generated method stubthis.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);}</span>
註冊成功後啟用之前資料庫

啟用之後資料庫

因為我們只希望使用者啟用一次,所以啟用成功後將資料的code欄位清空,state改為1。目前系統沒有上線,為了方便測試,我用的本地郵箱測試,僅支援區域網路,需要外網的請參見部落格http://blog.csdn.net/huyuyang6688/article/details/48031347,以上內容有不足之處請大家批評指正,謝謝!

著作權聲明:本文為博主原創文章,轉載請標明出處,謝謝!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.