SmtpClient get result from server on send

不问归期 提交于 2019-12-04 02:50:47

问题


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


回答1:


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.




回答2:


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.




回答3:


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 the SendCompleted event...



来源:https://stackoverflow.com/questions/7273949/smtpclient-get-result-from-server-on-send

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!