Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add response to SmtpResult #10892

Merged
merged 1 commit into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/OrchardCore.Build/Dependencies.props
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
<PackageManagement Include="Lucene.Net.Analysis.Common" Version="4.8.0-beta00015" />
<PackageManagement Include="Lucene.Net.QueryParser" Version="4.8.0-beta00015" />
<PackageManagement Include="Lucene.Net.Spatial" Version="4.8.0-beta00015" />
<PackageManagement Include="MailKit" Version="2.15.0" />
<PackageManagement Include="MailKit" Version="3.0.0" />
<PackageManagement Include="Markdig" Version="0.26.0" />
<PackageManagement Include="MessagePack" Version="2.2.60" />
<PackageManagement Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageManagement Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
<PackageManagement Include="MimeKit" Version="2.15.1" />
<PackageManagement Include="MimeKit" Version="3.0.0" />
<PackageManagement Include="MiniProfiler.AspNetCore.Mvc" Version="4.2.22" />
<PackageManagement Include="Moq" Version="4.16.1" />
<PackageManagement Include="ncrontab" Version="3.3.1" />
Expand Down
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