使用Greenmail测试电子邮件服务时如何禁用身份验证

时间:2019-06-23 20:57:41

标签: spring email authentication greenmail

我已经使用Spring Email编写了电子邮件服务,然后使用Greenmail测试了电子邮件服务。但是在运行测试用例时,它会给出错误身份验证失败,如下所示:

javax.mail.AuthenticationFailedException:535 5.7.0身份验证凭据在com.sun.mail.smtp.SMTPTransport $ Authenticator.authenticate(SMTPTransport.java:965)上无效 com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:876)

如果不在代码中输入用户名/密码,如何解决此错误。请注意,我也尝试过提供用户名/密码,但失败并出现相同的异常。感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

greenMail = new GreenMail(new ServerSetup(2525, "127.0.0.1", "smtp"));
greenMail.setUser("username", "secret");

设置绿色邮件实例进行测试时,需要使用setUser并输入 用户名和密码上方,然后在此上方的@One Guy帖子中添加指定的属性。在收到身份验证凭据无效异常后为我工作。

希望这可以帮助遇到问题的任何人

答案 1 :(得分:0)

您似乎没有添加模拟凭据,即位于application-test.yml文件夹中的名为src/test/resources的资源文件。

application.yml

spring:
  mail:
    default-encoding: UTF-8
    host: localhost
    jndi-name:
    username: username
    password: secret
    port: 2525
    properties:
      mail:
        debug: false
        smtp:
          debug: false
          auth: true
          starttls: true
    protocol: smtp
    test-connection: false

您可以通过以下链接查看示例:

https://memorynotfound.com/spring-mail-integration-testing-junit-greenmail-example/

答案 2 :(得分:0)

请参见AuthenticationDisabledTest.java,如何使用禁用的身份验证配置GreenMail junit测试。