ssh+jmail实现,注册完发送邮件,点击链接之后完成注册。

发送邮件的类:

  1. <span style="color:#333333;">package com.guang.utils;  
  2.   
  3. import java.io.PrintWriter;  
  4. import java.util.Properties;  
  5.   
  6. import javax.mail.BodyPart;  
  7. import javax.mail.Message;  
  8. import javax.mail.Multipart;  
  9. import javax.mail.PasswordAuthentication;  
  10. import javax.mail.Session;  
  11. import javax.mail.Transport;  
  12. import javax.mail.internet.InternetAddress;  
  13. import javax.mail.internet.MimeBodyPart;  
  14. import javax.mail.internet.MimeMessage;  
  15. import javax.mail.internet.MimeMultipart;  
  16. import javax.servlet.http.HttpServlet;  
  17.   
  18.   
  19. public class SendEmail {  
  20.       
  21.     public void send(String to){  
  22.       
  23.           
  24.           
  25.           
  26.         String subject="东京网上商城邮箱验证提醒";  
  27.         String content="感谢您于"+timeutil.getnowdate()+"在东京商城注册,复制以下链接,即可完成安全验证:"  
  28.         +"http://10.5.116.117/e_shop/registbyEmail.action?checkemail="+to     
  29.           
  30.         +"为保障您的帐号安全,请在24小时内点击该链接,您也可以将链接复制到浏览器地址栏访问。"+  
  31. "若您没有申请过验证邮箱 ,请您忽略此邮件,由此给您带来的不便请谅解。";  
  32.           
  33.           
  34.           String host = "smtp.163.com";      //发件人使用发邮件的电子信箱服务器   
  35.             
  36.           String from = "kobe_guang@163.com";     //发邮件的出发地(发件人的信箱)   
  37.             
  38.           Properties props = System.getProperties();   
  39.           // 设置邮件服务器   
  40.           props.put("mail.smtp.host", host);   
  41.           // 取得session   
  42.           props.put("mail.smtp.auth", "true"); //这样才能通过验证   
  43.           MyAuthenticator myauth = new MyAuthenticator("kobe_guang@163.com", "4515079");   
  44.           Session session = Session.getDefaultInstance(props, myauth);   
  45.          // session.setDebug(true);   
  46.           // 定义message   
  47.           MimeMessage message = new MimeMessage(session);   
  48.           try{  
  49.             
  50.           // 设定发送邮件的地址   
  51.           message.setFrom(new InternetAddress(from));   
  52.           // 设定接受邮件的地址   
  53.           message.addRecipient(Message.RecipientType.TO,   
  54.             new InternetAddress(to));   
  55.           // 设定邮件主题  
  56.           message.setSubject(subject);   
  57.           // 设定邮件内容  
  58.           BodyPart mdp=new MimeBodyPart();//新建一个存放信件内容的BodyPart对象  
  59.           mdp.setContent(content,"text/html;charset=gbk");//给BodyPart对象设置内容和格式/编码方式  
  60.           Multipart mm=new MimeMultipart();//新建一个MimeMultipart对象用来存放BodyPart对  
  61.          //象(事实上可以存放多个)  
  62.          mm.addBodyPart(mdp);//将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)  
  63.          message.setContent(mm);//把mm作为消息对象的内容  
  64.          //   message.setText(content);   
  65.           message.saveChanges();   
  66.           Transport.send(message);    
  67.           }catch(Exception e){  
  68.                 
  69.               e.printStackTrace();  
  70.           }  
  71.             
  72.        }  
  73.   
  74.       
  75. }  
  76.   
  77.   
  78.   
  79. class MyAuthenticator  extends javax.mail.Authenticator {  
  80.             private String strUser;  
  81.             private String strPwd;  
  82.             public MyAuthenticator(String user, String password) {  
  83.                 this.strUser = user;  
  84.                 this.strPwd = password;  
  85.             }  
  86.   
  87.             protected PasswordAuthentication getPasswordAuthentication() {  
  88.                 return new PasswordAuthentication(strUser, strPwd);  
  89.             }  
  90. }  
  91. </span><span style="color:#ff6666;">  
  92. </span>  

第一个action:用于在点击链接之后修改用户的状态。
  1. package com.guang.action;  
  2.   
  3. import com.guang.model.User;  
  4. import com.guang.service.UserService;  
  5. import com.guang.utils.UserStatus;  
  6. import com.opensymphony.xwork2.ActionSupport;  
  7.   
  8. public class FinishregisterAction extends ActionSupport {  
  9.   
  10.   
  11.     /** 
  12.      *  
  13.      */  
  14.     private static final long serialVersionUID = 1L;  
  15.     private String checkemail;  
  16.       
  17.   
  18.     public String getCheckemail() {  
  19.         return checkemail;  
  20.     }  
  21.   
  22.     public void setCheckemail(String checkemail) {  
  23.         this.checkemail = checkemail;  
  24.     }  
  25.   
  26.     UserService userService;  
  27.   
  28.     public UserService getUserService() {  
  29.         return userService;  
  30.     }  
  31.       
  32.   
  33.   
  34.     public void setUserService(UserService userService) {  
  35.         this.userService = userService;  
  36.     }  
  37.       
  38.       
  39.     @Override  
  40.     public String execute() throws Exception {  
  41.   
  42.         System.out.println("checkemail="+checkemail);  
  43.       
  44.         User user=userService.getUniqueResult(checkemail);  
  45.       
  46.         user.setStatus(UserStatus.YES);  
  47.           
  48.         this.userService.modify(user);  
  49.       
  50.       
  51.         return SUCCESS;  
  52.       
  53.     }  
  54.       
  55. }  

