Category Archives:






Sending e-mails with C# .NET 2.0


First step: Setting-up your SMTP (Simple Mail Transfer Protocol)


you must have installed IIS (internet information services), so you can setup your SMTP Virtual Server from there.


Control Panel->Administrative Tools->Internet Information Services , then you select the SMTP Virtual Server Properties and grant permission to local host ( (Access->Relay).


Second Step: Writing the basic C# Code


Example: Windows Application


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Net;

using System.Net.Mail;

using System.Windows.Forms;


namespace Threading


    public partial class Form1 : Form


        public Form1()




    protected void Button1_Click(object sender, EventArgs e)


        MailMessage objMM = new MailMessage();

        objMM.From = new MailAddress(textBox1.Text, “sender name”);

        objMM.To.Add(new MailAddress(textBox2.Text, “reciever name”));

        objMM.Subject = textBox3.Text;

        objMM.Body = richTextBox1.Text;

        SmtpClient objSC = new SmtpClient(“localhost”, 25);

        objSC.DeliveryMethod = SmtpDeliveryMethod.Network;

        objSC.Host = “”; // for example gmail smtp server

        objSC.EnableSsl = true;

        objSC.Credentials = new System.Net.NetworkCredential(“mailid”, “password”);





        catch (Exception ex)





        MessageBox.Show(“Mail send successfully”);






Be sure that the recipient checks his spam messages because many of the e-mails using this algorithm go there. Also don’t forget to include System.Net.Mail namespace (using System.Net.Mail ;).




In .NET 1.1 you can use SmtpMail class (.NET1.1 does not support SmtpClient class) and be sure to include System.WEB.Mail.

Smtp Classes in .Net

MailMessage Class:

     The MailMessage class can be considered the foundation class of the System.Net.Mail namespace. It deals with creating and managing the email message. All other classes will somehow interact with this class. The MailMessage class exposes such properties as the




 Gets the attachment collection used to store data attached to this e-mail message.


 Gets the address collection that contains the blind carbon copy (BCC) recipients for this e-mail message.


 Gets or sets the message body.


 Gets the address collection that contains the carbon copy (CC) recipients for this e-mail message.


 Gets or sets the from address for this e-mail message.


 Gets or sets the subject line for this e-mail message.


 Gets the address collection that contains the recipients of this e-mail message.

Below you will find an example of using the MailMessage class


//create the mail message
MailMessage mail = new MailMessage ();
//set the addresses
mail.From = new MailAddress ("");
mail.To.Add ("");
//set the content
mail.Subject = "This is an email";
mail.Body = "this is the body content of the email”;

MailAddress Class:

            The MailAddress class is used for creating email addresses. This class is used for setting the MailMessage.From, MailMessage.To, MailMessage.CC and MailMessage.BCC properties. Of these properties the .From class is actually a MailAddress, while the To, CC and BCC properties are actually collections of MailAddresses. The two most common properties of the MailAddress class are the DisplayName and the Address properties. They are described below.




Gets the e-mail address specified when this instance was created.


Gets the display name composed from the display name and address information specified when this instance was created.


//set the addresses

//to specify a friendly 'from' name, we use a different ctor
mail.From = new MailAddress ("", "Steve James");

Attachment Class:

            The Attachment class is used for creating and managing individual attachments of the MailMessage object. Attachments can be created from streams or file paths. The stream or file path must be set in the ctor of the Attachment.

Below is an example demonstrating the Attachment class


 Static void AttachmentFromFile ()


//create the mail message

MailMessage mail = new MailMessage ();

//set the addresses

mail.From = new MailAddress (“”);

mail.To.Add (“”);

//set the content

mail.Subject = “This is an email”;

mail.Body = “this content is in the body”;


//add an attachment from the file system

mail.Attachments.Add (new Attachment (“c:\\temp\\example.txt”));

//to add additional attachments, simply call .Add (…) again

mail.Attachments.Add (new Attachment (“c:\\temp\\example2.txt”));

mail.Attachments.Add (new Attachment (“c:\\temp\\example3.txt”));

//send the message

SmtpClient smtp = new SmtpClient(“”);




SmtpClient Class:

The SmtpClient class is responsible for sending or transporting the email. The SmtpClient can transport the email content over the network, or it can actually write them to the filesystem in the MS IIS Smtp Service Pickup Directory format, which resembles a RFC821 formatted message. Emails can be sent either synchronously or asynchronously. The SmtpClient also supports sending email via SSL for security purposes. The following list of properties are the most common used on the SmtpClient class.




Gets or sets the credentials used to authenticate the sender.


Specifies how outgoing email messages will be handled.


Specify whether the SmtpClient uses Secure Sockets Layer (SSL) to encrypt the connection.


Gets or sets the name or IP address of the host used for SMTP transactions.


Gets or sets the port used for SMTP transactions.


AlternateView class:

The AlternateView class is used for providing alternate bodies and creating Multi-Part mime emails. If you want to create an email that will be rendered properly in both Html capable and Plain Text only mail clients, then you will create alternate views of the message. There are a few main properties and methods you will use with the AlternateView class. They are:




Gets or sets the Base URI to use for resolving relative URIs in the AlternateView


Gets the set of embedded resources referred to by this attachment.

CreateAlternateViewFromString (static method)

Overloaded. Creates an AlternateView to view an email message using the specified format..


LinkedResource class:

The LinkedResource class is the last, and least used main class. It is mainly used for creating embedded images. To create an embedded image you will need to first create a Html formatted AlternateView. Within that alternate view you create an tag, that points to the ContentId (CID) of the LinkedResource. You then create a LinkedResource object and add it to the AlternateView’s LinkedResources collection.

SMTP Basics

Simple Mail Transfer Protocol:

Short for Simple Mail Transfer Protocol, a protocol for sending e-mail messages between servers. Most e-mail systems that send mail over the Internet use SMTP to send messages from one server to another; the messages can then be retrieved with an e-mail client using either POP or IMAP. In addition, SMTP is generally used to send messages from a mail client to a mail server.

Reasons why you might want to send email from your Web application:

*    create a “give us your feedback” Web page

*    implement a “forgotten password” script that sends a password to the user’s email account

*    send an automatic welcome email to your new newsletter subscriber

*    send automatic email update notifications

*    send automatic email notifications whenever an error occurs in your Web application

SMTP Server:

A relay is a service that allows you to send email. It is usually a full fledged mail server, or can be a specialized SMTP Service. Some examples of a mail server include Microsoft Exchange, IMail by IPSwitch, or Mail Enable by Mail Enable. An example of a SMTP service is the SMTP Service installed that can be installed with IIS. SNM sends email to a relay server, and the relay server is responsible for delivering the email to the final destination. When sending email to a relay server, you must have protocol permissions to use that server. Because of SPAM problems, relay servers are normally locked down, either by IPAddress or by some type of username/password authentication. Relaying errors are the most common problems when programmatically sending emails.


System. Net. Mail Namespace:

System.Net.Mail is the namespace used to send email if you are using the 2.0 (or higher) .NET Framework.

Unlike System.Web.Mail, which was introduced in the 1.0 Framework, it is not built upon the CDO/CDOSYS libraries. Although some functionality has been removed, the new System.Net.Mail namespace is much more versatile than the older CDO dependant System.Web.Mail.

In this Mail Namespace, the following classes are available

è    MailMessage

è    MailAddress

è    Attachment

è    SmtpClient

è    AlternateView

è    Linked Resource


My IQ Score

Free IQ Test by fun quizzes!
» » Christmas Trivia QuizNaughty or Nice QuizThe Dumb Test « «

Make a Quiz | Christmas Song Lyrics | Twilight Quizzes | Dumb MySpace Quizzes