Sending Email with SMTP Authentication in C# .NET

On my current C# .NET project, the user wants to have the application send an email if a new data is being inserted to the database. I search on the MSDN for class that will do this task and I found SmtpMail class.

Using SmtpMail is very simple, first you need to add a reference on your project. You can do this by right clicking on ‘References’ and click ‘Add References’. On ‘.Net’ tab find ‘System.Web.dll’ and add it to your project.

Now here is the simple code for sending email without SMTP authentication:

using System;
using System.Web.Mail;

MailMessage Message = new MailMessage();
Message.To = “recipient email address”;
Message.From = “sender email address”;
Message.Subject = “email subject”;
Message.Body = “email content”;

SmtpMail.SmtpServer = "your mail server name goes here";
SmtpMail.Send(Message);

For sending an email using a mail server that requires SMTP authentication then the above code need some modification. Here is the example:

MailMessage Message = new MailMessage();

// SMTP Authentication
Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "username goes here"); Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "password goes here");

Message.To = “recipient email address”;
Message.From = “sender email address”;
Message.Subject = “email subject”;
Message.Body = “email content”;


try
{
SmtpMail.SmtpServer = "your mail server name goes here";
SmtpMail.Send(Message);
}
catch(System.Web.HttpException ehttp)
{
MessageBox.Show(ehttp.Message);
}

How I got an error message using that code, the error message is “Could not access ‘CDO.Message’ object”. I’m confused with this error message as it doesn’t giving any detailed information. At first I thought that it’s because I uninstall Outlook Express and doesn’t have Microsoft Outlook installed – a web page said that CDO are being used by Outlook, but it’s not.

After replacing the try catch code above with this one below, I found out that I can’t connect to the mail server although I already specify it correctly on SmtpMail.SmtpServer properties.

try
{
SmtpMail.SmtpServer = "your mail server name goes here";
SmtpMail.Send(Message);
}
catch(Exception ex)
{
string strErrorList;
strErrorList = "The following exception occurred: " + ex.ToString();

//check the InnerException
while( ex.InnerException != null )
{
strErrorList += "\n--------------------------------\n";
strErrorList += "The following InnerException reported: " + ex.InnerException.ToString();
ex = ex.InnerException;
}
}

Although this trick seems odd, but it does work! Try replacing:

SmtpMail.SmtpServer = "your mail server name goes here";

with

SmtpMail.SmtpServer.Insert( 0, "your mail server name goes here");

A good resource website for using System.Web.Mail is SystemWebMail.com. You can visit this web page: The scary “Could not access ‘CDO.Message’ object” if you get CDO.Message error. I got the trick above from this web page also, which linking to a Google group page Google group page

Markus

7 thoughts on “Sending Email with SMTP Authentication in C# .NET”

  1. This code is very common used. You can also use it in combination with accessing a database. For example: you can generate an email newsletter based on email data stored in a database. Unfortunately, problems can occur: other mailservers and anti spam machines can see your mass mail as a spam bomb……there goes your nice formated marketing letter!!! ;)

  2. creating newsletter need more complicated emailing code, as you need to set the email header to bulk so the mail server knows that you send the mail in bulk.

    if your newsletter member is quite big then you need to send the email partially to you member, for example 60 emails per hour or so. this is to reduce the possibility your mail server blacklisted.

    the code above is only simple emailing code, if you look at MSDN the example there is not working. it seems that SmtpMail.SmtpServer doesn’t really set the mail server, that is why the code at MSDN example can’t connect to the mail server.

  3. use this below line
    :
    SmtpMail.SmtpServer = “”;

    i was having the same problem i i got the solution on net ,i had to do it on the same day and i completed it in night 1.30 by this solution s,it will 100% work.

    regards,Mukesh

  4. Pingback: to domain name
  5. Problem with that code is, it will wait until the receiver’s sever will pick up the mail when your server is calling – since this may take 2 – 3 seconds your app seems to hang, if its a desktop app; if its a webapp, your page rendering/loading will seem to hang.
    Only solution is to make this an asynchron task:
    Either by sending the mail in a background thread (by encapsulating the smtp-mail-setup stuff in a class), or use some of the fancy MVC features to accomplish it.
    Regards,

Leave a Reply

Your email address will not be published. Required fields are marked *