dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.06k stars 4.69k forks source link

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

Closed leealexander closed 1 month ago

leealexander commented 7 months ago

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.Sendinstead 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

ghost commented 7 months ago

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](https://github.com/dotnet/runtime/files/14491119/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: -
skyoxZ commented 7 months ago

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.

wfurt commented 6 months ago

we should investigate if this is regression.

dmsch commented 4 months ago

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

wfurt commented 3 months ago

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 ?

karelz commented 3 months ago

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

filipnavara commented 3 months ago

@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.

karelz commented 3 months ago

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.

wfurt commented 3 months ago

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