If you are working in an asp.net web application, you often need to send system generated mails from your asp.net application when the user performs certain actions. But you need not to worry, .Net provides a set of classes and functions which you can use to send emails from your application.  

The System.Net.Mail namespace in the .NET Framework 2.0 contains all the required classes and enumerations for email functionalities. Following are some important classes in the System.Net.Mail namespace which are used to send emails in asp.net applications:

 Class  Description
 MailMessage Represents an e-mail message that can be sent using the SmtpClient class and exposes properties such as From, To, CC, BCC, Attachments, Subject, and Body to formulate the message's contents
 MailAddress Represents the address of the sender or recipient (To, CC, and BCC)
 SmtpClient Allows applications to send emails using the SMTP
 Attachment Represents an attachment that is sent with an Email and is used in conjunction with the MailMessage class
 SmtpException Represents the exception that is thrown when the SmtpClient is not able to send the messag

 

Following code snippet can be used to send email:

(Don't forget to include the namespaces System.Net and System.Net.Mail in your program)

//Create an object of MailMessage class

    MailMessage message = new MailMessage();       

//Assign the MessageFromEmailID i.e. sender's EmailID

    message.From = new MailAddress("sender@domain.com");    

//Add the MessageToEmailID i.e. recipient's EmailID

    message.To.Add(new MailAddress("recipient@domain.com"));     

//Assign the subject of the email

    message.Subject = "Email Subject";       

    message.IsBodyHtml = true;   

//Assign the body of the email i.e. the message to be sent

    message.Body = "Message to be sent";                    

//Create an object of SmtpClient by passing ur SMTP Server name and SMTP port to be used. By default port 25 can be used.

    SmtpClient objSMTP = new SmtpClient("SMTPHost", "SMTPPort");          

    objSMTP.UseDefaultCredentials = true;      

//Call the Send  method of SmtpClient object and pass the MailMessage object, while will finally send the email message to the recipient's emailID

    objSMTP.Send(message);

 

Send an Email to multiple Recipients:

If you want to send an email to multiple recipients, then you need to modify the above code for adding reciepient's emailID by this one:

//Add the MessageToEmailID i.e. recipient's EmailID

    foreach (string recipientEmailID in arrAllRecipients)       

    {           

        message.To.Add(new MailAddress(recipientEmailID));       

    }

In the above code arrAllRecipients is an object of ArrayList in which all recipients emailIDs are added.

 

Send an Email to all Recipients added in the CC list:

If you also want to send an email to some recipients added in the CC list, then you need to modify the above code and add the following code snippet for adding CC reciepient's emailID:

//Add the MessageCCEmailID i.e. CC recipient's EmailID

    foreach (string ccRecipientEmailID in arrAllCCRecipients)       

    {           

        message.CC.Add(new MailAddress(ccRecipientEmailID ));       

    }

In the above code arrAllCCRecipients is an object of ArrayList in which all CC recipients emailIDs are added.


Sending attachments with an Email:

If you also want to send attachments with an email, then you need to modify the above code and add the following code snippet:

//Create an object of Attachment class for each fileyou want to send as attachment and add it to the MailMessage object

    foreach (string filePath in arrFilepaths)       

    {           

        Attachment attachFile = new Attachment(filePath);           

        message.Attachments.Add(attachFile);       

    } 

In the above code arrFilepaths is an object of ArrayList in which path of all the files to be attched in the email are added.