简体   繁体   English

如何知道我是否使用System.Net.Mail成功发送电子邮件

[英]How to know whether I send the email successfully using System.Net.Mail

I use C# to send email using System.Net.Mail. 我使用C#使用System.Net.Mail发送电子邮件。

But How can I know whether the email has been sent successfully or fail? 但我怎么知道电子邮件是成功发送还是失败?

当然不是一个直接的答案,但这里有一篇来自Coding Horror的关于通过代码发送电子邮件的好文章,它解决了你的问题。

The simple answer is that if it's a detectable failure then SmtpClient.Send will throw an SmtpFailedRecipientsException (it will throw an SmtpException if there is a connection problem). 简单的答案是,如果它是可检测的失败,那么SmtpClient.Send将抛出一个SmtpFailedRecipientsException (如果存在连接问题,它将抛出一个SmtpException )。

The long answer is that, for many reasons, you won't necessarily get an exception. 答案很长,因为很多原因,你不一定会得到例外。 This can be because your SMTP server is buffering the sends (or if you have it set to local iis or local directory), or simply because the SMTP server does not support returning those error codes. 这可能是因为您的SMTP服务器正在缓冲发送(或者如果您将其设置为本地iis或本地目录),或者仅仅因为SMTP服务器不支持返回这些错误代码。

Most advertising mailing systems track bouncebacks by setting a valid "from address" and monitoring the inbox for that account. 大多数广告邮件系统通过设置有效的“来自地址”并监控该帐户的收件箱来跟踪回弹。 Even that, however, is not foolproof (out of office messages, for example). 然而,即使这样也不是万无一失的(例如,不在办公室的消息)。

I would stick to catching exceptions and leaving the others. 我会坚持抓住例外并留下其他人。 If you're not sure if emails are valid, maybe you should send users a code to have them validate their email address. 如果您不确定电子邮件是否有效,可能您应该向用户发送一个代码,让他们验证他们的电子邮件地址。

Considering Emails are fire-and-forget, this can be quite hard. 考虑到电子邮件是即发即忘,这可能会非常困难。 Unless you get an exception, denoting a client-server communication issue due to wrong credentials etc, you have no means of knowing if a mail was sucessfully send (meaning: arrived at the addressee). 除非您收到异常,表示由于凭据错误等导致的客户端 - 服务器通信问题,否则您无法知道邮件是否成功发送(意味着:到达收件人)。

I've personally never come across an answer to this as there's not really a way you can grantee mail leaving your location is'nt going to get thrown away as soon as it leaves you. 我个人从来没有遇到过这方面的答案,因为没有一种方法可以让你的邮件离开你的位置,一旦离开你就不会被扔掉。 You could maybe try and check the sending mailbox for an NDR (none delivery report) but your not always granted to get one 您可以尝试检查发送邮箱是否有NDR(无交付报告)但您并不总是被授予获得一个

If you mean sent successfully then as other posters say, if it cannot be sent then you will get an exception. 如果你的意思是成功发送然后其他海报说,如果它不能发送,那么你将得到一个例外。 If you actually mean recieved succesfully then that is an entirely different matter. 如果你真的意味着成功收到那么这是完全不同的事情。 SMTP simply does not have a mechanism to find out if the message has been recieved by the recipient, sometimes you will get an ndr if a server can't deliver other times you won't. SMTP根本没有一种机制来查明收件人是否已收到邮件,有时如果服务器无法提供其他时间,您将获得一个ndr。 You could try and do what spammers do and that is to put some kind of image link in your html email body with a unique identifier in the query string. 您可以尝试做垃圾邮件发送者所做的事情,那就是在您的html电子邮件正文中使用查询字符串中的唯一标识符放置某种图像链接。 That would allow you to link someone hitting the image url with the email. 这样您就可以将点击图片网址的人与电子邮件相关联。 However that would only tell you the email had been read if the url was hit, it would not tell you that the email hadn't been recieved becuase the recipient might simply not have allowed images to be displayed in their email client. 然而,这只会告诉您如果网址被点击已经阅读了电子邮件,它不会告诉您电子邮件没有被收到,因为收件人可能根本就没有允许图像显示在他们的电子邮件客户端中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM