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

来源:互联网 发布:刷钻软件免费版 编辑:程序博客网 时间:2024/06/05 17:30

最近在做一个项目,需要发送邮件到注册邮箱,并且点击链接之后方可完成注册。直接上代码:

发送邮件的类:

[java] view plaincopyprint?
  1. <span style="color: rgb(51, 51, 51);">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. class MyAuthenticator  extends javax.mail.Authenticator { 
  79.             private String strUser; 
  80.             private String strPwd; 
  81.             public MyAuthenticator(String user, String password) { 
  82.                 this.strUser = user; 
  83.                 this.strPwd = password; 
  84.             } 
  85.  
  86.             protected PasswordAuthentication getPasswordAuthentication() { 
  87.                 return new PasswordAuthentication(strUser, strPwd); 
  88.             } 
  89. </span><span style="color: rgb(255, 102, 102);"
  90. </span> 

第一个action:用于在点击链接之后修改用户的状态。

[java] view plaincopyprint?
  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 FinishregisterActionextends ActionSupport { 
  9.  
  10.  
  11.     /**
  12.      *
  13.      */ 
  14.     private staticfinallong 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.      

注册的action:


[java] view plaincopyprint?
  1. <span style="color: rgb(51, 51, 51);">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 RegisterActionextends 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. </span> 

对应方法调用不列出了。

struts.xml里面的配置:

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

spring的配置文件(部分):

[html] view plaincopyprint?
  1. <beanid="userservice"class="com.guang.service.impl.UserServiceImpl"> 
  2.     <propertyname="userdao"ref="userdao"></property> 
  3.     <propertyname="sendemails"ref="sendmail"></property> 
  4. </bean>  
  5.  
  6. <bean id="RegisterAction"class="com.guang.action.RegisterAction"scope="prototype"> 
  7.     <propertyname="userService"ref="userservice"></property> 
  8. </bean> 
  9.  
  10.  
  11. <beanid="ListAllUsers"class="com.guang.action.UserAction"scope="prototype"> 
  12.     <propertyname="userService"ref="userservice"></property> 
  13. </bean> 
  14.  
  15. <beanid="checkemail"class="com.guang.action.checkEmailAction"scope="prototype"> 
  16.     <propertyname="userService"ref="userservice"></property> 
  17. </bean> 
  18.  
  19. <beanid="login_"class="com.guang.action.LoginAction"scope="prototype"> 
  20.     <propertyname="userService"ref="userservice"></property> 
  21. </bean> 
  22.  
  23. <beanid="finishregister"class="com.guang.action.FinishregisterAction"scope="prototype"> 
  24.     <propertyname="userService"ref="userservice"></property> 
  25. </bean> 

基本完成了。

希望对你有帮助。

原创粉丝点击