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

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

发送邮件的类:

  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> 
package com.guang.utils;

import java.io.PrintWriter;
import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.servlet.http.HttpServlet;


public class SendEmail {
	
	public void send(String to){
	
		
		
		
		String subject="东京网上商城邮箱验证提醒";
		String content="感谢您于"+timeutil.getnowdate()+"在东京商城注册,复制以下链接,即可完成安全验证:"
		+"http://10.5.116.117/e_shop/registbyEmail.action?checkemail="+to	
		
		+"为保障您的帐号安全,请在24小时内点击该链接,您也可以将链接复制到浏览器地址栏访问。"+
"若您没有申请过验证邮箱 ,请您忽略此邮件,由此给您带来的不便请谅解。";
		
		
		  String host = "smtp.163.com";      //发件人使用发邮件的电子信箱服务器 
	      
		  String from = "kobe_guang@163.com";     //发邮件的出发地(发件人的信箱) 
	      
		  Properties props = System.getProperties(); 
	      // 设置邮件服务器 
	      props.put("mail.smtp.host", host); 
	      // 取得session 
	      props.put("mail.smtp.auth", "true"); //这样才能通过验证 
	      MyAuthenticator myauth = new MyAuthenticator("kobe_guang@163.com", "4515079"); 
	      Session session = Session.getDefaultInstance(props, myauth); 
	     // session.setDebug(true); 
	      // 定义message 
	      MimeMessage message = new MimeMessage(session); 
	      try{
	      
	      // 设定发送邮件的地址 
	      message.setFrom(new InternetAddress(from)); 
	      // 设定接受邮件的地址 
	      message.addRecipient(Message.RecipientType.TO, 
	        new InternetAddress(to)); 
	      // 设定邮件主题
	      message.setSubject(subject); 
	      // 设定邮件内容
	      BodyPart mdp=new MimeBodyPart();//新建一个存放信件内容的BodyPart对象
	      mdp.setContent(content,"text/html;charset=gbk");//给BodyPart对象设置内容和格式/编码方式
	      Multipart mm=new MimeMultipart();//新建一个MimeMultipart对象用来存放BodyPart对
	     //象(事实上可以存放多个)
	     mm.addBodyPart(mdp);//将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)
	     message.setContent(mm);//把mm作为消息对象的内容
	     //   message.setText(content); 
	      message.saveChanges(); 
	      Transport.send(message);  
	      }catch(Exception e){
	    	  
	    	  e.printStackTrace();
	      }
	      
	   }

	
}



class MyAuthenticator  extends javax.mail.Authenticator {
			private String strUser;
			private String strPwd;
			public MyAuthenticator(String user, String password) {
				this.strUser = user;
				this.strPwd = password;
			}

			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(strUser, strPwd);
			}
}


第一个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 FinishregisterActionextends ActionSupport { 
  9.  
  10.  
  11.     /**
  12.      *
  13.      */ 
  14.     private staticfinal 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.      
package com.guang.action;

import com.guang.model.User;
import com.guang.service.UserService;
import com.guang.utils.UserStatus;
import com.opensymphony.xwork2.ActionSupport;

public class FinishregisterAction extends ActionSupport {


	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String checkemail;
	

	public String getCheckemail() {
		return checkemail;
	}

	public void setCheckemail(String checkemail) {
		this.checkemail = checkemail;
	}

	UserService userService;

	public UserService getUserService() {
		return userService;
	}
	


	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	
	@Override
	public String execute() throws Exception {

		System.out.println("checkemail="+checkemail);
	
		User user=userService.getUniqueResult(checkemail);
	
		user.setStatus(UserStatus.YES);
		
		this.userService.modify(user);
	
	
		return SUCCESS;
	
	}
	
}

注册的action:


  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> 
package com.guang.action;


import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.components.FieldError;

import com.guang.model.ShoppingCart;
import com.guang.model.User;
import com.guang.service.UserService;
import com.guang.service.impl.UserServiceImpl;
import com.guang.utils.UserStatus;
import com.guang.utils.timeutil;
import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport {

	private String repassword;
	
	private User user;

	UserService userService;

	@Override
	public String execute() throws Exception {
		
		//注册时间
		user.setRegisterdate(timeutil.getnowdate());
		
		//初始时,没有邮箱验证,所以设为NO
		user.setStatus(UserStatus.NO);
		
		
		ShoppingCart scart=new ShoppingCart();
		
		scart.setCartitemid(3);
		scart.setUser(user);
		user.setShoopingcart(scart);
		userService.addShoppingCart(scart);
		userService.addUser(user);
	
		return SUCCESS; 
	}
	
	public String getRepassword() {
		return repassword;
	}
	

	public User getUser() {
		return user;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}
	
	

	public void setUser(User user) {
		this.user = user;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	@Override
	public void validate() {

		if(this.user.getPassword().equals(this.repassword)){
			
		}else{
			
			addFieldError("reg_error", "两次密码输入不一致,请重新输入!");
		}
	}
	
	
	
	
}

对应方法调用不列出了。

struts.xml里面的配置:

  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.      
<action name="register" class="RegisterAction">
		<result name="success" type="redirect">/qiantai/go_check.jsp</result>
		<result name="input">/qiantai/register.jsp</result>
	</action>
	
	<action name="listAllUsers" class="ListAllUsers">
		<result name="success">/list.jsp</result>
	</action>
	
	<action name="imageCode" class="com.guang.action.ImageCodeAction">
		  <result name="success" type="stream"> 
                <param name="inputName">imageStream</param> 
                <param name="bufferSize">2048</param> 
            </result> 
	</action>
	
	<action name="checkcode" class="com.guang.action.ImageCodeAction" method="checkCode">
		<result></result>
	</action>
	
	
	<action name="checkemail" class="checkemail" >
		<result></result>
	</action>
	
	<action name="login" class="login_">
		<result name="success">/qiantai/goodslist.jsp</result>
		<result name="input">/qiantai/login.jsp</result>
	</action>
	
	<action name="go_register" class="com.guang.action.MiddleAction">
		<result name="success">/qiantai/register.jsp</result>
	</action>
	
	<action name="go_login" class="com.guang.action.MiddleAction" method="login">
		<result name="login">/qiantai/login.jsp</result>
	</action>
	
	
	<action name="registbyEmail" class="finishregister">
		<result name="success" type="redirect" >/qiantai/registerbyEmail.jsp</result>
	</action>
	
	

spring的配置文件(部分):

  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> 
<bean id="userservice" class="com.guang.service.impl.UserServiceImpl">
	<property name="userdao" ref="userdao"></property>
	<property name="sendemails" ref="sendmail"></property>
</bean> 

<bean id="RegisterAction" class="com.guang.action.RegisterAction" scope="prototype">
	<property name="userService" ref="userservice"></property>
</bean>


<bean id="ListAllUsers" class="com.guang.action.UserAction" scope="prototype">
	<property name="userService" ref="userservice"></property>
</bean>

<bean id="checkemail" class="com.guang.action.checkEmailAction" scope="prototype">
	<property name="userService" ref="userservice"></property>
</bean>

<bean id="login_" class="com.guang.action.LoginAction" scope="prototype">
	<property name="userService" ref="userservice"></property>
</bean>

<bean id="finishregister" class="com.guang.action.FinishregisterAction" scope="prototype">
	<property name="userService" ref="userservice"></property>
</bean>

基本完成了。

希望对你有帮助。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值