runtime icon indicating copy to clipboard operation
runtime copied to clipboard

SmtpClient SendAsync Fails to send emails even thought it doesn't error

Open leealexander opened this issue 2 years ago • 9 comments

Description

We utilized SmtpClient.SendAsync for email dispatch, yet faced user reports of undelivered emails, despite our system appearing functional. The issue was eventually linked to specific HTML email body content, suggesting a possible size-related problem—removing a few lines from the email body seemed to resolve the sending issue.

Our emails are sent via Office 365 (there are no logs for the failed emails). To eliminate the possibility of the issue being specific to Office 365, I also tried using a different email provider, but encountered the same problem.

Note this issue only started after we switched to .NET 8 from .NET 6. Also if we change to the synchronous version of the Send method it delivers the email.

Reproduction Steps

I have attached a small console application that replicates the issue. You will of course need to supply your own credentials and delivery emails. TestSendEmail.zip

Expected behavior

Expect delivery of emails.

Actual behavior

Nothing is sent.

Regression?

.NET 6 works

Known Workarounds

Use SmtpClient.Send instead or in our case we moved to MailKit.

Configuration

.NET 8 Windows 11 x64 - although it also fails on docker image mcr.microsoft.com/dotnet/sdk:8.0-alpine as well

Other information

No response

leealexander avatar Mar 05 '24 06:03 leealexander

Tagging subscribers to this area: @dotnet/ncl See info in area-owners.md if you want to be subscribed.

Issue Details

Description

We utilized SmtpClient.SendAsync for email dispatch, yet faced user reports of undelivered emails, despite our system appearing functional. The issue was eventually linked to specific HTML email body content, suggesting a possible size-related problem—removing a few lines from the email body seemed to resolve the sending issue.

Our emails are sent via Office 365 (there are no logs for the failed emails). To eliminate the possibility of the issue being specific to Office 365, I also tried using a different email provider, but encountered the same problem.

Note this issue only started after we switched to .NET 8 from .NET 6. Also if we change to the synchronous version of the Send method it delivers the email.

Reproduction Steps

I have attached a small console application that replicates the issue. You will of course need to supply your own credentials and delivery emails. TestSendEmail.zip

Expected behavior

Expect delivery of emails.

Actual behavior

Nothing is sent.

Regression?

.NET 6 works

Known Workarounds

Use SmtpClient.Send instead or in our case we moved to MailKit.

Configuration

.NET 8 Windows 11 x64 - although it also fails on docker image mcr.microsoft.com/dotnet/sdk:8.0-alpine as well

Other information

No response

Author: leealexander
Assignees: -
Labels:

area-System.Net, untriaged

Milestone: -

ghost avatar Mar 05 '24 06:03 ghost

Use MailKit instead to make life easier: https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient?view=net-8.0#remarks

Edit: Oh glad to see you already do that.

skyoxZ avatar Mar 05 '24 12:03 skyoxZ

we should investigate if this is regression.

wfurt avatar Mar 19 '24 04:03 wfurt

We can confirm this issue, since the update from 6.0 to 8.0, some emails are no longer sent without any exceptions being thrown. The problem does not occur in our development environments but only in the production environment (Azure AKS + Docker). When using Send instead of SendMailAsync, the problem no longer occurs.

We are using the following Docker images: mcr.microsoft.com/dotnet/aspnet:8.0.0-bookworm-slim-amd64 mcr.microsoft.com/dotnet/sdk:8.0.100

dmsch avatar May 16 '24 09:05 dmsch

this seems like regression from https://github.com/dotnet/runtime/pull/82644. When I sync one change back it works. Can you please take a look @filipnavara ?

wfurt avatar Jun 15 '24 00:06 wfurt

@filipnavara will you be able to take a look or should we jump on it? Thanks!

karelz avatar Jun 25 '24 08:06 karelz

@filipnavara will you be able to take a look or should we jump on it? Thanks!

I have been sick for the last week so my schedule is a bit behind and I cannot make any new commitments at the moment. It's on my radar, just with low priority.

I skimmed over Stephen's PR once again but I didn't see anything obviously wrong.

filipnavara avatar Jun 25 '24 08:06 filipnavara

I have been sick for the last week so my schedule is a bit behind and I cannot make any new commitments at the moment. It's on my radar, just with low priority.

Sorry to hear that, thanks for info! We might try to pick it up later on.

karelz avatar Jun 25 '24 09:06 karelz

One option would be to refer the offending change and/or add more tests. Then we can proceed as time permits.

wfurt avatar Jun 25 '24 16:06 wfurt