SpringBoot拦截全局异常并发送邮件给指定邮箱

发布时间:2024-05-07 点击:91
主要是看一下springboot中发送邮件的方法,至于拦截springboot全局异常之前的文章中有。
一 发送邮件
在springboot中发送邮件非常简单。
pom.xml引入maven依赖
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-mail</artifactid> </dependency>在application.yml里设置发信人的账号、密码
spring: mail: host: smtp.qq.com @qq.com : mail: smtp: auth: true starttls: enable: true required: true 这个username就是未来发信时的邮箱地址,password是授权码。
这里以普通qq邮箱为例,注意password不是qq密码,而是授权码。
在qq邮箱-设置-账户,找到图片中的地方,开启imap/smtp服务,开启后才能在别的客户端使用该qq邮箱发邮件,然后生成授权码,填写到application.yml的password位置。
然后就可以使用该邮箱作为发件人了。
看一下发邮件的具体代码,参考http://blog.csdn.net/clementad/article/details/51833416
package com.tianyalei.testmail.service; import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.beans.factory.annotation.autowired; import org.springframework.beans.factory.annotation.value; import org.springframework.core.io.filesystemresource; import org.springframework.mail.simplemailmessage; import org.springframework.mail.javamail.javamailsender; import org.springframework.mail.javamail.mimemessagehelper; import org.springframework.stereotype.service; import javax.mail.messagingexception; import javax.mail.internet.mimemessage; import java.io.file; @service public class mailservice { private final logger logger = loggerfactory.getlogger(this.getclass()); @autowired private javamailsender sender; @value(“${spring.mail.username}”) private string from; / * 发送纯文本的简单邮件 * @param to * @param subject * @param content */ public void sendsimplemail(string to, string subject, string content){ simplemailmessage message = new simplemailmessage(); message.setfrom(from); message.setto(to); message.setsubject(subject); message.settext(content); try { sender.send(message); logger.info(“简单邮件已经发送。”); } catch (exception e) { logger.error(“发送简单邮件时发生异常!”, e); } } / * 发送html格式的邮件 * @param to * @param subject * @param content */ public void sendhtmlmail(string to, string subject, string content){ mimemessage message = sender.createmimemessage(); try { //true表示需要创建一个multipart message mimemessagehelper helper = new mimemessagehelper(message, true); helper.setfrom(from); helper.setto(to); helper.setsubject(subject); helper.settext(content, true); sender.send(message); logger.info(“html邮件已经发送。”); } catch (messagingexception e) { logger.error(“发送html邮件时发生异常!”, e); } } / * 发送带附件的邮件 * @param to * @param subject * @param content * @param filepath */ public void sendattachmentsmail(string to, string subject, string content, string filepath){ mimemessage message = sender.createmimemessage(); try { //true表示需要创建一个multipart message mimemessagehelper helper = new mimemessagehelper(message, true); helper.setfrom(from); helper.setto(to); helper.setsubject(subject); helper.settext(content, true); filesystemresource file = new filesystemresource(new file(filepath)); string filename = filepath.substring(filepath.lastindexof(file.separator)); helper.addattachment(filename, file); sender.send(message); logger.info(“带附件的邮件已经发送。”); } catch (messagingexception e) { logger.error(“发送带附件的邮件时发生异常!”, e); } } / * 发送嵌入静态资源(一般是图片)的邮件 * @param to * @param subject * @param content 邮件内容,需要包括一个静态资源的id,比如:<img src=”cid:rscid01″ > * @param rscpath 静态资源路径和文件名 * @param rscid 静态资源id */ public void sendinlineresourcemail(string to, string subject, string content, string rscpath, string rscid){ mimemessage message = sender.createmimemessage(); try { //true表示需要创建一个multipart message mimemessagehelper helper = new mimemessagehelper(message, true); helper.setfrom(from); helper.setto(to); helper.setsubject(subject); helper.settext(content, true); filesystemresource res = new filesystemresource(new file(rscpath)); helper.addinline(rscid, res); sender.send(message); logger.info(“嵌入静态资源的邮件已经发送。”); } catch (messagingexception e) { logger.error(“发送嵌入静态资源的邮件时发生异常!”, e); } } }然后就可以使用里面的方法发邮件了。
可以先写个简单的测试类,调用
mailservice.sendsimplemail(“wuweifeng@xxx.com”, “主题:简单邮件”, “测试邮件内容”);填写个收信人的地址就ok了。然后就能收到邮件了。收信人可以有多个,通过simplemailmessage可以看到。
二 拦截全局异常并发邮件
如果,你对上面的内容还有疑问,推荐选择西部数码企业云邮箱!有专人协助您解答邮箱疑问。
西部数码优质企业邮箱服务商,提供安全稳定,简单易用,高性价比的企业邮箱。按需自由定制,不限空间,极速收发,能够满足用户对企业邮箱的不同需求。多种反垃圾邮件算法,99.9%精准度,智能过滤,减少垃圾邮件干扰。支持小程序收发邮件,随时随地移动办公。而且价格实惠,还可以免费试用,7×24小时专业团队服务支持!
高性价比企业邮箱开通链接:https://www.west.cn/services/mail/


我这个本案要怎么弄了我资料都更新了还是不行
企业建站云服务器
node.dll是什么?
网站可以换虚拟主机吗
云服务器购买价钱
无法绑定域名显示也添加白名单
服务器租赁 价格包含哪些方面?
nginx+php出现No input file specified解决办法