当前位置: 移动技术网 > IT编程>开发语言>Java > Spring Boot Mail QQ企业邮箱无法连接解决方案

Spring Boot Mail QQ企业邮箱无法连接解决方案

2020年09月22日  | 移动技术网IT编程  | 我要评论
这里记录一下qq企业邮箱发邮件问题,因为之前遇到过一种情况是本地测试没问题,结果线上出现问题couldn't connect to host, port: smtp.qq.com, 25; timeo

这里记录一下qq企业邮箱发邮件问题,因为之前遇到过一种情况是本地测试没问题,结果线上出现问题

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

要使用企业邮箱生成的授权密码.

这里只要是因为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;// 发送者
 
  @value("${mail.formmobile}")
  private string formmobile;// 联系电话
   
  /**
   * 发送简单邮件(收件人,主题,内容)
   */
  @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("简单邮件发送成功!");
    } catch (exception e) {
      log.info("发送简单邮件时发生异常!"+e);
    }
  }
 
 
  /**
   * 发送html邮件(收件人,主题,内容)
   */
  @override
  public void sendhtmlmail(string to, string subject, string content) {
    mimemessage message = mailsender.createmimemessage();
    try {
      mimemessagehelper helper = null;  //true表示需要创建一个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邮件发送成功");
      } catch (javax.mail.messagingexception e) {
        e.printstacktrace();
      }
 
    } catch (messagingexception e) {
      log.info("发送html邮件时发生异常!"+e);
    }
  }
 
  /**
   * 发送带附件的邮件
   * @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("带附件的邮件已经发送。");
      } catch (javax.mail.messagingexception e) {
        e.printstacktrace();
      }
 
    } catch (messagingexception e) {
      log.info("发送带附件的邮件时发生异常!"+e);
    }
  }
 
 
  /**
   * 发送html邮件(收件人,主题,内容),
   * 带多附件
   */
  @override
  public void sendhtmlmailandattachments(string[] to,string[] cc, string subject, string content, list<string> files) {
    mimemessage message = mailsender.createmimemessage();
    try {
      mimemessagehelper helper = null;  //true表示需要创建一个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邮件发送成功");
      } catch (javax.mail.messagingexception e) {
        e.printstacktrace();
      }
 
    } catch (messagingexception e) {
      log.info("发送html邮件时发生异常!"+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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网