tags:

views:

100

answers:

5

I'm using System.Net.Mail to send email, like so :

MailMessage message = new MailMessage();

message.From = new MailAddress("[email protected]");
message.To.Add(new MailAddress("[email protected]"));


message.Subject = "Hello";
message.Body = "This is a nice body..";

SmtpClient client = new SmtpClient();
client.Send(message);

How can i know if the E-mail was sent, can i put in a if sentence to check it out ? What would it look like then ?

A: 

Why not try sending to a valid email address (e.g. create an account with Hotmail or Gmail) and use that instead of "[email protected]"?

Steve Haigh
i'm using a valid email.. i just put this in here..I want the user on the web page to see if it works or not..
eski
OK, I misunderstood your question, I thought you were tryng to debug the code. I think marc_s is correct, you need to try /catch exceptions.
Steve Haigh
+6  A: 

You might want to wrap your SMTP call into a try...catch block - that way you can easily catch any obvious SMTP related errors that might happen:

try
{
   SmtpClient client = new SmtpClient();
   client.Send(message);
}
catch(Exception exc)
{
   // log the error, update your entry - whatever you need to do 
}

This will handle the most obvious errors, like

  • SMTP server not found
  • SMTP server not responding
  • SMTP refusing you to send (e.g. because you didn't provide any or valid credentials)

Once the SMTP server has your message, it's out of your .NET hands.... you can't really do much (except check for the SMTP server's logs for errors).

If you want to check and see whether your SMTP mails actually are "sent out", you can also add these lines of code to your app's app.config (or web.config) and let .NET put your mails into a directory (as EML files):

  <system.net>
    <mailSettings>
      <smtp deliveryMethod="SpecifiedPickupDirectory">
        <specifiedPickupDirectory pickupDirectoryLocation="C:\temp\mails"/>
      </smtp>
    </mailSettings>
  </system.net>

Your mails will now be stored into the C:\temp\mails directory as EML files and you can have a look at them and check to see whether they are as they should be.

marc_s
Alright, i usally use try and catch but sometimes i have seen that people use IF something doesnt work then this..
eski
I think TryCatch is synonymous to IfElse in case of exceptions.
Veer
+1 Very nice. I didn't know about the deliveryMethod.
Steven
A: 

You can install and use the DevNullSmtp server - it does not send any email, but will log and display all messages and traffic.

Oded
A: 

I would consider using 3rd party services (smtp.com comes to mind) to handle messaging. These usually provide tracking api's that can be queried for successful delivery.

Peter Lillevold
+2  A: 

What marc_s says is correct about the use of try/catch.

It's worth noting clearly though that there are no delivery guarantees with SMTP, and trying to work out actual delivery numbers is a very inexact science.

There are a number of techniques that some software attempts to use, like image bugs and tracking links, however these are not fully reliable.

Many servers will silently fail a message with an unknown address or if there are transmission errors, to avoid giving spam providers too much information.

So once you've sent it, if there's no exception, you can only hope that it succeeded. One valuable technique though is to use a regular expression to validate the email address when the user enters it. This helps to avoid some common address problems before they affect mail delivery.

JT