Spring Email with Greenmail: Authentication Credentials Invalid

1.6k views Asked by At

I am trying to write test code for Spring Email using Greenmail. This works perfectly with 1.5.5 version of Greenmail, however when I try to update the version of Greenmail (1.5.6 to 1.5.11) it keeps giving me an error 535 5.7.8 Authentication credentials invalid.

Application Properties

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

Here are my code

protected static GreenMail smtpServer;

@Autowired
protected EmailRepository emailRepository;

@BeforeClass
public static void beforeClass() {

    smtpServer = new GreenMail(new ServerSetup(3025, null, ServerSetup.PROTOCOL_SMTP ));
    smtpServer.setUser("username", "secret");
    smtpServer.start();
}

@Before
public void before() {
    emailRepository.deleteAll();
    smtpServer.reset();
}

@After
public void after() {
    emailRepository.deleteAll();
}

@AfterClass
public static void afterClass() {
    smtpServer.stop();
}

protected List<Email> createEmailRequests(int size, EmailStatus status) {
    int counter = 0;
    List<Email> emails = new ArrayList<>();

    do {
        MetaInfo metaInfo = new MetaInfo();
        //metainfo details

        Email email = new Email();
        //email details
        emails.add(email);

        counter++;
    } while (counter < size);

    emailRepository.saveAll(emails);
    return emails;
}
3

There are 3 answers

0
Rajko On

code with Greenmail 1.5.5 (that gives: jakarta.mail.AuthenticationFailedException: 535 5.7.8 Authentication credentials invalid with 2.0.0)

    var setup = new ServerSetup(PORT, HOST, "smtp");
    smtpServer = new GreenMail(setup);
    smtpServer.setUser(USER_EMAIL, USERNAME, TEST_PWWD);
    smtpServer.reset();

code with Greenmail 2.0.0

    var setup = new ServerSetup(PORT, HOST, "smtp");
    smtpServer = new GreenMail(setup)
            .withConfiguration(GreenMailConfiguration.aConfig()
                    .withUser(USER_EMAIL, USERNAME, TEST_PWWD));
    smtpServer.start();

this made it work for me

0
GeorgePap On

I had the same problem with greenmail-1.6.8.

According to this, I have created the GreenMail object as follows

GreenMail greenMail = new GreenMail(new ServerSetup(3025, null, "smtp")).withConfiguration(GreenMailConfiguration.aConfig().withDisabledAuthentication());
greenMail.start();

And all tests passed

0
antonyh On

I had the same problem with 1.5.13, it will work if you remove the credentials from the config/code.

# spring.mail.username=username     
# spring.mail.password=secret

and from the beforeClass() method:

// smtpServer.setUser("username", "secret");

Greenmail accepts all inbound connections without the need for authentication, but it's unclear why 1.5.5 worked like this and why it doesn't with the newer versions.