失败的消息:javax.mail.MessagingException:需要STARTTLS,但是主机不支持STARTTLS

时间:2018-12-14 15:55:09

标签: java spring-boot smtp tls1.2 starttls

有人可以帮忙吗? 我创建了SMTP服务器并添加了SMTP客户端。两者都在一个应用程序中运行。我尝试通过TLS将邮件从本地主机发送到localhost:1025 SMTP服务器,但是出现错误:

发送电子邮件时出错:org.springframework.mail.MailSendException:邮件服务器连接失败;嵌套的异常是javax.mail.MessagingException:需要STARTTLS,但是主机不支持STARTTLS。失败消息:javax.mail.MessagingException:必需STARTTLS,但是主机不支持STARTTLS。消息异常(1)是: 失败的消息1:javax.mail.MessagingException:需要STARTTLS,但主机不支持STARTTLS

我做错了什么?

application.properties

spring.profiles.active=local

smtpserver.enabled=true
smtpserver.hostName=localhost
smtpserver.port=1025

#enable/diable https
server.ssl.enabled=true
#ssl ciphers
server.ssl.ciphers=TLS_RSA_WITH_AES_128_CBC_SHA256, TLS_RSA_WITH_AES_256_CBC_SHA, TLS_RSA_WITH_AES_128_CBC_SHA, TLS_RSA_WITH_AES_256_CBC_SHA256

# SSL protocol to use.
server.ssl.protocol=TLS
# Enabled SSL protocols.
server.ssl.enabled-protocols=TLSv1.2
server.ssl.key-store = certificate.p12
server.ssl.key-store-password = password
server.ssl.key-store-type = PKCS12

#send
spring.mail.host=localhost
spring.mail.port=1025
spring.mail.username=username
spring.mail.password=password
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.timeout=5000
spring.mail.properties.mail.smtp.writetimeout=5000

发送电子邮件的控制器

@RestController
@ResponseBody
public class RestTest
{
    private static final Logger LOG = LoggerFactory.getLogger(Application.class);

    @Autowired
    private JavaMailSender sender;

    @GetMapping("/test")
    public ResponseEntity<String> test()
    {
        return ResponseEntity.ok("hi");
    }

    @GetMapping("/send")
    public String sendEmailGet()
    {
        try
        {
            LOG.info("Before send");
            sendEmail();
            LOG.info("After send");
            return "Email Sent!";
        }
        catch (Exception ex)
        {
            LOG.error("Error: {}", ex.getMessage());
            return "Error in sending email: " + ex;
        }

    }

    private void sendEmail()
        throws Exception
    {
        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message);

        message.setSender(new InternetAddress("client@test.com"));
        helper.setTo("test@gmail.com");

        helper.setText("TEST");

        helper.setSubject("SUBJECT");

        LOG.info("Sending");
        sender.send(message);
    }

}

SMTP服务器

@Service
public class SMTPServerService
{
    private static final Logger LOG = LoggerFactory.getLogger(SMTPServerService.class);

    @Value("${smtpserver.enabled:false}")
    String enabled  = "";
    @Value("${smtpserver.hostName:localhost}")
    String hostName = "";
    @Value("${smtpserver.port:25}")
    String port     = "";

    SMTPServer smtpServer;

    @PostConstruct
    public void start()
    {
        if (BooleanUtils.toBoolean(enabled))
        {
            SimpleMessageListenerImpl l = new SimpleMessageListenerImpl();
            smtpServer = new SMTPServer(new SimpleMessageListenerAdapter(l));
            smtpServer.setHostName(this.hostName);
            smtpServer.setPort(Integer.valueOf(port));
            smtpServer.setRequireTLS(true);
            smtpServer.start();

            LOG.info("******SMTP Server is running for domain {} on port {}", smtpServer.getHostName(),
                     smtpServer.getPort());

        }
        else
        {
            LOG.warn("SMTP Server NOT ENABLED by settings");
        }
    }

    @PreDestroy
    public void stop()
    {
        if (BooleanUtils.toBoolean(enabled))
        {
            LOG.info("******SStopping SMTP Server for domain {} on port {}", smtpServer.getHostName(),
                     smtpServer.getPort());
            smtpServer.stop();
        }
    }

    public boolean isRunning()
    {
        return smtpServer.isRunning();
    }
}

证书: Certificates

0 个答案:

没有答案