You might want to wrap your SMTP call in a try...catch - this way you can easily catch any obvious SMTP errors that might occur:
try { SmtpClient client = new SmtpClient(); client.Send(message); } catch(Exception exc) {
This will handle the most obvious errors, for example
- SMTP server not found
- SMTP server is not responding
- SMTP refuses to send you (for example, because you did not provide any valid credentials)
As soon as the SMTP server receives your message, it is out of your hands .NET ... you really can not do much (except checking the SMTP server logs for errors).
If you want to check and verify that your SMTP messages are actually sent, you can also add these lines of code to the app.config (or web.config) application and let .NET put your letters in (as EML files):
<system.net> <mailSettings> <smtp deliveryMethod="SpecifiedPickupDirectory"> <specifiedPickupDirectory pickupDirectoryLocation="C:\temp\mails"/> </smtp> </mailSettings> </system.net>
Now your letters will be saved in the C:\temp\mails as EML files, and you can look at them and check how much they should be.
source share