spring-projects / spring-projects/spring-boot
Allow verification of mail credentials on startup
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 81.5k
- Forks
- 42.7k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 65
Description
With a standard spring-boot-starter-mail setup and mail server properties configured, one ends up with a MailSender bean in the context that's usually used to send email at some point in the application lifecycle. That said, the credentials are not verified until the first email was sent.
One can register an ApplicationRunner to perform the verification on startup like this:
import jakarta.mail.MessagingException;
import java.util.Optional;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Component;
@Component
@ConditionalOnBean({JavaMailSenderImpl.class, MailProperties.class})
class MailCredentialsVerifier implements ApplicationRunner {
private final JavaMailSenderImpl mailSender;
private final MailProperties properties;
// Constructor and log omitted
@Override
public void run(ApplicationArguments args) throws Exception {
var host = properties.getHost();
var user = properties.getUsername();
try {
mailSender.get().testConnection();
log.info("Successfully verified mail credentials for {} on {}.", user, host);
} catch (MessagingException e) {
log.error("Unable to verify mail credentials for {} on {}.", user, host);
throw new IllegalStateException("Invalid mail credentials orunreachable mail server: " + e.getMessage(), e);
}
}
}
It would be nice if some mechanism like this was available out of the box, maybe behind an opt-in or -out property.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing the standard spring-boot-starter-mail setup, MailSender bean behavior, and the ApplicationRunner/JavaMailSenderImpl example in the issue. Define the opt-in or opt-out configuration and startup failure behavior, then verify that credentials are checked during startup and invalid credentials produce the intended result.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring-boot
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100