zoukankan      html  css  js  c++  java
  • 学习---邮件激活

    2019/10/21

        项目--网上商城 SSH(Struts+Spring+Hinberate) 模块:注册邮件激活模块   在没有联网的情况下,使用MailServer服务器+FoxMail客户端,实现邮箱账号发送邮件

         使用工具:MailServer 邮件服务器+Foxmail客户端6.5

     过程:用户填写注册表单,随机分配注册码Code ,通过给用户邮箱发送邮件,用户点击链接进行激活,从而完成注册

      1.MailServer 邮件服务器 的配置:(易邮件服务器),最好安装到C盘

        服务器设置 域名   单域名 xxx.com

        账号-->新建账号  (将其中的一个账号,在后期编码中设置为官方发送验证的邮箱)

      2.FoxMail客户端配置

       在没有联网的情况下,填写已经在MailServer中注册的账号,使用该账号来接受验证邮件

        邮箱-->新建邮箱账户   2.!!!将其中的服务器和客户端的地址修改为localhost!!

     

     编码

      MailUtils.java

    	
        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("system@shop.com", "123");  //修改为系统官方的邮箱账号
    			}
    			
    		});
    		// 2.创建邮件对象:
    		Message message = new MimeMessage(session);
    		// 设置发件人:
    		try {
    			message.setFrom(new InternetAddress("system@shop.com"));   //注意修改为自己需要的官方邮箱
    			// 设置收件人:
    			message.addRecipient(RecipientType.TO, new InternetAddress(to));
    			// 抄送 CC   密送BCC
    			// 设置标题
    			message.setSubject("来自XXXX官方激活邮件");
    			// 设置邮件正文:
    			message.setContent("<h1>XXXX官方激活邮件!点下面链接完成激活操作!</h1><h3><a href='http://127.0.0.1:8083/shop/user_active.action?code="+code+"'>http://127.0.0.1:8083/shop/user_active.action?code="+code+"</a></h3>", "text/html;charset=UTF-8");
    			// 3.发送邮件:                                        //a标签中的路径,要根据自身情况来写
    			Transport.send(message);
    		} catch (AddressException e) {
    			e.printStackTrace();
    		} catch (MessagingException e) {
    			e.printStackTrace();
    		}
    		
    	}
    	//测试代码
    	public static void main(String[] args) {
    		sendMail("aaa@shop.com","11111111111111");
    	}
    

      

     //修改用户数据库中激活的状态(0|1)

    /**
    * 用户激活的方法
    */
    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";
    }

     

     

     state为状态:0/1  0表示还未激活,code表示激活码,当用户注册还没有激活时候随机产生code,但是当用户完成激活之后,code变为NULL

  • 相关阅读:
    iPhone开发应用Sqlite使用手册
    2.23 Apps must follow the iOS Data Storage Guidelines or they will be rejected
    跨浏览器(IE/FF/OPERA)JS代码小结
    c#一次数据库查询,JS实现内容分页
    oracle PLSQL /sqlserver2005基本操作对比
    SqlParameter构造函数的临界边缘
    SQL SERVER 2005分页存储过程
    *自创*可变长度随机数字/字母的生成小结(针对文件上传及验证码)
    Visual Source Safe连接数据文件图解 解决密码缓存问题
    [Ubuntu] Invalid command 'VirtualDocumentRoot', perhaps misspelled or defined by a module not included in the server configuration
  • 原文地址:https://www.cnblogs.com/who-am-i/p/11715652.html
Copyright © 2011-2022 走看看