Skip to content

Commit

Permalink
Add response to SmtpResult (#10892)
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamco authored Jan 10, 2022
1 parent 070a9c1 commit 928f314
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/OrchardCore/OrchardCore.Email.Abstractions/SmtpResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public class SmtpResult
/// </summary>
public IEnumerable<LocalizedString> Errors { get; protected set; }

/// <summary>
/// Get or sets thet response text from the SMTP server.
/// </summary>
public string Response { get; set; }

/// <summary>
/// Whether if the operation succeeded or not.
/// </summary>
Expand Down
20 changes: 15 additions & 5 deletions src/OrchardCore/OrchardCore.Email.Core/Services/SmtpService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public async Task<SmtpResult> 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)
Expand All @@ -78,7 +80,7 @@ public async Task<SmtpResult> 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);
Expand All @@ -87,12 +89,16 @@ public async Task<SmtpResult> 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)
Expand Down Expand Up @@ -204,7 +210,7 @@ private bool CertificateValidationCallback(object sender, X509Certificate certif

return false;
}
private async Task SendOnlineMessage(MimeMessage message)
private async Task<string> SendOnlineMessage(MimeMessage message)
{
var secureSocketOptions = SecureSocketOptions.Auto;

Expand Down Expand Up @@ -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;
}
}

Expand Down
24 changes: 24 additions & 0 deletions test/OrchardCore.Tests/Email/EmailTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[email protected]",
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<string> SendEmailAsync(MailMessage message, string defaultSender = null)
{
var pickupDirectoryPath = Path.Combine(Directory.GetCurrentDirectory(), "Email");
Expand Down

0 comments on commit 928f314

Please sign in to comment.