Arunabh
Arunabh

Reputation: 179

How to disable authentication while testing email service using greenmail

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.

Upvotes: 1

Views: 2303

Answers (3)

James Albert
James Albert

Reputation: 31

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

Upvotes: 3

Marcel
Marcel

Reputation: 266

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

Upvotes: 1

Jonathan JOhx
Jonathan JOhx

Reputation: 5978

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/

Upvotes: 0

Related Questions