福泉市自己的网站,做招商的网络营销推广,wordpress训网 插件,重庆万泰建设集团有限公司SpringBoot实现发送邮箱 引言
在现代应用程序中#xff0c;电子邮件通常是不可或缺的一部分。在Spring Boot中#xff0c;你可以轻松地实现发送不同类型的邮件#xff0c;包括文本、带附件和HTML邮件。本博客将向你展示如何使用Spring Boot发送这些不同类型的电子邮件。
步…SpringBoot实现发送邮箱 引言
在现代应用程序中电子邮件通常是不可或缺的一部分。在Spring Boot中你可以轻松地实现发送不同类型的邮件包括文本、带附件和HTML邮件。本博客将向你展示如何使用Spring Boot发送这些不同类型的电子邮件。
步骤一导入依赖
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-mail/artifactId
/dependency步骤二配置邮箱信息
mail:# 邮件 SMTP 服务器的主机名host: smtp.qq.com# 用于登录 SMTP 服务器的邮箱地址username: 1111111qq.com# 用于登录 SMTP 服务器的邮箱密码或授权码password: abcdefghijklmnopqrstuvwxyz# SMTP 服务器的端口port: 587# 是否启用 SMTP 认证通常应设置为 truesmtpAuth: true# 是否启用 STARTTLS 安全传输协议通常应设置为 truesmtpStarttlsEnable: true# 是否要求使用 STARTTLS 安全传输协议通常应设置为 truesmtpStarttlsRequired: true# 默认编码defaultEncoding: UTF-8步骤三邮箱配置类
Data
//配置属性文件
Component
//说明配置文件属性的头部
ConfigurationProperties(prefix mail)
public class MailConfig {private String host;private String username;private String password;private Integer port;private String smtpAuth;private String smtpStarttlsEnable;private String smtpStarttlsRequired;private String defaultEncoding;Beanpublic JavaMailSender javaMailSender() {//邮箱发送对象JavaMailSenderImpl javaMailSender new JavaMailSenderImpl();javaMailSender.setHost(host);javaMailSender.setPort(port);javaMailSender.setUsername(username);javaMailSender.setPassword(password);javaMailSender.setDefaultEncoding(defaultEncoding);// 配置其他属性如协议、调试等根据需要Properties properties new Properties();properties.setProperty(mail.smtp.auth, smtpAuth);properties.setProperty(mail.smtp.starttls.enable, smtpStarttlsEnable);properties.setProperty(mail.smtp.starttls.required, smtpStarttlsRequired);javaMailSender.setJavaMailProperties(properties);return javaMailSender;}
}步骤四创建邮箱工具类
这里的发送人必须设置不然会报异常501 Mail from address must be same as authorization user.
Component
public class MailUtils {Value(${mail.username})private String username;Resourceprivate JavaMailSender javaMailSender;/*** 邮箱发送** param to 收信人* param title 邮箱标题* param content 邮箱内容*/public void sendMail(String to, String title, String content) {//邮箱消息对象SimpleMailMessage simpleMailMessage new SimpleMailMessage();simpleMailMessage.setFrom(username);//发送人simpleMailMessage.setTo(to);//收件人simpleMailMessage.setSubject(title);//邮箱标题simpleMailMessage.setText(content);//邮箱内容//实现发送邮箱javaMailSender.send(simpleMailMessage);}/*** 群发邮箱** param toList 收信人集合* param title 邮箱标题* param content 邮箱内容*/public void sendEmailToMultipleRecipients(ListString toList, String title, String content) {SimpleMailMessage message new SimpleMailMessage();message.setFrom(username);//发送人message.setTo(toList.toArray(new String[0]));message.setSubject(title);message.setText(content);javaMailSender.send(message);}/*** 发送HTML邮箱** param to 收信人* param title 邮箱标题* param text HTML内容* param filePath 文件路径* throws MessagingException 邮箱异常*/public void sendEmailWithAttachment(String to, String title, String text, String filePath) throws MessagingException {MimeMessage message javaMailSender.createMimeMessage();message.setFrom(username);//发送人MimeMessageHelper helper new MimeMessageHelper(message, true);helper.setTo(to);helper.setSubject(title);helper.setText(text);FileSystemResource file new FileSystemResource(new File(filePath));helper.addAttachment(FileUtil.getName(filePath), file);javaMailSender.send(message);}/*** 发送HTML邮箱** param to 收信人* param title 邮箱标题* param text HTML内容* param file 文件* throws MessagingException 邮箱异常*/public void sendEmailWithAttachment(String to, String title, String text, File file) throws MessagingException {MimeMessage message javaMailSender.createMimeMessage();message.setFrom(username);//发送人MimeMessageHelper helper new MimeMessageHelper(message, true);helper.setTo(to);helper.setSubject(title);helper.setText(text);helper.addAttachment(FileUtil.getName(file), file);javaMailSender.send(message);}/*** 发送HTML邮箱** param to 收信人* param title 邮箱标题* param htmlContent HTML内容* throws MessagingException 邮箱异常*/public void sendHtmlEmail(String to, String title, String htmlContent) throws MessagingException {MimeMessage message javaMailSender.createMimeMessage();message.setFrom(username);//发送人MimeMessageHelper helper new MimeMessageHelper(message, true);helper.setTo(to);helper.setSubject(title);helper.setText(htmlContent, true); // 设置为true表示HTML内容javaMailSender.send(message);}
}通过使用Spring Boot和JavaMailSender你可以轻松地实现发送文本、带附件和HTML邮件的功能。这些示例可以帮助你在你的应用程序中集成邮件发送功能以便满足不同类型的邮件需求 文章转载自: http://www.morning.dybth.cn.gov.cn.dybth.cn http://www.morning.ywpwq.cn.gov.cn.ywpwq.cn http://www.morning.zqcdl.cn.gov.cn.zqcdl.cn http://www.morning.qfnrx.cn.gov.cn.qfnrx.cn http://www.morning.cpqqf.cn.gov.cn.cpqqf.cn http://www.morning.fstesen.com.gov.cn.fstesen.com http://www.morning.qrdkk.cn.gov.cn.qrdkk.cn http://www.morning.ftlgy.cn.gov.cn.ftlgy.cn http://www.morning.qmfhh.cn.gov.cn.qmfhh.cn http://www.morning.jfmyt.cn.gov.cn.jfmyt.cn http://www.morning.thwcg.cn.gov.cn.thwcg.cn http://www.morning.qxljc.cn.gov.cn.qxljc.cn http://www.morning.jkwwm.cn.gov.cn.jkwwm.cn http://www.morning.qfths.cn.gov.cn.qfths.cn http://www.morning.qynnw.cn.gov.cn.qynnw.cn http://www.morning.ncqzb.cn.gov.cn.ncqzb.cn http://www.morning.lrskd.cn.gov.cn.lrskd.cn http://www.morning.tyklz.cn.gov.cn.tyklz.cn http://www.morning.lgrkr.cn.gov.cn.lgrkr.cn http://www.morning.gbhsz.cn.gov.cn.gbhsz.cn http://www.morning.mxbks.cn.gov.cn.mxbks.cn http://www.morning.wjlhp.cn.gov.cn.wjlhp.cn http://www.morning.chrbp.cn.gov.cn.chrbp.cn http://www.morning.zqcdl.cn.gov.cn.zqcdl.cn http://www.morning.bgnkl.cn.gov.cn.bgnkl.cn http://www.morning.qlckc.cn.gov.cn.qlckc.cn http://www.morning.bqyb.cn.gov.cn.bqyb.cn http://www.morning.czqqy.cn.gov.cn.czqqy.cn http://www.morning.ggtkk.cn.gov.cn.ggtkk.cn http://www.morning.krjyq.cn.gov.cn.krjyq.cn http://www.morning.qfcnp.cn.gov.cn.qfcnp.cn http://www.morning.wkmpx.cn.gov.cn.wkmpx.cn http://www.morning.zsyqg.cn.gov.cn.zsyqg.cn http://www.morning.owenzhi.com.gov.cn.owenzhi.com http://www.morning.hnmbq.cn.gov.cn.hnmbq.cn http://www.morning.rxcqt.cn.gov.cn.rxcqt.cn http://www.morning.fhxrb.cn.gov.cn.fhxrb.cn http://www.morning.zdydj.cn.gov.cn.zdydj.cn http://www.morning.ppqzb.cn.gov.cn.ppqzb.cn http://www.morning.yxyyp.cn.gov.cn.yxyyp.cn http://www.morning.znrgq.cn.gov.cn.znrgq.cn http://www.morning.fmdvbsa.cn.gov.cn.fmdvbsa.cn http://www.morning.pljdy.cn.gov.cn.pljdy.cn http://www.morning.dzqr.cn.gov.cn.dzqr.cn http://www.morning.qdlr.cn.gov.cn.qdlr.cn http://www.morning.yzxhk.cn.gov.cn.yzxhk.cn http://www.morning.807yy.cn.gov.cn.807yy.cn http://www.morning.kwxr.cn.gov.cn.kwxr.cn http://www.morning.tfzjl.cn.gov.cn.tfzjl.cn http://www.morning.fgwzl.cn.gov.cn.fgwzl.cn http://www.morning.xwbwm.cn.gov.cn.xwbwm.cn http://www.morning.mtmph.cn.gov.cn.mtmph.cn http://www.morning.sftpg.cn.gov.cn.sftpg.cn http://www.morning.jhwwr.cn.gov.cn.jhwwr.cn http://www.morning.dywgl.cn.gov.cn.dywgl.cn http://www.morning.wpxfk.cn.gov.cn.wpxfk.cn http://www.morning.uytae.cn.gov.cn.uytae.cn http://www.morning.qsctt.cn.gov.cn.qsctt.cn http://www.morning.lzqdl.cn.gov.cn.lzqdl.cn http://www.morning.ppbqz.cn.gov.cn.ppbqz.cn http://www.morning.mglqf.cn.gov.cn.mglqf.cn http://www.morning.shuangxizhongxin.cn.gov.cn.shuangxizhongxin.cn http://www.morning.bpmnq.cn.gov.cn.bpmnq.cn http://www.morning.xbyyd.cn.gov.cn.xbyyd.cn http://www.morning.kpbgvaf.cn.gov.cn.kpbgvaf.cn http://www.morning.nffwl.cn.gov.cn.nffwl.cn http://www.morning.touziyou.cn.gov.cn.touziyou.cn http://www.morning.hxwhyjh.com.gov.cn.hxwhyjh.com http://www.morning.flxgx.cn.gov.cn.flxgx.cn http://www.morning.wrtxk.cn.gov.cn.wrtxk.cn http://www.morning.dyzbt.cn.gov.cn.dyzbt.cn http://www.morning.thxfn.cn.gov.cn.thxfn.cn http://www.morning.rhpy.cn.gov.cn.rhpy.cn http://www.morning.ntwxt.cn.gov.cn.ntwxt.cn http://www.morning.lsbjj.cn.gov.cn.lsbjj.cn http://www.morning.kwjyt.cn.gov.cn.kwjyt.cn http://www.morning.dgckn.cn.gov.cn.dgckn.cn http://www.morning.krkwp.cn.gov.cn.krkwp.cn http://www.morning.dxpzt.cn.gov.cn.dxpzt.cn http://www.morning.zlhzd.cn.gov.cn.zlhzd.cn