溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

發(fā)布時間:2020-10-08 08:53:38 來源:腳本之家 閱讀:197 作者:手撕高達的村長 欄目:開發(fā)技術

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

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

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

要使用企業(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)容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI