Java implements sending mail (configurable) forgot password, send mail

Those who have learned the basics of Java should know that there is an email in Java. If you are not familiar with it, you can simply review it.

This article regards sending emails as a configurable and configurable file, which is convenient for future maintenance

1. Maven dependency package (jar package that is dependent on sending emails)

                        <!--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>                 

Second, set the configurable file

   New .properties file

  Configured as follows

##Enable debug debugging
#mail.debug=true
## Sending server requires authentication
mail.smtp.auth=true
##Set the mail server hostname
mail.host=smtp.163.com
## Send mail protocol name
mail.transport.protocol=smtp


##link address
url=127.0.0.1:8089
##Email account password
mail.username=xxxx
mail.password=xxxx

3. Send mail

@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 (!"Super Admin".equals(roleName) && "Super Admin" .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 { 
     //Pass in the email to be sent, user id username sendEmil(user.getEmail(),user.getId(),user.getTrueName()); }
catch (MessagingException e) { e.printStackTrace (); } if (1 == flag) { result.setSuccess(true); } else { result.setSuccess(false); } return result; }   /** * Reset password to send email * @throws MessagingException */ public void sendEmil(String Femail,String id,String userName) throws MessagingException, IOException { Properties props = new Properties();      // Read configuration file props.load( this .getClass().getResourceAsStream("/mailConfig.properties" )); Session session = Session.getInstance(props); // Create a mail object Message msg = new MimeMessage(session); try { msg.setSubject( "Change password" ); } catch (MessagingException e) { // TODO Auto-generated catch block e.printStackTrace(); }     // set the email content String msgContent = "Dear User" + userName + " Hello, <br/><br/>" + "You are" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format( new Date()) + "Submit a password reset request.<br/><br/>" + "Please open the following link to reset your password:<br/><br/>" + "<a href="+props.getProperty("url")+"/user/resetPwd?id="+id+">http://"+props.getProperty("url")+"/user/resetPwd?id="+id+"</a><br/><br/>" + "Thank you for using this system." + "<br/><br/>" + "This is an automatic email, please do not reply directly!" ; msg.setContent(msgContent, "text/html;charset=utf-8"); // Set the email content, html format // Set the sender msg.setFrom( new InternetAddress(MimeUtility.encodeText("Modify password") + " <"+ props.getProperty("mail.username")+">")); // Set the mail source Transport transport = session.getTransport(); // Connect the mail server transport.connect(props.getProperty(" mail.username"), props.getProperty("mail.password" )); // send mail transport.sendMessage(msg, new Address[] { new InternetAddress(Femail)}); // close the connection transport.close(); }

4. This example uses NetEase mailbox. To complete sending emails, you need to complete the last step. Open POP3 and take NetEase mailbox as an example.

get authorization code

end

 

If there is any reprint, please contact [email protected]

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325573145&siteId=291194637