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

时间:2021-10-18 02:17:05

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

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

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

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

这里只要是因为QQ邮箱默认端口是465,需要修改为SSL配置

java代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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);
    }
  }
   
}

邮箱配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#邮箱配置
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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.cnblogs.com/sunxun/p/13445527.html