欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring boot搭建郵件服務(wù)的完整步驟

 更新時(shí)間:2019年09月12日 17:10:37   作者:祥哥去哪里啊  
這篇文章主要給大家介紹了關(guān)于Spring boot搭建郵件服務(wù)的完整步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

前言:

發(fā)送郵件,肯定是每個(gè)公司都會(huì)有的基本業(yè)務(wù)。很多公司都會(huì)選擇把發(fā)送郵件作為一個(gè)基礎(chǔ)服務(wù),對(duì)外提供接口。直接調(diào)用就可發(fā)郵件了。但是我們都知道發(fā)送郵件耗時(shí)都比較長(zhǎng)。那么今天就介紹下使用Spring boot+eventbus來(lái)打造一個(gè)簡(jiǎn)單郵件服務(wù)

規(guī)劃接口列表

發(fā)送郵件的類(lèi)型準(zhǔn)備的有三種

  • 發(fā)送普通郵件
  • 發(fā)送html郵件
  • 發(fā)送圖文郵件

還有一個(gè)細(xì)節(jié),如果我們同步的取發(fā)送郵件會(huì)有兩個(gè)問(wèn)題。

  • 接口響應(yīng)時(shí)間比較長(zhǎng)
  • 遇到并發(fā)的情況,容易導(dǎo)致服務(wù)器壓力過(guò)大或者郵箱服務(wù)封ip

所以我們準(zhǔn)備使用隊(duì)列來(lái)執(zhí)行發(fā)送郵件的操作。可以解決這個(gè)問(wèn)題。隊(duì)列我選用的是Google的eventbus。是一款很輕量的隊(duì)列。直接走的內(nèi)存

準(zhǔn)備工作

首先要在pom.xml中引入 需要使用的包

 <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-mail</artifactId>
 </dependency>
 <dependency>
   <groupId>com.google.guava</groupId>
   <artifactId>guava</artifactId>
   <version>23.0</version>
  </dependency>
  <dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
  </dependency>
  • spring-boot-starter-mail :spring-boot提供的發(fā)郵件的maven庫(kù)
  • guava:google提供的開(kāi)源庫(kù)。里面包含來(lái)很多工具
  • lombok:可以幫你省去編寫(xiě)實(shí)體類(lèi)的工具

引入之后,我們還需要配置發(fā)送郵件所需要的必要配置

在application.properties中配置郵箱

spring.mail.host=smtp.mail.me.com //郵箱發(fā)送服務(wù)器
spring.mail.port=587//服務(wù)器端口
spring.mail.username=xxx6666@icloud.com//發(fā)件人郵箱
spring.mail.password=password//客戶(hù)端專(zhuān)用密碼
//如果和我一樣使用的icloud郵箱 還需要下列兩個(gè)配置,別的有的郵箱不需要
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

做到這里其實(shí)就已經(jīng)完成了,發(fā)郵件所需要的配置了。但是我們是要用隊(duì)列來(lái)發(fā)送,所以還需要配置下隊(duì)列

@Configuration
public class AsyncEventBusConfig {
 //實(shí)例化bean,采用單例形式注入容器
 @Bean
 @Scope("singleton")
 public AsyncEventBus asyncEventBus(){
  //創(chuàng)建線程池對(duì)象
  final ThreadPoolExecutor executor=executor();
  return new AsyncEventBus(executor);
 }
 //創(chuàng)建線程池方法
 private ThreadPoolExecutor executor(){
  return new
    ThreadPoolExecutor(2,
    2,0L,
    TimeUnit.MICROSECONDS,
    new LinkedBlockingQueue<>());
 }
}

封裝EmailService

準(zhǔn)備好了之后,就可以直接來(lái)封裝發(fā)送郵件的業(yè)務(wù)了。之前有提到我們需要三個(gè)接口,同樣的,我們也需要三個(gè)service方法

@Service
public class EmailService {

 @Autowired
 private JavaMailSender javaMailSender;

 /**
  * 發(fā)件人。這里發(fā)件人一般是同使用的發(fā)件郵箱一致
  */
 @Value("${spring.mail.username}")
 private String from;


 /**
  * 發(fā)送文本郵件
  * @param to 收件人郵箱地址
  * @param subject 主題
  * @param content 內(nèi)容
  */
 public void sendTextMail(String to,
        String subject,
        String content) {
  SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
  simpleMailMessage.setTo(to);
  simpleMailMessage.setSubject(subject);
  simpleMailMessage.setText(content);
  simpleMailMessage.setFrom(from);
  javaMailSender.send(simpleMailMessage);
 }


 /**
  * 發(fā)送html內(nèi)容的郵件
  * @param to 收件人
  * @param htmlContent html內(nèi)容
  * @param subject 主題
  * @throws MessagingException
  */
 public void sendHtmlMail(String to,
        String htmlContent,
        String subject) throws MessagingException {
  MimeMessage message = javaMailSender.createMimeMessage();
  MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);
  messageHelper.setTo(to);
  messageHelper.setSubject(subject);
  messageHelper.setFrom(from);
  messageHelper.setText(htmlContent, true);
  javaMailSender.send(message);
 }

 /**
  * 發(fā)送圖文郵件
  * @param to 收件人
  * @param imgContent 圖文內(nèi)容
  * @param subject 主題
  * @param rscId 資源id
  * @param imgPath 資源路徑
  * @throws MessagingException
  */
 public void sendImgMail(String to,
       String imgContent,
       String subject,
       String rscId,
       String imgPath) throws MessagingException {
  MimeMessage message = javaMailSender.createMimeMessage();
  MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);
  messageHelper.setTo(to);
  messageHelper.setSubject(subject);
  messageHelper.setFrom(from);
  messageHelper.setText(imgContent, true);
  messageHelper.addInline(rscId, new File(imgPath));
  javaMailSender.send(message);
 }
}

