并非所有在 ASP.NET MVC 中使用 SmtpClient 发送的电子邮件都会被送达。为什么?如何解决?
设置如下:
- 我有一个通知控制器,每天从任务调度程序调用 1 次。
- 操作方法向上拉出 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:
- I have a Notifications controller that is called from task scheduler 1x/day
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
smtp 客户端有一个默认超时,默认值为 100 秒
更多信息在这里
there is a default timeout with the smtp client and default value is 100sec
more info here
如果您没有任何例外,我会检查垃圾邮件文件夹和电子邮件地址。我还尝试从您的 Outlook 手动向未收到消息的地址之一发送电子邮件。
顺便说一句,除非您使用不同的邮件服务器,否则我认为您可以将此代码更改为
您也可以尝试 SmtpClient 类的 SendAsync 方法,如下所示:
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
You might also try SendAsync method of the SmtpClient class, like this:
我强烈建议使用
SmtpClient.SendAsync()
因为 Send() 是阻塞的。我会检查您发送到的 SMTP 服务器的日志。注意:与使用受信任的提供商相比,发送自己的电子邮件更有可能成为垃圾邮件。
编辑:添加了示例 SendAsync 代码
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