1

I have written an email service using Spring Email and then used Greenmail to test the email service. But while running the test case it gives an error Authentication failed as below:

javax.mail.AuthenticationFailedException: 535 5.7.0 Authentication credentials invalid at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:965) at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:876)

How can this error be resolved without giving my username/password in the code. Note that I have also tried giving username/password but it failed with the same exception. Thanks for any help.

Arunabh
  • 167
  • 1
  • 7
  • 18

3 Answers3

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

When Setting up your greenmail instance for testing you need to use the setUser and enter username and password above and then add the properties specified in the @One Guy post above this. has worked for me after I received the Authentication credentials invalid Exception.

Hope this helps anyone facing a the issue

1

See AuthenticationDisabledTest.java how to configure a GreenMail junit test using disabled authentication.

Marcel
  • 266
  • 2
  • 5
0

Looks like you don't added the mock credentials i.e. a a resource file called application-test.yml file which is located in the src/test/resources folder.

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

You can take a look an example by following link:

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

Jonathan JOhx
  • 5,784
  • 2
  • 17
  • 33
  • Thanks for reply. I added the above tag in yaml file as it is but got the same authentication exception. Tried making the "auth: false" which is now giving a new exception: IllegalStateException Could not start mail server smtps:127.0.0.1:3456, try to set server startup time out>1000... – Arunabh Jun 24 '19 at 06:03