隊(duì)列監(jiān)聽(tīng)

既然封裝好了方法,那么就需要調(diào)用。調(diào)用的方式,其實(shí)就是將接口傳來(lái)的數(shù)據(jù)傳到隊(duì)列里。隊(duì)列的消費(fèi)者接收到了消息就將消息拿來(lái)調(diào)用發(fā)送郵件的方法

我們首先創(chuàng)建一個(gè)消費(fèi)類(lèi),用來(lái)接受消息,處理消息。

@Service
public class EventBusListener {

 /**
  * 引入bean
  */
 @Autowired
 private AsyncEventBus asyncEventBus;

 @Autowired
 private EmailService emailService;

 /**
  * 注冊(cè)服務(wù)類(lèi)
  */
 @PostConstruct
 public void init(){
  asyncEventBus.register(this);
 }

 /**
  * 線程安全,消費(fèi) 文本消息
  * @param textEmailDTO
  */
 @AllowConcurrentEvents
 @Subscribe
 public void sendTextMail(TextEmailDTO textEmailDTO){
  emailService.sendTextMail(
    textEmailDTO.getTo(),
    textEmailDTO.getSubject(),
    textEmailDTO.getContent()
  );
 }

 /**
  * 線程安全 消費(fèi) html消息
  * @param htmlEmailDTO
  */
 @AllowConcurrentEvents
 @Subscribe
 public void sendHtmlMail(HtmlEmailDTO htmlEmailDTO){
  try {
   emailService.sendHtmlMail(
     htmlEmailDTO.getTo(),
     htmlEmailDTO.getHtmlContent(),
     htmlEmailDTO.getSubject()
   );
  } catch (MessagingException e) {
   // nothing to do
  }
 }

 /**
  * 線程安全 消費(fèi) 圖文消息
  * @param imgEmailDTO
  */
 @AllowConcurrentEvents
 @Subscribe
 public void sendImgMail(ImgEmailDTO imgEmailDTO){
  try {
   emailService.sendImgMail(
     imgEmailDTO.getTo(),
     imgEmailDTO.getImgContent(),
     imgEmailDTO.getSubject(),
     imgEmailDTO.getRscId(),
     imgEmailDTO.getImgPath()
   );
  } catch (MessagingException e) {
   // nothing to do
  }
 }
}

其實(shí)eventbus拋消息都是使用的post方法來(lái)拋消息。走到不同的方法里面是利用了類(lèi)的多態(tài),拋入不同的實(shí)體類(lèi)就可以進(jìn)行區(qū)分了。走進(jìn)了不同的方法,就調(diào)用相應(yīng)Service方法。

控制器與測(cè)試

控制器部分,沒(méi)什么好說(shuō)的,我就貼出圖文的代碼。其余代碼可以在我的github上面看

先看眼實(shí)體類(lèi)

@Data
public class ImgEmailDTO implements Serializable {
 public ImgEmailDTO() {
 }

 /**
  * 圖片路徑
  */
 private String imgPath;

 /**
  * 資源id
  */
 private String rscId;

 /**
  * 主題
  */
 private String subject;

 /**
  * 圖片正文(同樣可以使用html)
  */
 private String imgContent;

 /**
  * 收件人
  */
 private String to;
}
 /**
  * 發(fā)送圖文郵件
  * @param request
  * @return
  */
 @RequestMapping(value = "/sendImgMail", method = RequestMethod.POST)
 public Result<Integer> sendImgMail(@RequestBody Request<ImgEmailDTO> request) {
  Result<Integer> result = Result.create();
  ImgEmailDTO imgEmailDTO=request.getData();
  StringBuilder sb=new StringBuilder();
  sb.append(imgEmailDTO.getImgContent());
  //cid:資源id。在spring中會(huì)自動(dòng)綁定
  sb.append("<img src=\'cid:").append(imgEmailDTO.getRscId()).append("\'></img>");
  imgEmailDTO.setImgContent(sb.toString());
  asyncEventBus.post(imgEmailDTO);
  return result.success(1);
 }

圖文要稍微特殊一點(diǎn),需要拼接下正文內(nèi)容。然后將實(shí)體類(lèi)中的content替換。最后將實(shí)體類(lèi)拋入隊(duì)列。直接返回接口請(qǐng)求。隊(duì)列那邊就會(huì)排著隊(duì)搞定所有的郵件

下面來(lái)做個(gè)測(cè)試

請(qǐng)求很迅速的返回了結(jié)果

然后去郵箱中查看結(jié)果

好了今天對(duì)郵件服務(wù)的介紹就寫(xiě)到這里。知識(shí)點(diǎn)并不深?yuàn)W,主要介紹一個(gè)思路。如有不對(duì)的地方,請(qǐng)大神指出。謝謝

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論