亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring Boot Mail QQ企業(yè)郵箱無法連接解決方案

 更新時間:2020年09月21日 11:11:46   作者:手撕高達的村長  
這篇文章主要介紹了Spring Boot Mail QQ企業(yè)郵箱無法連接解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

這里記錄一下QQ企業(yè)郵箱發(fā)郵件問題,因為之前遇到過一種情況是本地測試沒問題,結(jié)果線上出現(xiàn)問題

Couldn't connect to host, port: smtp.qq.com, 25; timeout -1

要使用企業(yè)郵箱生成的授權密碼.

這里只要是因為QQ郵箱默認端口是465,需要修改為SSL配置

java代碼

package com.chenpeng.cpeducloud.service.impl;
import lombok.extern.slf4j.Slf4j;
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.messaging.MessagingException;
import org.springframework.stereotype.Service;
 
import com.chenpeng.cpeducloud.base.WebConstants;
import com.chenpeng.cpeducloud.service.MailService;
import com.chenpeng.cpeducloud.util.Constants;
import com.chenpeng.cpeducloud.util.DateUtils;
import com.chenpeng.cpeducloud.util.StringUtils;
 
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 
/**auth : szy
 *time : 2019-05-16
 **/
@Service
@Slf4j
public class MailServiceImpl implements MailService {
 
  @Autowired
  private JavaMailSender mailSender;
 
  @Value("${mail.formSender}")
  private String sender;// 發(fā)送者
 
  @Value("${mail.formMobile}")
  private String formMobile;// 聯(lián)系電話
   
  /**
   * 發(fā)送簡單郵件(收件人,主題,內(nèi)容)
   */
  @Override
  public void sendSimpleMail(String to, String subject, String content) {
    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom(sender);
    message.setTo(to);
    message.setSubject(subject);
    message.setText(content);
    try {
      mailSender.send(message);
      log.info("簡單郵件發(fā)送成功!");
    } catch (Exception e) {
      log.info("發(fā)送簡單郵件時發(fā)生異常!"+e);
    }
  }
 
 
  /**
   * 發(fā)送Html郵件(收件人,主題,內(nèi)容)
   */
  @Override
  public void sendHtmlMail(String to, String subject, String content) {
    MimeMessage message = mailSender.createMimeMessage();
    try {
      MimeMessageHelper helper = null;  //true表示需要創(chuàng)建一個multipart message
      try {
        helper = new MimeMessageHelper(message, true);
        message.setFrom(sender);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);
        mailSender.send(message);
        log.info("html郵件發(fā)送成功");
      } catch (javax.mail.MessagingException e) {
        e.printStackTrace();
      }
 
    } catch (MessagingException e) {
      log.info("發(fā)送html郵件時發(fā)生異常!"+e);
    }
  }
 
  /**
   * 發(fā)送帶附件的郵件
   * @param to
   * @param subject
   * @param content
   * @param filePath
   */
  @Override
  public void sendAttachmentsMail(String to, String subject, String content, String filePath){
    MimeMessage message = mailSender.createMimeMessage();
 
    try {
      MimeMessageHelper helper = null;
      try {
        helper = new MimeMessageHelper(message, true);
        message.setFrom(sender);
        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);
        //helper.addAttachment("test"+fileName, file);
 
        mailSender.send(message);
        log.info("帶附件的郵件已經(jīng)發(fā)送。");
      } catch (javax.mail.MessagingException e) {
        e.printStackTrace();
      }
 
    } catch (MessagingException e) {
      log.info("發(fā)送帶附件的郵件時發(fā)生異常!"+e);
    }
  }
 
 
  /**
   * 發(fā)送Html郵件(收件人,主題,內(nèi)容),
   * 帶多附件
   */
  @Override
  public void sendHtmlMailAndAttachments(String[] to,String[] cc, String subject, String content, List<String> files) {
    MimeMessage message = mailSender.createMimeMessage();
    try {
      MimeMessageHelper helper = null;  //true表示需要創(chuàng)建一個multipart message
      try {
        helper = new MimeMessageHelper(message, true);
        message.setFrom(sender);
        helper.setTo(to);
        helper.setCc(cc);
        helper.setSubject(subject);
        helper.setText(content, true);
 
        for (String filePath : files){
          FileSystemResource file = new FileSystemResource(new File(filePath));
          String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
          helper.addAttachment(fileName, file);
        }
        mailSender.send(message);
        log.info("html郵件發(fā)送成功");
      } catch (javax.mail.MessagingException e) {
        e.printStackTrace();
      }
 
    } catch (MessagingException e) {
      log.info("發(fā)送html郵件時發(fā)生異常!"+e);
    }
  }
   
}

