dotnet / runtime

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

使用 SmtpClient 发送带有附件的邮件 有一定几率会触发“操作已超时”错误 #107128

Open 2506328575 opened 2 weeks ago

2506328575 commented 2 weeks ago

Description

操作已超时

Reproduction Steps

  MailAddress toMail = new MailAddress(ReceiveUser, ReceiveName);
            MailAddress fromMail = new MailAddress(AppSettingsHelper.GetConfigStr("EmailFrom"),
                AppSettingsHelper.GetConfigStr("EmailUserName"));

            // 创建 MailMessage 对象,并设置主题、内容、是否支持 HTML 等属性
            MailMessage mail = new MailMessage(fromMail, toMail);
            mail.Subject = MailTitle;
            mail.IsBodyHtml = true;
            mail.Body = MailContent;

            // 添加附件
            foreach (var filePath in attachFiles)
            {
                Attachment attachment = new Attachment(filePath);
                mail.Attachments.Add(attachment);
            }

            // 创建 SmtpClient 对象,并设置相关属性
            SmtpClient client = new SmtpClient();
            client.Timeout = 100; //设置超时时间
            client.EnableSsl = true;
            client.Host = AppSettingsHelper.GetConfigStr("EmailAddress");
            client.Port = 587;
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential(AppSettingsHelper.GetConfigStr("EmailFrom"),
                AppSettingsHelper.GetConfigStr("EmailPwd"));
            client.DeliveryMethod = SmtpDeliveryMethod.Network;

            // 发送邮件
            client.Send(mail);

Expected behavior

能够解决这个问题

Actual behavior

报错

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

dotnet-policy-service[bot] commented 2 weeks ago

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

rzikm commented 2 weeks ago

操作已超时 The operation timed out.

This sounds like the mail server may be temporarily unavailable, which email server are you using? is it possible to ping the server using the ping cli command?

If you think this is a problem in .NET, what version of .NET are you using?. Note that SmtpClient not maintained and its use is discouraged and it is recommended to use other mail clients such as MailKit.

ergunr commented 2 weeks ago

Holla

Envoyé de mon iPhone

Le 29 août 2024 à 14:04, Radek Zikmund @.***> a écrit :



操作已超时 The operation timed out.

This sounds like the mail server may be temporarily unavailable, which email server are you using? is it possible to ping the server using the ping cli command?

If you think this is a problem in .NET, what version of .NET are you using?. Note that SmtpClient not maintained and its use is discouraged and it is recommended to use other mail clients such as MailKit.

— Reply to this email directly, view it on GitHubhttps://github.com/dotnet/runtime/issues/107128#issuecomment-2317332275, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AUW2LQZXA3FYE44F3MPAKXTZT354BAVCNFSM6AAAAABNJ6TNZCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMJXGMZTEMRXGU. You are receiving this because you are subscribed to this thread.Message ID: @.***>

wfurt commented 2 weeks ago

firewall policies for ping and SMTP may be different. I would suggest to get packet captures or simply try simple Socket app or something line nc

dotnet-policy-service[bot] commented 2 weeks ago

This issue has been marked needs-author-action and may be missing some important information.

dotnet-policy-service[bot] commented 1 day ago

This issue has been automatically marked no-recent-activity because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will remove no-recent-activity.