SmtpClient get result from server on send
Asked Answered
M

3

17

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

Molarity answered 1/9, 2011 at 17:16 Comment(0)
H
23

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.

Hagbut answered 1/9, 2011 at 17:22 Comment(2)
+1: Along with the "you don't need to know how it gets sent" should be a note explaining that although email normally gets shuttled out pretty quickly, various spam control and server settings could cause a single email to be delayed by anywhere between minutes and days.Albumose
sometimes servers return their message id with 250 ok.. so it's bad to not to see the successful response......Pixilated
A
6

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.

Avra answered 1/9, 2011 at 17:23 Comment(1)
That example doesn't even work. Throws OutOfBounds Array exception on new smptClient(args[0])Runofthemill
K
2

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...

Kymry answered 1/9, 2011 at 17:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.