SmtpClient 在发送时从服务器获取结果
SmtpClient 发送方法返回 void。有什么办法可以得到服务器的响应吗?我是否只是假设它是成功的,除非它抛出异常?
我指的课程... http://msdn.microsoft. com/en-us/library/system.net.mail.smtpclient.aspx
The SmtpClient send method returns void. Is there any way to get the server response? Do I just assume it was successful unless it throws an exception?
The class I'm referring to... http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要回答你的第二点,是的,你所能做的就是假设它是成功的 - 这意味着它将消息发送到服务器并且服务器接受了它,除非你得到异常。
您可能已经知道其余的内容,但以防万一...
从那里,电子邮件可能会丢失并且无法通过多种方式发送。您的服务器可能会接受它并决定不发送它,或者接受它并在崩溃之前断电。它可能会被垃圾邮件过滤器拦截,等等。
您可以将电子邮件视为与普通邮件相似,因为它在发件人和收件人之间经过了多次处理。从您的代码中,您只能确认它已到达您用于发送的 SMTP 服务器,这类似于将其交给邮局的出纳员。您不知道(或需要知道)消息是如何从那里路由的。它可以是通过空中、地面或信鸽。你不在考虑范围之内——你不需要知道它是如何发送的,只要你相信他们知道如何发送它即可。 (对于电子邮件也是如此。)
如果您需要确认收件人是否打开了它,可以通过多种方法将图像嵌入服务器上的 HTML 消息中,并在访问该图像时在日志中进行跟踪等。(谷歌电子邮件跟踪和电子邮件打开跟踪)
另一方面...
如果服务器拒绝它,那么您确实会以某种方式得到服务器响应 - 错误中应该有错误代码和描述,您可以用于排除未成功的原因,或使用错误处理尝试另一条路线等。
To answer your second point, yes, all you can do is assume it's successful - meaning it got the message to the server and the server accepted it, unless you get an exception.
You probably already know the rest of this, but just in case...
From there, the email could get lost and not delivered any number of ways. Your server may accept it and decide not to send it, or accept it and lose power before crashing. It may get blocked by a spam filter along the way, etc.
You can think of an email as being similar to a regular piece of mail in that it passes through several hands between the sender and the recipient. From your code, you can only confirm that it got to the SMTP server you're using to send, which is similar to handing it to a teller at the post office. You don't know (or need to know) how the message is routed from there. it could be by air, ground, or carrier pigeon. You're out of the equation - you don't need to know how it gets sent, just that you trust that they know how to send it. (The same can be said for an email.)
If you need to confirm that the recipient opened it, there are ways of embedding an image in an HTML message on your server and tracking in your logs when that image is accessed, etc. (Google email tracking and email open tracking)
On the other hand...
If the server rejects it, then you do get a server response in a manner of speaking - there should be an error code and a description in the error, which you can use to troubleshoot why it didn't make it, or use error handling to try another route, etc.
您可以利用 SendCompleted 事件来检查您的 smtp 客户端是否正常工作,如下所示:
http://msdn.microsoft.com/en -us/library/system.net.mail.smtpclient.sendcompleted.aspx
但是您无法确认您的消息已到达收件人,因为它可能卡在消息链中的任何服务器/过滤器中。
You can utilize SendCompleted Event to check that your smtpclient works fine like this:
http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.sendcompleted.aspx
But you cannot get confirmation that your message reached recipient because it may stuck in any server/filter in message chain.
您认为它是成功的,除非它抛出...虽然在这种情况下成功仅意味着它被邮件服务器接受,但其他任何事情都取决于服务器...
如果您想要一点控制权,您可以使用
SendAsync
并挂钩SendCompleted
事件...You assume that it was successful unless it throws... although success in this case only means that it was accepted by the mail server, anything else is then up to the server...
IF you want a little bit of control you can use
SendAsync
and hook theSendCompleted
event...