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 747c8550009..9f398f09d59 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");