From 592593f4b85af604ecbe10d36bc21e3c44a74d91 Mon Sep 17 00:00:00 2001 From: Filip Gospodinov Date: Wed, 20 Oct 2021 16:42:49 +0200 Subject: [PATCH] Expose test_connected via transport (#677) It is useful for application developers to validate SMTP settings by testing the connection. Co-authored-by: Alexis Mousset --- src/transport/smtp/async_transport.rs | 15 +++++++++++++++ src/transport/smtp/transport.rs | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/transport/smtp/async_transport.rs b/src/transport/smtp/async_transport.rs index b7f0334..8ed6472 100644 --- a/src/transport/smtp/async_transport.rs +++ b/src/transport/smtp/async_transport.rs @@ -167,6 +167,21 @@ where pool_config: PoolConfig::default(), } } + + /// Tests the SMTP connection + /// + /// `test_connection()` tests the connection by using the SMTP NOOP command. + /// The connection is closed afterwards if a connection pool is not used. + pub async fn test_connection(&self) -> Result { + let mut conn = self.inner.connection().await?; + + let is_connected = conn.test_connected().await; + + #[cfg(not(feature = "pool"))] + conn.quit().await?; + + Ok(is_connected) + } } impl Debug for AsyncSmtpTransport { diff --git a/src/transport/smtp/transport.rs b/src/transport/smtp/transport.rs index f72ec82..5e22b76 100644 --- a/src/transport/smtp/transport.rs +++ b/src/transport/smtp/transport.rs @@ -107,6 +107,21 @@ impl SmtpTransport { pool_config: PoolConfig::default(), } } + + /// Tests the SMTP connection + /// + /// `test_connection()` tests the connection by using the SMTP NOOP command. + /// The connection is closed afterwards if a connection pool is not used. + pub fn test_connection(&self) -> Result { + let mut conn = self.inner.connection()?; + + let is_connected = conn.test_connected(); + + #[cfg(not(feature = "pool"))] + conn.quit()?; + + Ok(is_connected) + } } /// Contains client configuration.