Java實現發送郵件(可配置)忘記密碼,發送郵件


學過Java基礎的應該知道Java里有郵件這一塊,不熟悉的話可以簡單復習一下

本文章把發送郵件做為可配置可配置文件,這樣方便以后維護

一、Maven依賴包 (發送郵件所依賴的jar包)

                        <!--mail-->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>                 

二、設置可配置文件

   新建.properties文件

  配置人如下

##開啟debug調試
#mail.debug=true
## 發送服務器需要身份驗證
mail.smtp.auth=true
##設置郵件服務器主機名
mail.host=smtp.163.com
##發送郵件協議名稱
mail.transport.protocol=smtp


##鏈接地址
url=127.0.0.1:8089
##郵箱賬號密碼
mail.username=xxxx
mail.password=xxxx

三、發送郵件

@PostMapping("/updateResetPwd")
@ResponseBody
public JsonResult updateResetPwd(@RequestBody User input,HttpServletRequest request) throws IOException {
   JsonResult result = new JsonResult();
   String roleName = (String) request.getSession().getAttribute("roleName");
   if(!"超級管理員".equals(roleName) && "超級管理員".equals(input.getRoleName())){
      result.setMsg("supermana");
        return result;
   }
    input.setPassword(PasswordUtils.passwordEncrypt("123456"));
    User user = userService.selectById(input.getId());
    Integer flag = userService.updateResetPassword(input);

    try {
     //傳入要發送的郵箱,用戶id用戶名 sendEmil(user.getEmail(),user.getId(),user.getTrueName()); }
catch (MessagingException e) { e.printStackTrace(); } if (1 == flag) { result.setSuccess(true); } else { result.setSuccess(false); } return result; }   /** * 重置密碼發送郵件 * @throws MessagingException */ public void sendEmil(String Femail,String id,String userName) throws MessagingException, IOException { Properties props = new Properties();      // 讀取配置文件 props.load(this.getClass().getResourceAsStream("/mailConfig.properties")); Session session = Session.getInstance(props); // 創建郵件對象 Message msg = new MimeMessage(session); try { msg.setSubject("修改密碼"); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); }     // 設置郵件內容 String msgContent = "親愛的用戶 " + userName + " ,您好,<br/><br/>" + "您在" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "提交重置密碼的請求。<br/><br/>" + "請打開以下鏈接重置密碼:<br/><br/>" + "<a href="+props.getProperty("url")+"/user/resetPwd?id="+id+">http://"+props.getProperty("url")+"/user/resetPwd?id="+id+"</a><br/><br/>" + "感謝使用本系統。" + "<br/><br/>" + "此為自動發送郵件,請勿直接回復!"; msg.setContent(msgContent, "text/html;charset=utf-8");// 設置郵件內容,為html格式 // 設置發件人 msg.setFrom(new InternetAddress(MimeUtility.encodeText("修改密碼") + " <"+ props.getProperty("mail.username")+">"));// 設置郵件來源 Transport transport = session.getTransport(); // 連接郵件服務器 transport.connect(props.getProperty("mail.username"), props.getProperty("mail.password")); // 發送郵件 transport.sendMessage(msg, new Address[] {new InternetAddress(Femail)}); // 關閉連接 transport.close(); }

四、本例子使用網易郵箱,要完成發送郵件,還需要完成最后一步,開啟POP3以網易郵箱為例

獲得授權碼

結束

 

如有轉載,請聯系243661038@qq.com

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM