Sending Mail Using C#

Posted on September 14th, 2009, by Joel Comments Off

Sending mail in PHP is pretty easy, all you need to do is check if your Linux server (if that’s what you are running) has email capability. However in .net you need SMTP server host to send mails, so here is a easy tutorial as well as source code to send mails using C#.

Prerequisites

To test your code properly, you must have an SMTP host, either running in your local machine or a remote server.

Lets Get Started

So lets get started, first of all lets look at the namespaces we will be using, please note that we will be using System.Net.Mail and not System.Web.Mail, System.Web.Mail is an outdated class. So here is the list of namespaces we will be using.

using System;
using System.Collections.Generic;
using System.Web;
using System.Net.Mail;
using System.Text;
using System.Collections;

Now lets create a new message body, for that you can use a just a string object or a StringBuilder object. If you want your mail to be plain text and basically small (1-5 lines) you can use string, but I advise you to use StringBuilder as it gives more flexibility and easy to use for HTML mails.

StringBuilder MailBody = new StringBuilder();
MailBody.Append("<html><head></head><body><p>");//open body
MailBody.Append("Hello this is a test mail.<br /><br />");
MailBody.Append("This is using C#<br />");
MailBody.Append("And it was not even hard<br />");
MailBody.Append("Please note that this is a system generated mail, therefore do not respond to this mail, Jus Kidding.<br />");
MailBody.Append("</p></body>");//closebody

The above is for a HTML mail, if you do not want to create a html mail, you can also use a string

string MailB = "This is a test message";

I will be using the StringBuilder in this example.

Now lets create a Generic Dictionary to add the mail content, I like generic datatypes coz they are typesafe and thus avoids a lot of casting. You could also use a normal Dictionary object.

Dictionary<string, string> MailContent = new Dictionary<string, string>();
MailContent.Add("From", "wizard@dotnetwizard.net");
MailContent.Add("To", "blacknoise@dotnetwizard.net");
MailContent.Add("Subject", "This is a Test Message");
MailContent.Add("Body", MailBody.ToString());

Now that we have done all the main things lets get to sending the mail, through the SMTP client.
For the sending mail method to connect to the SMTP host add the host domain, username and password in the config file like the following, this should be added directly under the <configuration> node of the config file.

<!-- Add the email settings to the <system.net> element -->
<system.net>
<mailSettings>
<smtp>
<network host="smtp.yoursite.com" port="25" userName="myacc@yoursite.com" password="mypass123"/>
</smtp>
</mailSettings>
</system.net>

Now lets create the actual sending mail part, for this lets create method which accepts the Dictionary we created previously as a parameter.

public void MailMessage(Dictionary<string, string> MC)
{
System.Net.Mail.MailMessage MM = new MailMessage(MC["From"], MC["To"], MC["Subject"], MC["Body"]);
MM.IsBodyHtml = true; //if you are sending HTML format message add this
SmtpClient SMTPC = new SmtpClient();
SMTPC.Send(MM);//thats all mail is sent
}

We create a new Mail Message object and pass the values we created previously on the dictionary to it. And also if we are sending a HTML format mail we set the IsBodyHtml property of the MailMessage object to true. Then we create a SmtpClient object which takes the settings we previously added to the config file and we pass the MailMessage object to the SmtpClinet.Sent method.

I hope this was easily understandable, if you have any questions please feel free to ask in the comments below.

Related posts:

  1. How to Schedule sending a mail in Gmail and outlook
  2. Receive info via MSN Mail via msg plus
  3. Google Mail: Google's approach to email
  4. Auto Login to Live Mail Using Firefox
  5. Pan, Zoom & Rotate Images With WPF using C#
Posted in C#, Coding | Tags: , ,

Comments are closed.