Version Française
Search Stories : Publier Votre Article
> How to send templated emails in Asp.Net 2.0 <
Published on 4/11/2007 By Malek Chtiwi
This is a quick guide on how to implement a SendMail class that send a templated email in Asp.Net 2.0, we will use C# for this article.
1/ First we need to define our smtp server adress in the web.config file , just add the smtphost key 'smtphost' with the adress (ou IP) of your smtp server to the appSettings session in your web.config file :

     <appSettings>

        <add key="smtphost" value="smtp.yoursite.com"/>

     </appSettings>

2/ Then we create a SendMail function that create the mail object, configure, send the mail and return a boolean value that confirm (or not) the success of the process :

public static bool SendMail(string To_Mail, string Mail_Title, string Message_Body, string Mail_Model, bool Is_Html)

    {

        // smail represents our email message

 // it has properties like From, To, Subject, Body, and so on.

        System.Net.Mail.MailMessage smail = new System.Net.Mail.MailMessage();

   

        // specifying if our email must be sent in html or text format

        smail.IsBodyHtml = Is_Html;

   

        // email Body Encoding

        smail.BodyEncoding = System.Text.Encoding.GetEncoding("iso-8859-1");

       

        // Now specify your email adress and your/company name

        // you can even set a erroneous adress if you want to send spams for example.

        smail.From = new System.Net.Mail.MailAddress("mail@xcess.info", "Sing It.");

       

        // email adress of the receiver, we can add many receivers.

        smail.To.Add(new System.Net.Mail.MailAddress(To_Mail));

   

        // mail title/subject

        smail.Subject = Mail_Title;

   

        // we replace the newline with it's html equivalent in the message

        Message_Body = Message_Body.Replace("\r\n", "<br />");

   

        // then we place our message in the mail template by replacing

        // the 'placemailhere' string by our message if it exists.

        if (Mail_Model.ToLower().Contains("placemailhere"))

        {

            smail.Body = Mail_Model.Replace("placemailhere", Message_Body);

        }

        else

        {

            smail.Body = Message_Body;

        }

   

        // MailMessage instance to a specified SMTP server

        System.Net.Mail.SmtpClient Client = new System.Net.Mail.SmtpClient();

   

        // Setting up the Smtp server

        Client.Host = System.Web.Configuration.WebConfigurationManager.AppSettings["smtphost"];

   

        // Sending the mail

        try

        {

            Client.Send(smail);

        }

        catch

        {

            return false;

        }

        return true;

    }
3/ and Finally we call the SendMail function :

        // With template

        xcess.SendMail("toemail@xxx.com", "Mail Subject", "This is the body", "xxx placemailhere xxx", true);

 

        // Without template

        xcess.SendMail("toemail@xxx.com", "Mail Subject", "This is the body", "", true);

Download the source code for this article.
Happy coding!
By Malek Chtiwi.
 Commentaires : Publier Votre Article
Request for Quick Reply!!
By bharat, Date: 12/14/2007
I am facing a problem to write a code :
when I write the Namespace :System.Net.Mail.MailMessage;
or
System.Web.Mail.MailMessage;
It show an Error:The missing a reference file.

so please give me a solution.

with regards,
Bharat
Request for Quick Reply!!
By kirti, Date: 1/25/2008
Imports System.Data
Imports System.Net
Imports System.Net.Mail.SmtpClient
Imports System.Net.Mail
Dim smtpMailObj As New Net.Mail.SmtpClient
smtpMailObj.Host = "197.0.0.1"
smtpMailObj.Send(txtTo.Text, txtFrom.Text, "Test Mail", txtMessage.Text) Response.Write("Your Message has been sent successfully")
it give the Failure sending mail.resulte why how can solved this problem

i have mail sending problem
By kirti, Date: 1/25/2008
Imports System.Data
Imports System.Net
Imports System.Net.Mail.SmtpClient
Imports System.Net.Mail
Dim smtpMailObj As New Net.Mail.SmtpClient
smtpMailObj.Host = "197.0.0.1"
smtpMailObj.Send(txtTo.Text, txtFrom.Text, "Test Mail", txtMessage.Text) Response.Write("Your Message has been sent successfully")
it give the Failure sending mail.resulte why how can solved this problem

i have mail sending problem
By kirti, Date: 1/25/2008
Imports System.Data
Imports System.Net
Imports System.Net.Mail.SmtpClient
Imports System.Net.Mail
Dim smtpMailObj As New Net.Mail.SmtpClient
smtpMailObj.Host = "197.0.0.1"
smtpMailObj.Send(txtTo.Text, txtFrom.Text, "Test Mail", txtMessage.Text) Response.Write("Your Message has been sent successfully")
it give the Failure sending mail.resulte why how can solved this problem

i have mail sending problem
By kirti, Date: 1/25/2008
Imports System.Data
Imports System.Net
Imports System.Net.Mail.SmtpClient
Imports System.Net.Mail
Dim smtpMailObj As New Net.Mail.SmtpClient
smtpMailObj.Host = "197.0.0.1"
smtpMailObj.Send(txtTo.Text, txtFrom.Text, "Test Mail", txtMessage.Text) Response.Write("Your Message has been sent successfully")
it give the Failure sending mail.resulte why how can solved this problem

Simple SMS
By Daniel, Date: 2/13/2008
SMS
thnx
By jK, Date: 8/20/2009
It's nice, Thank you for your code.
 Ajouter un commentaire :
Nom (ou pseudo) :
Email :
   * Adresse non affichée avec votre message.
Titre :
Commentaire :
Other articles
LimousineWorld : Launching the Mercedes S550 Stretch Limo
 By Josee Hanlon, Date : 12/8/2007
Sfax, Tunisia
 By Xcess, Date : 1/23/2007
VS 2008 ERROR Creating control in content pages (Unknown server tag)
 By Malek Chtioui, Date : 6/1/2008
Mahdia, Tunisia
 By Xcess, Date : 1/23/2007
3 day Travel Certificates to Increase Business Traffic
 By Travel Certs, Date : 10/22/2008
Top 5
Unlock your Nokia phone online for FREE.
 By Xcess, Date : 4/1/2007
A Program That Can Fix Dead Pixels On Your LCD Screen.
 By Malek Chtioui, Date : 6/14/2007
Pictures of Dubai, a New City In Full Expansion
 By Malek Chtioui, Date : 6/12/2007
Request.Url parameters & details (Asp.net)
 By Malek Chtioui (Xcess), Date : 4/7/2008
How to : Resetting/Formatting your Nokia Phone (series 60 Nokia Mobile)
 By James, Date : 2/22/2008
Xcess Company, Adress : Xcess Company S.A.R.L., Boulevard Aïcha Bent Abi Backer, Khézama 3, Sousse, Tunisia.
Fax : (+216) 73 276 669 , Email/MSN : mail@xcess.info