郵箱配置

#郵箱配置
mail:
 host: smtp.exmail.qq.com
 username: 11111@qq.com
 password: 密鑰不是密碼
 default-encoding: utf-8
 port: 465
 properties:
  mail:
   smtp:
    auth: true
    ssl:
     enable: true
     socketFactory:
      class: com.sun.mail.util.MailSSLSocketFactory
      fallback: false
    starttls:
        enable: true
        required: true

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Java中String類startsWith方法詳解

    Java中String類startsWith方法詳解

    這篇文章主要給大家介紹了關于Java中String類startsWith方法的相關資料,startsWith() 方法用于檢測字符串是否以指定的前綴開始,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-05-05
  • Mybatis中SqlSession下的四大對象之執(zhí)行器(executor)

    Mybatis中SqlSession下的四大對象之執(zhí)行器(executor)

    mybatis中sqlsession下的四大對象是指:executor, statementHandler,parameterHandler,resultHandler對象。這篇文章主要介紹了Mybatis中SqlSession下的四大對象之執(zhí)行器(executor),需要的朋友可以參考下
    2019-04-04
  • Java中Spring的單例模式使用

    Java中Spring的單例模式使用

    這篇文章主要介紹了Java中Spring的單例模式使用,spring中的單例也不影響應用并發(fā)訪問。大多數(shù)時候客戶端都在訪問我們應用中的業(yè)務對象,為減少并發(fā)控制,不應該在業(yè)務對象中設置那些容易造成出錯的成員變量,下面一起進入文章了解更多詳細內(nèi)容吧
    2022-01-01
  • Java Spring之@Async原理案例詳解

    Java Spring之@Async原理案例詳解

    這篇文章主要介紹了Java Spring之@Async原理案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • MyBatis-Plus非表字段的三種處理方法小結(jié)

    MyBatis-Plus非表字段的三種處理方法小結(jié)

    這篇文章主要介紹了MyBatis-Plus非表字段的三種處理方法小結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • SpringBoot全局異常處理與定制404頁面的方法

    SpringBoot全局異常處理與定制404頁面的方法

    這篇文章主要介紹了SpringBoot全局異常處理與定制404頁面的相關資料,本文通過實例代碼圖文相結(jié)合給大家介紹的非常詳細,需要的朋友可以參考下
    2007-09-09
  • IDEA?database和datagrip無法下載驅(qū)動問題解決辦法

    IDEA?database和datagrip無法下載驅(qū)動問題解決辦法

    這篇文章主要給大家介紹了關于IDEA?database和datagrip無法下載驅(qū)動問題的解決辦法,文中通過代碼介紹的非常詳細,對大家學習或者使用idea具有一定的參考借鑒價值,需要的朋友可以參考下
    2024-03-03
  • Java經(jīng)典面試題最全匯總208道(六)

    Java經(jīng)典面試題最全匯總208道(六)

    這篇文章主要介紹了Java經(jīng)典面試題最全匯總208道(六),本文章內(nèi)容詳細,該模塊分為了六個部分,本次為第六部分,需要的朋友可以參考下
    2023-01-01
  • SpringBoot在IDEA中實現(xiàn)熱部署的步驟

    SpringBoot在IDEA中實現(xiàn)熱部署的步驟

    這篇文章主要介紹了SpringBoot在IDEA中實現(xiàn)熱部署的步驟,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下
    2020-11-11
  • java控制臺輸入示例分享

    java控制臺輸入示例分享

    這篇文章主要介紹了java控制臺輸入示例分享,需要的朋友可以參考下
    2014-03-03

最新評論