Skip to content

Commit bc41000

Browse files
authored
AzureEmailSender - Always Map Message ID in SendResponse Objects for Diagnostic Support (#26)
* Update AzureEmailSender.cs Map MessageId always when is available * Changed Async task completion
1 parent 4a352fe commit bc41000

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/FluentEmail.Core/FluentEmail.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121

2222
<ItemGroup>
23-
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
23+
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.0" />
2424
</ItemGroup>
2525

2626
</Project>

src/Senders/FluentEmail.Azure.Email/AzureEmailSender.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,12 @@ public async Task<SendResponse> SendAsync(IFluentEmail email, CancellationToken?
138138
_ => 3
139139
}).ToString());
140140

141+
string messageId = null;
141142

142143
try
143144
{
144145
EmailSendOperation sendOperation = await _emailClient.SendAsync(WaitUntil.Started, emailMessage, token ?? CancellationToken.None);
145-
var messageId = sendOperation.Id;
146+
messageId = sendOperation.Id;
146147
if (string.IsNullOrWhiteSpace(messageId))
147148
{
148149
return new SendResponse
@@ -153,10 +154,10 @@ public async Task<SendResponse> SendAsync(IFluentEmail email, CancellationToken?
153154

154155
// We want to verify that the email was sent.
155156
// The maximum time we will wait for the message status to be sent/delivered is 2 minutes.
156-
var cancellationToken = new CancellationTokenSource(TimeSpan.FromMinutes(2));
157-
var sendStatusResult = sendOperation.WaitForCompletion(cancellationToken.Token).Value;
157+
using var cancellationToken = new CancellationTokenSource(TimeSpan.FromMinutes(2));
158+
var sendStatusResult = await sendOperation.WaitForCompletionAsync(cancellationToken.Token);
158159

159-
if (sendStatusResult.Status == EmailSendStatus.Succeeded)
160+
if (sendStatusResult.Value.Status == EmailSendStatus.Succeeded)
160161
{
161162
return new SendResponse
162163
{
@@ -168,19 +169,22 @@ public async Task<SendResponse> SendAsync(IFluentEmail email, CancellationToken?
168169
{
169170
return new SendResponse
170171
{
172+
MessageId = messageId,
171173
ErrorMessages = new List<string> { "Failed to send email, timed out while getting status." }
172174
};
173175
}
174176

175177
return new SendResponse
176178
{
179+
MessageId = messageId,
177180
ErrorMessages = new List<string> { "Failed to send email." }
178181
};
179182
}
180183
catch (Exception ex)
181184
{
182185
return new SendResponse
183186
{
187+
MessageId = messageId,
184188
ErrorMessages = new List<string> { ex.Message }
185189
};
186190
}

0 commit comments

Comments
 (0)