如何将深圳公司的网站数据通过SQL语句导入至数据库?
摘要:深圳公司网站建设公司,网站 数据库 sql 导入数据库,wordpress 分类合并,做的好的办公家具网站文章目录前言环境、版本等pom依赖引入springboot项目配置文件获取邮箱授权码配置properties文件定义接口信息接收类编写
深圳公司网站建设公司,网站 数据库 sql 导入数据库,wordpress 分类合并,做的好的办公家具网站文章目录前言环境、版本等pom依赖引入springboot项目配置文件获取邮箱授权码配置properties文件定义接口信息接收类编写邮件发送服务类编写接口swagger测试1、简单邮件发送2、html格式发送(支持附件)前言
最近再看xxl-job的源码#xff0c;其中在邮件告警通知中使用到了告警信…
文章目录前言环境、版本等pom依赖引入springboot项目配置文件获取邮箱授权码配置properties文件定义接口信息接收类编写邮件发送服务类编写接口swagger测试1、简单邮件发送2、html格式发送(支持附件)前言
最近再看xxl-job的源码其中在邮件告警通知中使用到了告警信息邮件通知的方式挺有意思的特写一篇文章进行简单的配置和使用。
环境、版本等
springboot 2.1.4.RELEASEjdk 1.8
pom依赖引入
springboot的版本就已经对mail组件进行了控制只需要引入对应的依赖即可无需单独设置版本。(也可以设定指定的版本号)
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-mail/artifactId
/dependencyspringboot项目配置文件
由于加入了spring-boot-starter-mail依赖组件此时如果需要使用mail功能还需要进行下面的几项配置。
获取邮箱授权码
进入QQ邮箱的设置找到账户。 在账户项中下滑至POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务。
选择生成授权码。需要发送确认短信信息当发送成功后将会获得当前邮箱的授权码信息。 将授权码信息复制粘贴到spring.mail.password中即可
配置properties文件
创建application.properties文件并在其中配置如下信息
server.port80spring.mail.hostsmtp.qq.com
spring.mail.port25
spring.mail.username302592372qq.com
spring.mail.from302592372qq.com # 邮件发送者
spring.mail.password邮箱授权码
spring.mail.properties.mail.smtp.authtrue
spring.mail.properties.mail.smtp.starttls.enabletrue
spring.mail.properties.mail.smtp.starttls.requiredtrue
spring.mail.properties.mail.smtp.socketFactory.classjavax.net.ssl.SSLSocketFactory定义接口信息接收类
主要是接口传递参数使用如下结构
import lombok.Data;
import java.io.Serializable;Data
public class MailRequest implements Serializable {/*** 接收人*/private String sendTo;/*** 邮件主题*/private String subject;/*** 邮件内容*/private String text;/*** 附件路径*/private String filePath;}编写邮件发送服务类
编写邮件发送操作的服务类使用两种方式简单邮件内容发送、html邮件内容发送。
import cn.xj.emails.uo.MailRequest;
import lombok.extern.slf4j.Slf4j;
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 org.springframework.util.StringUtils;import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;Slf4j
Service
public class SendMailService {Autowiredprivate JavaMailSender javaMailSender;Value(${spring.mail.from})private String sendMailer;/*** 简单邮件内容发送* param mailRequest*/public void sendSimpleMail(MailRequest mailRequest) {SimpleMailMessage message new SimpleMailMessage();//邮件发件人message.setFrom(sendMailer);//邮件收件人 1或多个message.setTo(mailRequest.getSendTo().split(,));//邮件主题message.setSubject(mailRequest.getSubject());//邮件内容message.setText(mailRequest.getText());//邮件发送时间message.setSentDate(new Date());javaMailSender.send(message);log.info(发送邮件成功:{}-{},sendMailer,mailRequest.getSendTo());}/*** Html格式邮件,可带附件* param mailRequest*/public void sendHtmlMail(MailRequest mailRequest) {MimeMessage message javaMailSender.createMimeMessage();try {MimeMessageHelper helper new MimeMessageHelper(message,true);//邮件发件人helper.setFrom(sendMailer);//邮件收件人 1或多个helper.setTo(mailRequest.getSendTo().split(,));//邮件主题helper.setSubject(mailRequest.getSubject());//邮件内容helper.setText(mailRequest.getText(),true);//邮件发送时间helper.setSentDate(new Date());String filePath mailRequest.getFilePath();if (StringUtils.hasText(filePath)) {FileSystemResource file new FileSystemResource(new File(filePath));String fileName filePath.substring(filePath.lastIndexOf(File.separator));helper.addAttachment(fileName,file);}javaMailSender.send(message);log.info(发送邮件成功:{}-{},sendMailer,mailRequest.getSendTo());} catch (MessagingException e) {log.error(发送邮件时发生异常,e);}}
}编写接口
制定一个测试 controller进行简单的接口开发。
import cn.xj.emails.service.SendMailService;
import cn.xj.emails.uo.MailRequest;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;RestController
RequestMapping(/email)
Api(value 发送邮件接口,tags {发送邮件接口})
public class TestController {Autowiredprivate SendMailService sendMailService;PostMapping(/simple)public void SendSimpleMessage(RequestBody MailRequest mailRequest) {sendMailService.sendSimpleMail(mailRequest);}PostMapping(/html)public void SendHtmlMessage(RequestBody MailRequest mailRequest) { sendMailService.sendHtmlMail(mailRequest);}
}swagger测试
为了测试的方便项目中整合了swagger2进行接口测试当然也可以使用postman等工具。
1、简单邮件发送 /email/simple 收到邮件如下
2、html格式发送(支持附件) /email/html 收到的邮件如下所示
