spring启动应用程序return me:550访问被拒绝-helo名称无效(参见rfc2821 4.1.1.1)

oyjwcjzk  于 2021-07-04  发布在  Java
关注(0)|答案(2)|浏览(470)

在尝试通过spring boot应用程序发送电子邮件时,出现以下错误:

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 boot应用程序是用jhipster生成的,下面是我的application-dev.yml config for mail config:

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

注意:example.com只是一个不共享机密数据的例子,我的配置是正确的,我已经用它进行了测试,工作得很好,但不是在我的spring boot应用程序上

dhxwm5r4

dhxwm5r41#

我在下面提供了一个示例。显示我如何发送电子邮件 spring-boot 将starter邮件依赖项添加到 pom.xml :

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

在应用程序类中添加以下bean:

@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);

 }

}
to94eoyn

to94eoyn2#

经过审查,我发现我忘了添加一些邮件配置在我的邮箱 application-dev.yml : mail.smtp.starttls.enable : true 以及 mail.smtp.starttls.auth : true 邮件配置应如下所示:

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

相关问题