Add testConnection on JavaMailSender

Add a way to test that a particular JavaMailSender instance can connect
to the server that it is configured for.

Issue: SPR-12799
This commit is contained in:
Stephane Nicoll
2015-03-10 16:58:29 +01:00
parent 73e8021e59
commit a64532ede2
2 changed files with 73 additions and 19 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -372,6 +372,21 @@ public class JavaMailSenderImpl implements JavaMailSender {
}
}
/**
* Validate that this instance can connect to the server that it is configured
* for. Throws a {@link MessagingException} if the connection attempt failed.
*/
public void testConnection() throws MessagingException {
Transport transport = null;
try {
transport = connectTransport();
}
finally {
if (transport != null) {
transport.close();
}
}
}
/**
* Actually send the given array of MimeMessages via JavaMail.