网站服务器如何维护,网站怎么做透明导航,wordpress模板安装后效果和预览不同,网站开发 职位一. 发送邮件原理
发件人【设置授权码】 - SMTP协议【Simple Mail TransferProtocol - 是一种提供可靠且有效的电子邮件传输的协议】 - 收件人
二. 获取授权码
开通POP3/SMTP#xff0c;获取授权码
授权码是QQ邮箱推出的#xff0c;用于登录第三方客户端的专用密码。适用…一. 发送邮件原理
发件人【设置授权码】 - SMTP协议【Simple Mail TransferProtocol - 是一种提供可靠且有效的电子邮件传输的协议】 - 收件人
二. 获取授权码
开通POP3/SMTP获取授权码
授权码是QQ邮箱推出的用于登录第三方客户端的专用密码。适用于登录以下服务POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务。
温馨提醒为了你的帐户安全更改QQ密码以及独立密码会触发授权码过期需要重新获取新的授权码登录
第一步进入邮箱设置 - 账户 - 生成授权码 【注意】POP3/SMTP ,IMAP/SMTP服务都需要开启
三. SpringBoot发送邮件步骤
1、导入依赖
!--对邮件的支持jar--
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-mail/artifactId
/dependency
2、配置邮箱
spring:servlet:multipart:#设置单个文件大小max-file-size: 10MB#设置单次请求文件的总大小max-request-size: 50MBmail:host: smtp.qq.com # 设置邮箱主机(服务商),这里使用QQ邮件服务器username: # 设置用户名 - 发送方password: # 设置密码该处的密码是QQ邮箱开启SMTP的授权码而非QQ密码properties:mail:smtp:auth: true # 必须进行授权认证它的目的就是阻止他人任意乱发邮件starttls: #SMTP加密方式:连接到一个TLS保护连接enable: truerequired: true
3、发送邮件的时候可以自定义发送的内容创建工具类
Component
Slf4j
public class MailUtil {/*** 1. 来源人名* 2. 来源内容*/public static final String originalText hr style\border: 1px dashed #ef859d2e;margin: 20px 0\\n div\n div style\font-size: 18px;font-weight: bold;color: #C5343E\\n %s\n /div\n div style\margin-top: 6px;font-size: 16px;color: #000000\\n p\n %s\n /p\n /div\n /div;/*** 发件人*/public static final String replyMail 你之前的评论收到来自 %s 的回复;public static final String commentMail 你的文章 %s 收到来自 %s 的评论;public static final String messageMail 你收到来自 %s 的留言;public static final String loveMail 你收到来自 %s 的祝福;public static final String imMail 你收到来自 %s 的消息;public static final String notificationMail 你收到来自 %s 的订阅;Autowiredprivate JavaMailSender mailSender;Value(${spring.mail.username})private String sendMailer;/*** 1. 网站名称* 2. 邮件类型* 3. 发件人* 4. 发件内容* 5. originalText* 6. 网站名称*/private String mailText;PostConstructpublic void init() {this.mailText div style\font-family: serif;line-height: 22px;padding: 30px\\n div style\display: flex;justify-content: center;width: 100%%;max-width: 900px;background-size: cover;border-radius: 10px\/div\n div style\margin-top: 20px;display: flex;flex-direction: column;align-items: center\\n div style\margin: 10px auto 20px;text-align: center\\n div style\line-height: 32px;font-size: 26px;font-weight: bold;color: #000000\\n 嘿你在 %s 中收到一条新消息。\n /div\n div style\font-size: 16px;font-weight: bold;color: rgba(0, 0, 0, 0.19);margin-top: 21px\\n %s\n /div\n /div\n div style\min-width: 250px;max-width: 800px;min-height: 128px;background: #F7F7F7;border-radius: 10px;padding: 32px\\n div\n div style\font-size: 18px;font-weight: bold;color: #C5343E\\n %s\n /div\n div style\margin-top: 6px;font-size: 16px;color: #000000\\n p\n %s\n /p\n /div\n /div\n %s\n a style\width: 150px;height: 38px;background: #ef859d38;border-radius: 32px;display: flex;align-items: center;justify-content: center;text-decoration: none;margin: 40px auto 0\\n href\https://poetize.cn\ target\_blank\\n span style\color: #DB214B\有朋自远方来/span\n /a\n /div\n div style\margin-top: 20px;font-size: 12px;color: #00000045\\n 此邮件由 %s 自动发出直接回复无效一天最多发送 CommonConst.COMMENT_IM_MAIL_COUNT 条通知邮件和 CommonConst.CODE_MAIL_COUNT 条验证码邮件退订请联系站长。\n /div\n /div\n /div;}public String getMailText() {return mailText;}
}
4、实现类。一般来说验证码可以应用在注册、登录、找回密码等功能上。前端需要传递flag标志后端进行判断发送哪种邮件
public Result getCodeForForgetPassword(String place, Integer flag) {int i new Random().nextInt(900000) 100000;if (flag 1) {// 任务需求} else if (flag 2) {log.info(place --- 邮箱验证码--- i);ListString mail new ArrayList();mail.add(place);String text getCodeMail(i);WebInfo webInfo (WebInfo) UtopianCache.get(CommonConst.WEB_INFO);AtomicInteger count (AtomicInteger) UtopianCache.get(CommonConst.CODE_MAIL mail.get(0));if (count null || count.get() CommonConst.CODE_MAIL_COUNT) {mailUtil.sendMailMessage(mail, 您有一封来自 (webInfo null ? Utopian : webInfo.getWebName()) 的回信, text);if (count null) {UtopianCache.put(CommonConst.CODE_MAIL mail.get(0), new AtomicInteger(1), CommonConst.CODE_EXPIRE);} else {count.incrementAndGet();}} else {return Result.fail(验证码发送次数过多请明天再试);}}UtopianCache.put(CommonConst.FORGET_PASSWORD place _ flag, Integer.valueOf(i), 300);return Result.success();}首先根据标志为flag参数这里2为注册时候使用的格式。调用getCodeMail方法获取模板内容
private String getCodeMail(int i) {WebInfo webInfo (WebInfo) UtopianCache.get(CommonConst.WEB_INFO);String webName (webInfo null ? Utopian : webInfo.getWebName());return String.format(mailUtil.getMailText(),webName,String.format(MailUtil.imMail, PoetryUtil.getAdminUser().getUsername()),UtopianUtil.getAdminUser().getUsername(),String.format(codeFormat, i),,webName);}模板内容为【utopian.cn】%s为本次验证的验证码请在5分钟内完成验证。为保证账号安全请勿泄漏此验证码。
对用户点击发送的验证码的次数也需要进行统计不超过三次否则进行提示
4、发送邮件
这里采用异步的方式分别填充邮件主题邮件内容发送时间等信息 Asyncpublic void sendMailMessage(ListString to, String subject, String text) {log.info(发送邮件);log.info(to{}, JSON.toJSONString(to));log.info(subject{}, subject);log.info(text{}, text);try {//true代表支持复杂的类型MimeMessageHelper mimeMessageHelper new MimeMessageHelper(mailSender.createMimeMessage(), true);//邮件发信人mimeMessageHelper.setFrom(sendMailer);//邮件收信人1或多个mimeMessageHelper.setTo(to.toArray(new String[0]));//邮件主题mimeMessageHelper.setSubject(subject);//邮件内容mimeMessageHelper.setText(text, true);//邮件发送时间mimeMessageHelper.setSentDate(new Date());//发送邮件mailSender.send(mimeMessageHelper.getMimeMessage());log.info(发送成功);} catch (MessagingException e) {log.info(发送失败);log.error(e.getMessage());}} 文章转载自: http://www.morning.ndmh.cn.gov.cn.ndmh.cn http://www.morning.qgdsd.cn.gov.cn.qgdsd.cn http://www.morning.khpx.cn.gov.cn.khpx.cn http://www.morning.rmryl.cn.gov.cn.rmryl.cn http://www.morning.fthcn.cn.gov.cn.fthcn.cn http://www.morning.jwcmq.cn.gov.cn.jwcmq.cn http://www.morning.hrjrt.cn.gov.cn.hrjrt.cn http://www.morning.rcqyk.cn.gov.cn.rcqyk.cn http://www.morning.xldpm.cn.gov.cn.xldpm.cn http://www.morning.rppf.cn.gov.cn.rppf.cn http://www.morning.xdpjs.cn.gov.cn.xdpjs.cn http://www.morning.wlbwp.cn.gov.cn.wlbwp.cn http://www.morning.lsbjj.cn.gov.cn.lsbjj.cn http://www.morning.jljiangyan.com.gov.cn.jljiangyan.com http://www.morning.cnbdn.cn.gov.cn.cnbdn.cn http://www.morning.slfkt.cn.gov.cn.slfkt.cn http://www.morning.wkrkb.cn.gov.cn.wkrkb.cn http://www.morning.tkgjl.cn.gov.cn.tkgjl.cn http://www.morning.nhlnh.cn.gov.cn.nhlnh.cn http://www.morning.zdxinxi.com.gov.cn.zdxinxi.com http://www.morning.thrtt.cn.gov.cn.thrtt.cn http://www.morning.wqtzs.cn.gov.cn.wqtzs.cn http://www.morning.trbxt.cn.gov.cn.trbxt.cn http://www.morning.lnnc.cn.gov.cn.lnnc.cn http://www.morning.gthgf.cn.gov.cn.gthgf.cn http://www.morning.rnqbn.cn.gov.cn.rnqbn.cn http://www.morning.xkqjw.cn.gov.cn.xkqjw.cn http://www.morning.ymbqr.cn.gov.cn.ymbqr.cn http://www.morning.yrxcn.cn.gov.cn.yrxcn.cn http://www.morning.rycbz.cn.gov.cn.rycbz.cn http://www.morning.lkpzx.cn.gov.cn.lkpzx.cn http://www.morning.wtnwf.cn.gov.cn.wtnwf.cn http://www.morning.fdxhk.cn.gov.cn.fdxhk.cn http://www.morning.mrfbp.cn.gov.cn.mrfbp.cn http://www.morning.wkmpx.cn.gov.cn.wkmpx.cn http://www.morning.nyqb.cn.gov.cn.nyqb.cn http://www.morning.jwgmx.cn.gov.cn.jwgmx.cn http://www.morning.xnpml.cn.gov.cn.xnpml.cn http://www.morning.hyhzt.cn.gov.cn.hyhzt.cn http://www.morning.zmyzt.cn.gov.cn.zmyzt.cn http://www.morning.mtxrq.cn.gov.cn.mtxrq.cn http://www.morning.hhmfp.cn.gov.cn.hhmfp.cn http://www.morning.cttgj.cn.gov.cn.cttgj.cn http://www.morning.knrgb.cn.gov.cn.knrgb.cn http://www.morning.rqknq.cn.gov.cn.rqknq.cn http://www.morning.yldgw.cn.gov.cn.yldgw.cn http://www.morning.yydzk.cn.gov.cn.yydzk.cn http://www.morning.aishuxue.com.cn.gov.cn.aishuxue.com.cn http://www.morning.rmjxp.cn.gov.cn.rmjxp.cn http://www.morning.hnkkm.cn.gov.cn.hnkkm.cn http://www.morning.dsgdt.cn.gov.cn.dsgdt.cn http://www.morning.jqrp.cn.gov.cn.jqrp.cn http://www.morning.tpyjr.cn.gov.cn.tpyjr.cn http://www.morning.pgzgy.cn.gov.cn.pgzgy.cn http://www.morning.bhjyh.cn.gov.cn.bhjyh.cn http://www.morning.lizimc.com.gov.cn.lizimc.com http://www.morning.zrwlz.cn.gov.cn.zrwlz.cn http://www.morning.sh-wj.com.cn.gov.cn.sh-wj.com.cn http://www.morning.wnrcj.cn.gov.cn.wnrcj.cn http://www.morning.qzqfq.cn.gov.cn.qzqfq.cn http://www.morning.pjwfs.cn.gov.cn.pjwfs.cn http://www.morning.ygrdb.cn.gov.cn.ygrdb.cn http://www.morning.qblcm.cn.gov.cn.qblcm.cn http://www.morning.xpqsk.cn.gov.cn.xpqsk.cn http://www.morning.zfkxj.cn.gov.cn.zfkxj.cn http://www.morning.sgqw.cn.gov.cn.sgqw.cn http://www.morning.qxljc.cn.gov.cn.qxljc.cn http://www.morning.5-73.com.gov.cn.5-73.com http://www.morning.cyyhy.cn.gov.cn.cyyhy.cn http://www.morning.ldcrh.cn.gov.cn.ldcrh.cn http://www.morning.mpflb.cn.gov.cn.mpflb.cn http://www.morning.0small.cn.gov.cn.0small.cn http://www.morning.kqbzy.cn.gov.cn.kqbzy.cn http://www.morning.rqnzh.cn.gov.cn.rqnzh.cn http://www.morning.qpqcq.cn.gov.cn.qpqcq.cn http://www.morning.jgnjl.cn.gov.cn.jgnjl.cn http://www.morning.rfmzs.cn.gov.cn.rfmzs.cn http://www.morning.qtfss.cn.gov.cn.qtfss.cn http://www.morning.wdply.cn.gov.cn.wdply.cn http://www.morning.lfcnj.cn.gov.cn.lfcnj.cn