并非所有在 ASP.NET MVC 中使用 SmtpClient 发送的电子邮件都会被送达。为什么?如何解决?

发布于 2024-08-22 03:59:42 字数 1525 浏览 2 评论 0原文

设置如下:

  1. 我有一个通知控制器,每天从任务调度程序调用 1 次。
  2. 操作方法向上拉出 300 个地址,循环访问它们,并使用 SmtpClient 类向每个收件人发送单独的电子邮件。

据我所知,该过程运行良好,没有任何例外......除了并非所有电子邮件都已送达。有人对发生的事情以及如何解决有任何想法吗?

下面是代码:

foreach (var emp in division.Users)
{
    var fromAddress = "myfromaddress";
    var mailServer = "mymailserver";

    var toEmail = emp.EmailAddress;

    var message = new MailMessage(fromAddress, toEmail)
                    {
                        Subject = subject,
                        Body = "<body style='font:normal 13px tahoma,arial,helvetica;'>" + body + "</body>",
                        IsBodyHtml = true
                    };

    var client = new SmtpClient(mailServer);
    client.Send(message);
}

更新:

在发送电子邮件之间添加暂停可以解决该问题。但为什么这会起作用呢?有没有更好的方法(例如使用 Async())同样可以更好地解决问题???

更新了代码...

foreach (var emp in division.Users)
{
    var fromAddress = "myfromaddress";
    var mailServer = "mymailserver";

    var toEmail = emp.EmailAddress;

    var message = new MailMessage(fromAddress, toEmail)
                    {
                        Subject = subject,
                        Body = "<body style='font:normal 13px tahoma,arial,helvetica;'>" + body + "</body>",
                        IsBodyHtml = true
                    };

    var client = new SmtpClient(mailServer);
    client.Send(message);

    Thread.Sleep(3000); // Wait 3s until sending next message
}

Here is the set-up:

  1. I have a Notifications controller that is called from task scheduler 1x/day
  2. The action method pulls upwards of 300 addresses, loops thru them and uses the SmtpClient class to send an individual e-mail to each recepient.

From what I can tell the process runs fine with no exceptions ... except that not all e-mails are being delivered. Anyone have any ideas on what is going on and how to resolve?

Here is the code:

foreach (var emp in division.Users)
{
    var fromAddress = "myfromaddress";
    var mailServer = "mymailserver";

    var toEmail = emp.EmailAddress;

    var message = new MailMessage(fromAddress, toEmail)
                    {
                        Subject = subject,
                        Body = "<body style='font:normal 13px tahoma,arial,helvetica;'>" + body + "</body>",
                        IsBodyHtml = true
                    };

    var client = new SmtpClient(mailServer);
    client.Send(message);
}

UPDATE:

Adding a pause in between sending e-mails resolves the problem. But why does this work? And is there a better way (e.g. using Async()) that would equally resolve the issue in a better way???

Updated code ...

foreach (var emp in division.Users)
{
    var fromAddress = "myfromaddress";
    var mailServer = "mymailserver";

    var toEmail = emp.EmailAddress;

    var message = new MailMessage(fromAddress, toEmail)
                    {
                        Subject = subject,
                        Body = "<body style='font:normal 13px tahoma,arial,helvetica;'>" + body + "</body>",
                        IsBodyHtml = true
                    };

    var client = new SmtpClient(mailServer);
    client.Send(message);

    Thread.Sleep(3000); // Wait 3s until sending next message
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小帐篷 2024-08-29 03:59:42

smtp 客户端有一个默认超时,默认值为 100 秒

更多信息在这里

there is a default timeout with the smtp client and default value is 100sec

more info here

懒猫 2024-08-29 03:59:42

如果您没有任何例外,我会检查垃圾邮件文件夹和电子邮件地址。我还尝试从您的 Outlook 手动向未收到消息的地址之一发送电子邮件。

顺便说一句,除非您使用不同的邮件服务器,否则我认为您可以将此代码更改为

var client = new SmtpClient(mailServer); 
var mailServer = "mymailserver";

foreach (var emp in division.Users) 
{ 
    var fromAddress = "myfromaddress"; 


    var toEmail = emp.EmailAddress; 

    var message = new MailMessage(fromAddress, toEmail) 
                    { 
                        Subject = subject, 
                        Body = "<body style='font:normal 13px tahoma,arial,helvetica;'>" + body + "</body>", 
                        IsBodyHtml = true 
                    }; 


    client.Send(message); 
} 

您也可以尝试 SmtpClient 类的 SendAsync 方法,如下所示:

// setup the callback method when the send finishes
client.SendCompleted += SendComplete; //new SendCompletedEventHandler(smtpSender_SendCompleted);

// send the email
client.SendAsync(message, null);



private void SendComplete(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
    // do stuff on complete
}

If you are not having any exceptions I'd check SPAM folders and email addresses. I'd also try sending an email manually from your outlook to one of the addresses that didn't recieve a message.

On a side note, unless you are using different mail servers, I think you can change this code to

var client = new SmtpClient(mailServer); 
var mailServer = "mymailserver";

foreach (var emp in division.Users) 
{ 
    var fromAddress = "myfromaddress"; 


    var toEmail = emp.EmailAddress; 

    var message = new MailMessage(fromAddress, toEmail) 
                    { 
                        Subject = subject, 
                        Body = "<body style='font:normal 13px tahoma,arial,helvetica;'>" + body + "</body>", 
                        IsBodyHtml = true 
                    }; 


    client.Send(message); 
} 

You might also try SendAsync method of the SmtpClient class, like this:

// setup the callback method when the send finishes
client.SendCompleted += SendComplete; //new SendCompletedEventHandler(smtpSender_SendCompleted);

// send the email
client.SendAsync(message, null);



private void SendComplete(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
    // do stuff on complete
}
归属感 2024-08-29 03:59:42

我强烈建议使用 SmtpClient.SendAsync() 因为 Send() 是阻塞的。

我会检查您发送到的 SMTP 服务器的日志。注意:与使用受信任的提供商相比,发送自己的电子邮件更有可能成为垃圾邮件。

编辑:添加了示例 SendAsync 代码

smtpClient.SendCompleted += smtpClient_SendCompleted;

static void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
    if (e.Error != null)
    {
        Log.ErrorFormat("Async Mail exception {0} :: {1}", 
            e.Error.GetType().Name, e.Error.Message);
    }
}

I would strongly advice on using SmtpClient.SendAsync() as Send() is blocking.

I would check the logs of the SMTP server that you're sending to. Note: sending your own emails are more likely to end in Junk mail than using a trusted provider.

Edit: added sample SendAsync code

smtpClient.SendCompleted += smtpClient_SendCompleted;

static void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
    if (e.Error != null)
    {
        Log.ErrorFormat("Async Mail exception {0} :: {1}", 
            e.Error.GetType().Name, e.Error.Message);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文