Spring 引导应用程序 return 我:550 访问被拒绝 - 无效的 HELO 名称(参见 RFC2821 4.1.1.1)

Spring boot application return me : 550 Access denied - Invalid HELO name (See RFC2821 4.1.1.1)

在尝试通过我的 spring 启动应用程序发送电子邮件时,我收到了这个错误:

failures; nested exception is javax.mail.MessagingException: Can't send command to SMTP host;
nested exception is:
java.net.SocketException: Software caused connection abort: socket write error. Failed messages: com.sun.mail.smtp.SMTPSendFailedException: 550 Access denied - Invalid HELO name (See RFC2821 4.1.1.1);
nested exception is:
com.sun.mail.smtp.SMTPSenderFailedException: 550 Access denied - Invalid HELO name (See RFC2821 4.1.1.1)

我的 Spring 引导应用程序是用 jhipster 生成的,这是我的应用程序-dev.yml 邮件配置的配置:

mail:   
  host: mail.example.com
  port: 587
  username: support@example.com
  password: ************

注意:example.com 只是一个不共享机密数据的示例,我的配置是正确的,我已经对其进行了测试并且运行良好,但在我的 spring 启动应用程序上却没有

我在下面提供了一个示例。 显示我如何使用 spring-boot

发送电子邮件

在您的 pom.xml 中添加起始邮件依赖项:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

在您的应用程序中添加以下 bean class :

@Bean
public JavaMailSenderImpl customJavaMailSenderImpl(){

    JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
    mailSender.setHost(host);
    mailSender.setPort(port);
     
    mailSender.setUsername(username);
    mailSender.setPassword(password);
     
    Properties props = mailSender.getJavaMailProperties();
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.debug", "true");
     
    return mailSender;

}

查看示例,使用下面的 customJavaMailSenderImpl bean :

@Service
public class emailSender{
   
   @Autowired
   JavaMailSenderImpl customJavaMailSenderImpl;

   public void mailWithAttachment() throws Exception {
    
    MimeMessage message = customJavaMailSenderImpl.createMimeMessage();
      
    MimeMessageHelper helper = new MimeMessageHelper(message, true,"utf-8");
     
    helper.setTo("abc@example.com");
    helper.setSubject("test");
    helper.setText("hello test", true);
    helper.setFrom("abd@example.com", "John Doe");
      
    
 
    customJavaMailSenderImpl.send(message);
    
 }


}

检查后我发现我忘记在 application-dev.yml 中添加一些邮件配置 :

mail.smtp.starttls.enable : truemail.smtp.starttls.auth : true

邮件配置应如下所示:

mail:   
  host: mail.example.com
  port: 587
  username: support@example.com
  password: ************
  properties:
    mail:
      smtp:
        auth: true
        starttls:
          enable: true