This is the fourth day of my participation in the August More text Challenge. For details, see:August is more challenging

Problem description

The DEBUG: JavaMail version 1.6.1 DEBUG: successfully the loaded resource: / meta-inf/JavaMail. Default. Will the DEBUG: Tables of loaded providers DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]} DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]} DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle] DEBUG SMTP: useEhlo true, useAuth false DEBUG SMTP: trying to connect to host "smtp.163.com", port 465, isSSL false DEBUG SMTP: EOF: [EOF] DEBUG SMTP: could not connect to host "smtp.163.com", port: 465, response: -1 2021-06-02 16:35:42.323 ERROR 11141 -- [NIO-8999-exec-1] O.A.C.C.C. [.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: smtp.163.com, port: 465, response: -1. Failed messages: javax.mail.MessagingException: Could not connect to SMTP host: smtp.163.com, port: 465, response: -1; message exceptions (1) are: Failed message 1: javax.mail.MessagingException: Could not connect to SMTP host: smtp.163.com, port: 465, response: -1] with root cause javax.mail.MessagingException: Could not connect to SMTP host: smtp.163.com, port: 465, response: 1 at the sun. The mail. The SMTP. SMTPTransport. OpenServer (SMTPTransport. Java: 2197) ~ [javax.mail. Mail - 1.6.1. Jar: 1.6.1] the at Com. Sun. Mail. The SMTP. SMTPTransport. ProtocolConnect (SMTPTransport. Java: 740) ~ [javax.mail. Mail - 1.6.1. Jar: 1.6.1] the at Javax.mail. Mail. Service. The connect (Service. Java: 366) ~ [javax.mail. Mail - 1.6.1. Jar: 1.6.1] the at org.springframework.mail.javamail.JavaMailSenderImpl.connectTransport(JavaMailSenderImpl.java:515) ~ [spring - the context - support - 5.0.8. The jar: 5.0.8. RELEASE] the at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:435) ~ [spring - the context - support - 5.0.8. The jar: 5.0.8. RELEASE] the at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:321) ~ [spring - the context - support - 5.0.8. The jar: 5.0.8. RELEASE]Copy the code

Project configuration

spring:
  mail:
    Configure the SMTP server address
    host: smtp.163.com
    # Email address of sender
    username: [email protected]
    # Configure the password. Note that it is not the real password, but the authorization code just obtained
    password: ajhdasdkaklsjdl
    # Port number 465 or 587
    port: 465
    The default message code is UTF-8
    default-encoding: UTF-8
    # Configure SSL encryption factory
    properties:
      mail:
        smtp:
          socketFactoryClass: javax.net.ssl.SSLSocketFactory
        # indicates that the DEBUG mode is enabled. In this way, the log of the email sending process will be printed in the console for easy troubleshooting
        debug: true
Copy the code

Sending code

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("test")
public class TestController {

    @Autowired
    JavaMailSender javaMailSender;

    @GetMapping("sendMail")
    public void sendMail(a){
        SimpleMailMessage message = new SimpleMailMessage();
        // Set the email subject
        message.setSubject("This is a test email.");
        // Set the email sender as set in application.yml
        message.setFrom("[email protected]");
        // Set the mail receiver. There can be multiple recipients separated by commas
        // message.setTo("10*****[email protected]","12****32*qq.com");
        message.setTo("[email protected]");
        // Set the email sending date
        message.setSentDate(new Date());
        // Set the body of the message
        message.setText("This is the body of the test email."); javaMailSender.send(message); }}Copy the code

Problems in debugging

The suspect is the red line above

  • I clearly set the account, authorization code login, but useAuth is still false
  • IsSsl is false

Found in the error stack com. Sun. Mail. The SMTP. SMTPTransport# protocolConnect

The useAuth attribute is resolved from the protocol type (SMTP, pop3). The complete attribute name should be mail.smtp. Auth

Then,com.sun.mail.smtp.SMTPTransport#openServer(java.lang.String, int) Here,isSSLIs a member attribute

Only in the constructor is assigned according to debuggingcom.sun.mail.smtp.SMTPTransport#SMTPTransport(javax.mail.Session, javax.mail.URLName, java.lang.String, boolean) So the full property name should bemail.smtp.ssl.enableSo just modify the YML file

The solution

	
spring:
  mail:
    Configure the SMTP server address
    host: smtp.163.com
    # Email address of sender
    username: [email protected]
    # Configure the password. Note that it is not the real password, but the authorization code just obtained
    password: ajhdasdkaklsjdl
    # Port number 465 or 587
    port: 465
    The default message code is UTF-8
    default-encoding: UTF-8
    # Configure SSL encryption factory
    properties:
      mail:
        smtp:
          starttls:
            enable: true
          auth: true
          ssl:
            enable: true
          socketFactoryClass: javax.net.ssl.SSLSocketFactory
        # indicates that the DEBUG mode is enabled. In this way, the log of the email sending process will be printed in the console for easy troubleshooting
        debug: true
server:
  port: 8999
  servlet:
    path: /mall
Copy the code