注册的action:
  1. <span style="color:#333333;">package com.guang.action;  
  2.   
  3.   
  4. import java.io.PrintWriter;  
  5.   
  6. import javax.servlet.http.HttpServletRequest;  
  7. import javax.servlet.http.HttpServletResponse;  
  8.   
  9. import org.apache.struts2.ServletActionContext;  
  10. import org.apache.struts2.components.FieldError;  
  11.   
  12. import com.guang.model.ShoppingCart;  
  13. import com.guang.model.User;  
  14. import com.guang.service.UserService;  
  15. import com.guang.service.impl.UserServiceImpl;  
  16. import com.guang.utils.UserStatus;  
  17. import com.guang.utils.timeutil;  
  18. import com.opensymphony.xwork2.ActionSupport;  
  19.   
  20. public class RegisterAction extends ActionSupport {  
  21.   
  22.     private String repassword;  
  23.       
  24.     private User user;  
  25.   
  26.     UserService userService;  
  27.   
  28.     @Override  
  29.     public String execute() throws Exception {  
  30.           
  31.         //注册时间  
  32.         user.setRegisterdate(timeutil.getnowdate());  
  33.           
  34.         //初始时,没有邮箱验证,所以设为NO  
  35.         user.setStatus(UserStatus.NO);  
  36.           
  37.           
  38.         ShoppingCart scart=new ShoppingCart();  
  39.           
  40.         scart.setCartitemid(3);  
  41.         scart.setUser(user);  
  42.         user.setShoopingcart(scart);  
  43.         userService.addShoppingCart(scart);  
  44.         userService.addUser(user);  
  45.       
  46.         return SUCCESS;   
  47.     }  
  48.       
  49.     public String getRepassword() {  
  50.         return repassword;  
  51.     }  
  52.       
  53.   
  54.     public User getUser() {  
  55.         return user;  
  56.     }  
  57.   
  58.     public UserService getUserService() {  
  59.         return userService;  
  60.     }  
  61.   
  62.     public void setRepassword(String repassword) {  
  63.         this.repassword = repassword;  
  64.     }  
  65.       
  66.       
  67.   
  68.     public void setUser(User user) {  
  69.         this.user = user;  
  70.     }  
  71.   
  72.     public void setUserService(UserService userService) {  
  73.         this.userService = userService;  
  74.     }  
  75.   
  76.     @Override  
  77.     public void validate() {  
  78.   
  79.         if(this.user.getPassword().equals(this.repassword)){  
  80.               
  81.         }else{  
  82.               
  83.             addFieldError("reg_error", "两次密码输入不一致,请重新输入!");  
  84.         }  
  85.     }  
  86.       
  87.       
  88.       
  89.       
  90. }  
  91. </span>  

对应方法调用不列出了。

struts.xml里面的配置

  1. <action name="register" class="RegisterAction">  
  2.         <result name="success" type="redirect">/qiantai/go_check.jsp</result>  
  3.         <result name="input">/qiantai/register.jsp</result>  
  4.     </action>  
  5.       
  6.     <action name="listAllUsers" class="ListAllUsers">  
  7.         <result name="success">/list.jsp</result>  
  8.     </action>  
  9.       
  10.     <action name="imageCode" class="com.guang.action.ImageCodeAction">  
  11.           <result name="success" type="stream">   
  12.                 <param name="inputName">imageStream</param>   
  13.                 <param name="bufferSize">2048</param>   
  14.             </result>   
  15.     </action>  
  16.       
  17.     <action name="checkcode" class="com.guang.action.ImageCodeAction" method="checkCode">  
  18.         <result></result>  
  19.     </action>  
  20.       
  21.       
  22.     <action name="checkemail" class="checkemail" >  
  23.         <result></result>  
  24.     </action>  
  25.       
  26.     <action name="login" class="login_">  
  27.         <result name="success">/qiantai/goodslist.jsp</result>  
  28.         <result name="input">/qiantai/login.jsp</result>  
  29.     </action>  
  30.       
  31.     <action name="go_register" class="com.guang.action.MiddleAction">  
  32.         <result name="success">/qiantai/register.jsp</result>  
  33.     </action>  
  34.       
  35.     <action name="go_login" class="com.guang.action.MiddleAction" method="login">  
  36.         <result name="login">/qiantai/login.jsp</result>  
  37.     </action>  
  38.       
  39.       
  40.     <action name="registbyEmail" class="finishregister">  
  41.         <result name="success" type="redirect" >/qiantai/registerbyEmail.jsp</result>  
  42.     </action>  
  43.       
  44.       

spring的配置文件(部分):
  1. <bean id="userservice" class="com.guang.service.impl.UserServiceImpl">  
  2.     <property name="userdao" ref="userdao"></property>  
  3.     <property name="sendemails" ref="sendmail"></property>  
  4. </bean>   
  5.   
  6. <bean id="RegisterAction" class="com.guang.action.RegisterAction" scope="prototype">  
  7.     <property name="userService" ref="userservice"></property>  
  8. </bean>  
  9.   
  10.   
  11. <bean id="ListAllUsers" class="com.guang.action.UserAction" scope="prototype">  
  12.     <property name="userService" ref="userservice"></property>  
  13. </bean>  
  14.   
  15. <bean id="checkemail" class="com.guang.action.checkEmailAction" scope="prototype">  
  16.     <property name="userService" ref="userservice"></property>  
  17. </bean>  
  18.   
  19. <bean id="login_" class="com.guang.action.LoginAction" scope="prototype">  
  20.     <property name="userService" ref="userservice"></property>  
  21. </bean>  
  22.   
  23. <bean id="finishregister" class="com.guang.action.FinishregisterAction" scope="prototype">  
  24.     <property name="userService" ref="userservice"></property>  
  25. </bean>  

基本完成了。

希望对你有帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值