From 2b8ad96821e4825f483d862f65573c746680d3a7 Mon Sep 17 00:00:00 2001 From: Hisham Bin Ateya Date: Fri, 17 Dec 2021 11:23:51 +0300 Subject: [PATCH] Add response to SmtpResult --- src/OrchardCore.Build/Dependencies.props | 4 ++-- .../SmtpResult.cs | 5 ++++ .../Services/SmtpService.cs | 20 ++++++++++++---- test/OrchardCore.Tests/Email/EmailTests.cs | 24 +++++++++++++++++++ 4 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/OrchardCore.Build/Dependencies.props b/src/OrchardCore.Build/Dependencies.props index bc31925b5cf..44ac7454281 100644 --- a/src/OrchardCore.Build/Dependencies.props +++ b/src/OrchardCore.Build/Dependencies.props @@ -24,12 +24,12 @@ - + - + diff --git a/src/OrchardCore/OrchardCore.Email.Abstractions/SmtpResult.cs b/src/OrchardCore/OrchardCore.Email.Abstractions/SmtpResult.cs index cf7fade5b1e..bb5c93199f6 100644 --- a/src/OrchardCore/OrchardCore.Email.Abstractions/SmtpResult.cs +++ b/src/OrchardCore/OrchardCore.Email.Abstractions/SmtpResult.cs @@ -18,6 +18,11 @@ public class SmtpResult /// public IEnumerable Errors { get; protected set; } + /// + /// Get or sets thet response text from the SMTP server. + /// + public string Response { get; set; } + /// /// Whether if the operation succeeded or not. /// diff --git a/src/OrchardCore/OrchardCore.Email.Core/Services/SmtpService.cs b/src/OrchardCore/OrchardCore.Email.Core/Services/SmtpService.cs index 7b01d32a736..eac1c4cd4b1 100644 --- a/src/OrchardCore/OrchardCore.Email.Core/Services/SmtpService.cs +++ b/src/OrchardCore/OrchardCore.Email.Core/Services/SmtpService.cs @@ -56,6 +56,8 @@ public async Task SendAsync(MailMessage message) return SmtpResult.Failed(S["SMTP settings must be configured before an email can be sent."]); } + SmtpResult result; + var response = default(string); try { // Set the MailMessage.From, to avoid the confusion between _options.DefaultSender (Author) and submitter (Sender) @@ -78,7 +80,7 @@ public async Task SendAsync(MailMessage message) switch (_options.DeliveryMethod) { case SmtpDeliveryMethod.Network: - await SendOnlineMessage(mimeMessage); + response = await SendOnlineMessage(mimeMessage); break; case SmtpDeliveryMethod.SpecifiedPickupDirectory: await SendOfflineMessage(mimeMessage, _options.PickupDirectoryLocation); @@ -87,12 +89,16 @@ public async Task SendAsync(MailMessage message) throw new NotSupportedException($"The '{_options.DeliveryMethod}' delivery method is not supported."); } - return SmtpResult.Success; + result = SmtpResult.Success; } catch (Exception ex) { - return SmtpResult.Failed(S["An error occurred while sending an email: '{0}'", ex.Message]); + result = SmtpResult.Failed(S["An error occurred while sending an email: '{0}'", ex.Message]); } + + result.Response = response; + + return result; } private MimeMessage FromMailMessage(MailMessage message) @@ -204,7 +210,7 @@ private bool CertificateValidationCallback(object sender, X509Certificate certif return false; } - private async Task SendOnlineMessage(MimeMessage message) + private async Task SendOnlineMessage(MimeMessage message) { var secureSocketOptions = SecureSocketOptions.Auto; @@ -243,8 +249,12 @@ private async Task SendOnlineMessage(MimeMessage message) await client.AuthenticateAsync(_options.UserName, _options.Password); } } - await client.SendAsync(message); + + var response = await client.SendAsync(message); + await client.DisconnectAsync(true); + + return response; } } diff --git a/test/OrchardCore.Tests/Email/EmailTests.cs b/test/OrchardCore.Tests/Email/EmailTests.cs index c82747d11ff..80029071aa2 100644 --- a/test/OrchardCore.Tests/Email/EmailTests.cs +++ b/test/OrchardCore.Tests/Email/EmailTests.cs @@ -214,6 +214,30 @@ public async Task SendEmail_WithoutToAndCcAndBccHeaders_ShouldThrowsException() Assert.True(result.Errors.Any()); } + [Fact] + public async Task SendOfflineEmailHasNoResponse() + { + // Arrange + var message = new MailMessage + { + To = "info@oc.com", + Subject = "Test", + Body = "Test Message" + }; + var settings = new SmtpSettings + { + DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory + }; + + var smtp = CreateSmtpService(settings); + + // Act + var result = await smtp.SendAsync(message); + + // Assert + Assert.Null(result.Response); + } + private async Task SendEmailAsync(MailMessage message, string defaultSender = null) { var pickupDirectoryPath = Path.Combine(Directory.GetCurrentDirectory(), "Email");