• SSH项目里面 忘记密码的邮件发送功能


      1 package com.xxx.util;
      2 
      3 import java.util.Date;
      4 import java.util.Properties;
      5 
      6 import javax.mail.Address;
      7 import javax.mail.BodyPart;
      8 import javax.mail.Message;
      9 import javax.mail.MessagingException;
     10 import javax.mail.Multipart;
     11 import javax.mail.Session;
     12 import javax.mail.Transport;
     13 import javax.mail.internet.InternetAddress;
     14 import javax.mail.internet.MimeBodyPart;
     15 import javax.mail.internet.MimeMessage;
     16 import javax.mail.internet.MimeMultipart;
     17 
     18 public class SimpleMailSender {
     19     public boolean sendTextMail(MailSenderInfo mailInfo) {   
     20           // 判断是否需要身份认证   
     21           MyAuthenticator authenticator = null;   
     22           Properties pro = mailInfo.getProperties();  
     23           if (mailInfo.isValidate()) {   
     24           // 如果需要身份认证,则创建一个密码验证器   
     25             authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());   
     26           }  
     27           // 根据邮件会话属性和密码验证器构造一个发送邮件的session   
     28           Session sendMailSession = Session.getDefaultInstance(pro,authenticator);   
     29           try {   
     30           // 根据session创建一个邮件消息   
     31           Message mailMessage = new MimeMessage(sendMailSession);   
     32           // 创建邮件发送者地址   
     33           Address from = new InternetAddress(mailInfo.getFromAddress());   
     34           // 设置邮件消息的发送者   
     35           mailMessage.setFrom(from);   
     36           // 创建邮件的接收者地址,并设置到邮件消息中   
     37           Address to = new InternetAddress(mailInfo.getToAddress());   
     38           mailMessage.setRecipient(Message.RecipientType.TO,to);   
     39           // 设置邮件消息的主题   
     40           mailMessage.setSubject(mailInfo.getSubject());   
     41           // 设置邮件消息发送的时间   
     42           mailMessage.setSentDate(new Date());   
     43           // 设置邮件消息的主要内容   
     44           String mailContent = mailInfo.getContent();   
     45           mailMessage.setText(mailContent);   
     46           // 发送邮件   
     47           Transport.send(mailMessage);  
     48           return true;   
     49           } catch (MessagingException ex) {   
     50               ex.printStackTrace();   
     51           }   
     52           return false;   
     53         }   
     54           
     55         /**  
     56           * 以HTML格式发送邮件  
     57           * @param mailInfo 待发送的邮件信息  
     58           */   
     59         public static boolean sendHtmlMail(MailSenderInfo mailInfo){   
     60           // 判断是否需要身份认证   
     61           MyAuthenticator authenticator = null;  
     62           Properties pro = mailInfo.getProperties();  
     63           //如果需要身份认证,则创建一个密码验证器    
     64           if (mailInfo.isValidate()) {   
     65             authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());  
     66           }   
     67           // 根据邮件会话属性和密码验证器构造一个发送邮件的session   
     68           Session sendMailSession = Session.getDefaultInstance(pro,authenticator);   
     69           try {   
     70           // 根据session创建一个邮件消息   
     71           Message mailMessage = new MimeMessage(sendMailSession);   
     72           // 创建邮件发送者地址   
     73           Address from = new InternetAddress(mailInfo.getFromAddress());   
     74           // 设置邮件消息的发送者   
     75           mailMessage.setFrom(from);   
     76           // 创建邮件的接收者地址,并设置到邮件消息中   
     77           Address to = new InternetAddress(mailInfo.getToAddress());   
     78           // Message.RecipientType.TO属性表示接收者的类型为TO   
     79           mailMessage.setRecipient(Message.RecipientType.TO,to);   
     80           // 设置邮件消息的主题   
     81           mailMessage.setSubject(mailInfo.getSubject());   
     82           // 设置邮件消息发送的时间   
     83           mailMessage.setSentDate(new Date());   
     84           // MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象   
     85           Multipart mainPart = new MimeMultipart();   
     86           // 创建一个包含HTML内容的MimeBodyPart   
     87           BodyPart html = new MimeBodyPart();   
     88           // 设置HTML内容   
     89           html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");   
     90           mainPart.addBodyPart(html);   
     91           // 将MiniMultipart对象设置为邮件内容   
     92           mailMessage.setContent(mainPart);   
     93           // 发送邮件   
     94           Transport.send(mailMessage);   
     95           return true;   
     96           } catch (MessagingException ex) {   
     97               ex.printStackTrace();   
     98           }   
     99           return false;   
    100         }   
    101 }
      1 package com.xxx.util;
      2 
      3 import java.util.Properties;
      4 
      5 public class MailSenderInfo {   
      6       
      7     // 发送邮件的服务器的IP和端口   
      8     private String mailServerHost;
      9     private String mailServerPort = "25";   
     10       
     11     // 邮件发送者的地址   
     12     private String fromAddress;   
     13       
     14     // 邮件接收者的地址   
     15     private String toAddress;   
     16       
     17     // 登陆邮件发送服务器的用户名和密码   
     18     private String userName;   
     19     private String password;   
     20       
     21     // 是否需要身份验证   
     22     private boolean validate = false;   
     23       
     24     // 邮件主题   
     25     private String subject;   
     26       
     27     // 邮件的文本内容   
     28     private String content;   
     29       
     30     // 邮件附件的文件名   
     31     private String[] attachFileNames;     
     32     /**  
     33       * 获得邮件会话属性  
     34       */   
     35     public Properties getProperties(){   
     36       Properties p = new Properties();   
     37       p.put("mail.smtp.host", this.mailServerHost);   
     38       p.put("mail.smtp.port", this.mailServerPort);   
     39       p.put("mail.smtp.auth", validate ? "true" : "false");   
     40       return p;   
     41     }   
     42     public String getMailServerHost() {   
     43       return mailServerHost;   
     44     }   
     45     public void setMailServerHost(String mailServerHost) {   
     46       this.mailServerHost = mailServerHost;   
     47     }  
     48     public String getMailServerPort() {   
     49       return mailServerPort;   
     50     }  
     51     public void setMailServerPort(String mailServerPort) {   
     52       this.mailServerPort = mailServerPort;   
     53     }  
     54     public boolean isValidate() {   
     55       return validate;   
     56     }  
     57     public void setValidate(boolean validate) {   
     58       this.validate = validate;   
     59     }  
     60     public String[] getAttachFileNames() {   
     61       return attachFileNames;   
     62     }  
     63     public void setAttachFileNames(String[] fileNames) {   
     64       this.attachFileNames = fileNames;   
     65     }  
     66     public String getFromAddress() {   
     67       return fromAddress;   
     68     }   
     69     public void setFromAddress(String fromAddress) {   
     70       this.fromAddress = fromAddress;   
     71     }  
     72     public String getPassword() {   
     73       return password;   
     74     }  
     75     public void setPassword(String password) {
     76       this.password = password;
     77     }  
     78     public String getToAddress() {
     79       return toAddress;
     80     }   
     81     public void setToAddress(String toAddress) {
     82       this.toAddress = toAddress;
     83     }
     84     public String getUserName() {
     85       return userName;
     86     }
     87     public void setUserName(String userName) {
     88       this.userName = userName;
     89     }
     90     public String getSubject() {
     91       return subject;   
     92     }  
     93     public void setSubject(String subject) {   
     94       this.subject = subject;   
     95     }  
     96     public String getContent() {   
     97       return content;   
     98     }  
     99     public void setContent(String textContent) {   
    100       this.content = textContent;   
    101     }   
    102 }

    以上是两个工具类,下面是action里面的使用

     1     //找回密码
     2     public String findpwd(){
     3         try {
     4             boolean result = userService.checkEmail(item.getEmail());
     5             int randNum= (int) (((Math.random()+1)*1000000));
     6             if (result) {
     7                 MailSenderInfo mailInfo = new MailSenderInfo();
     8                 mailInfo.setMailServerHost("smtp.126.com");
     9                 mailInfo.setMailServerPort("25");
    10                 mailInfo.setValidate(true);
    11                 mailInfo.setUserName("chuguozhan@126.com");
    12                 mailInfo.setPassword("60514895a");//您的邮箱密码
    13                 mailInfo.setFromAddress("chuguozhan@126.com");
    14                 mailInfo.setToAddress(item.getEmail());
    15                 mailInfo.setSubject("密码找回:");
    16                 mailInfo.setContent("您的重置密码是:"+String.valueOf(randNum));
    17                 
    18                 SimpleMailSender sms = new SimpleMailSender();
    19                 if(sms.sendHtmlMail(mailInfo)){
    20                     User user = userService.getUserByEmail(item.getEmail());
    21                     user.setPassword(String.valueOf(randNum));
    22                     userService.doModifyPwd(user);
    23                     log.info("findpwd success!!!");
    24                     resultMap.put("retcode", RetCode.SUCCESS);
    25                     resultMap.put("retmsg", "重置密码已发送到了您的注册邮箱");
    26                 }else{
    27                     log.info("findpwd fail!!!");
    28                     resultMap.put("retcode", RetCode.FAIL);
    29                     resultMap.put("retmsg", "密码找回失败");
    30                 }
    31             }else{
    32                 log.info("checkPwd fail!!!");
    33                 resultMap.put("retcode", RetCode.CHECK);
    34                 resultMap.put("retmsg", "邮箱不存在,请确认您输入的邮箱是否正确!");
    35             }
    36         } catch (Exception e) {
    37             log.error("findpwd is bug",e);
    38             resultMap.put("retcode", RetCode.UNKOWN_WRONG);
    39             resultMap.put("retmsg", "密码找回出现错误!");
    40         }
    41         return SUCCESS;
    42     }

    Dao层和Service层功能实现就不贴了

  • 相关阅读:
    列表导航栏实例(01)
    点阵图形的高效旋转算法
    7种健康食物助抗初秋干燥
    打造自己的reset.css
    如何点击链接直接跳转到app store指定应用下载页面
    【iOSiap防护】验证用户付费收据!拒绝iap Cracker!拒绝iap Free!让iphone越狱用户无从下手!【2012年5月2日更新防护iap Free的方法】
    如何自定义协议从自己的一个app打开另一个app
    传统网站与Web标准——DIV+CSS布局实例
    ANSI、Unicode、Unicode big endian、UTF8编码
    良好的XHTML规则
  • 原文地址:https://www.cnblogs.com/heyuxiu/p/5784104.html
Copyright © 2020-2023  润新知