1、开启POP3|SMTP服务
首先登录QQ邮箱>>>登录成功后找到设置>>>然后找到邮箱设置>>>点击账户>>>找到POP3|SMTP服务>>>点击开启(开启需要验证,验证成功后会有一串授权码用于发送邮件使用)>>>验证成功
记下QQ邮箱提示的授权码,这个授权码,就是发送邮件时需要的密码。
2、引入依赖
1 2 3 4
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
|
3、application.yml配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| spring: mail: host: smtp.qq.com username: ****@qq.com password: fe456156fefefee port: 465 default-encoding: UTF-8 properties: mail: smtp: ssl: enable: true debug: true
|
4、编写发送邮件的方法
邮件生成工具类 EmailUtil.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
@Slf4j public class EmailUtil {
public static String buildEmailContent(String emailHtmlPath, String captcha) { ClassPathResource resource = new ClassPathResource(emailHtmlPath); InputStream inputStream = null; BufferedReader fileReader = null; StringBuilder buffer = new StringBuilder(); String line; try { inputStream = resource.getInputStream(); fileReader = new BufferedReader(new InputStreamReader(inputStream)); while ((line = fileReader.readLine()) != null) { buffer.append(line); } } catch (Exception e) { log.info("发送邮件读取模板失败{}", e.getMessage()); } finally { if (fileReader != null) { try { fileReader.close(); } catch (IOException e) { e.printStackTrace(); } } if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return MessageFormat.format(buffer.toString(), captcha, EMAIL_TITLE, EMAIL_TITLE_ENGLISH, PLATFORM_RESPONSIBLE_PERSON, PLATFORM_ADDRESS); }
public static String buildPaySuccessEmailContent(String emailHtmlPath, String orderName, String orderTotal) { ClassPathResource resource = new ClassPathResource(emailHtmlPath); InputStream inputStream = null; BufferedReader fileReader = null; StringBuilder buffer = new StringBuilder(); String line; try { inputStream = resource.getInputStream(); fileReader = new BufferedReader(new InputStreamReader(inputStream)); while ((line = fileReader.readLine()) != null) { buffer.append(line); } } catch (Exception e) { log.info("发送邮件读取模板失败{}", e.getMessage()); } finally { if (fileReader != null) { try { fileReader.close(); } catch (IOException e) { e.printStackTrace(); } } if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return MessageFormat.format(buffer.toString(), orderName, orderTotal, PLATFORM_RESPONSIBLE_PERSON, PATH_ADDRESS, EMAIL_TITLE); }
public void sendPaySuccessEmail(String emailAccount, JavaMailSender mailSender, EmailConfig emailConfig, String orderName, String orderTotal) throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setSubject("【" + EMAIL_TITLE + "】感谢您的购买,请查收您的订单"); helper.setText(buildPaySuccessEmailContent(EMAIL_HTML_PAY_SUCCESS_PATH, orderName, orderTotal), true); helper.setTo(emailAccount); helper.setFrom(EMAIL_TITLE + '<' + emailConfig.getEmailFrom() + '>'); mailSender.send(message); }
}
|
邮件常量 EmailConstant.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
public interface EmailConstant {
String EMAIL_HTML_CONTENT_PATH = "email.html";
String EMAIL_HTML_PAY_SUCCESS_PATH = "pay.html";
String CAPTCHA_CACHE_KEY = "api:captcha:";
String EMAIL_SUBJECT = "验证码邮件";
String EMAIL_TITLE = "LinkSauce-API 接口开放平台";
String EMAIL_TITLE_ENGLISH = "LinkSauce-API Open Interface Platform";
String PLATFORM_RESPONSIBLE_PERSON = "Ohh";
String PLATFORM_ADDRESS = "<a href='https://api.qimuu.icu/'>请联系我们</a>";
String PATH_ADDRESS = "'https://api.qimuu.icu/'"; }
|
邮件配置 EmailConfig.java
1 2 3 4 5 6 7 8 9 10 11 12 13
|
@Configuration @ConfigurationProperties(prefix = "spring.mail") @Data public class EmailConfig { private String emailFrom = "*****@qq.com"; }
|
发送邮件方法 sendEmail
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| private void sendEmail(String emailAccount, String captcha) throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setSubject(EMAIL_SUBJECT); helper.setText(buildEmailContent(EMAIL_HTML_CONTENT_PATH, captcha), true); helper.setTo(emailAccount); helper.setFrom(EMAIL_TITLE + '<' + emailConfig.getEmailFrom() + '>'); mailSender.send(message); }
|
邮件样式模板 email.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta content="IE=edge" http-equiv="X-UA-Compatible"> <meta content="email code" name="description"> <meta content="width=device-width, initial-scale=1" name="viewport"> </head>
<body> <div style="background-color:#ECECEC; padding: 35px;"> <table align="center" cellpadding="0" style="width: 600px;height: 100%; margin: 0px auto; text-align: left; position: relative; border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; font-size: 14px; font-family:微软雅黑, 黑体; line-height: 1.5; box-shadow: rgb(153, 153, 153) 0px 0px 5px; border-collapse: collapse; background-position: initial initial; background-repeat: initial initial;background:#fff;"> <tbody> <tr> <th style="height: 25px; line-height: 25px; padding: 15px 35px; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: RGB(148,0,211); background-color: RGB(148,0,211); border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px;" valign="middle"> <font face="微软雅黑" size="5" style="color: rgb(255, 255, 255); ">{1}</font> </th> </tr> <tr> <td style="word-break:break-all"> <div style="padding:25px 35px 40px; background-color:#fff;opacity:0.8;"> <h2 style="margin: 5px 0px; "> <font color="#333333" style="line-height: 20px; "> <font size="4" style="line-height: 22px; "> 尊敬的用户:</font> </font> </h2> <p>您好!感谢您使用{1},您的账号正在进行邮箱验证,验证码为:<span style="color: '#ff8c00';font-size: 16px;font-weight: bold">{0}</span> ,有效期5分钟,请尽快填写验证码完成验证!</p><br> <h2 style="margin: 5px 0px; "> <font color="#333333" style="line-height: 20px; "> <font size="4" style="line-height: 22px; "> Dear user:</font> </font> </h2> <p>Hello! Thanks for using {2}, your account is being authenticated by email, the verification code is: <span style="color: '#ff8c00';font-size: 16px;font-weight: bold"> {0} </span> , valid for 5 minutes. Please fill in the verification code as soon as possible!</p> <div style="width:100%;margin:0 auto;"> <div style="padding:10px 10px 0;border-top:1px solid #ccc;color:#747474;margin-bottom:20px;line-height:1.3em;font-size:12px;"> <p>{3}</p> <p>此电子邮件仅限本人查看!如果有人要求你与他分享此 电子邮件或验证,或你认为误收此电子邮件,{4}</p> <br> <p>此为系统邮件,请勿回复<br> Please do not reply to this system email </p> </div> </div> </div> </td> </tr> </tbody> </table> </div> </body> </html>
|