Tuesday 18 February 2014

code for send mail with HTML Body

--> if you want to send mail with HTML body and CSS class in asp.net using c# code then  do following

--> first import two namespace in code behind file(C # code file)

------------------------------------------------------------------------------------------------


using System.Net;

using System.Net.Mail;

--> next write following function in your code behind file
------------------------------------------------------------------------------------------------

 void SendMail()
    {
       String From, Recipients, smtpHost, port,amount,name;
        smtpHost = "0.0.0.0";// Your IP Address
        port = "25";
        From = "abc@xyz.com";   //From: address which account to you send mail
         Recipients = txtemail.Text;  // To: address where you send mail
        Subject = "send mail"; //  Your subject which is appear in recipients inbox
        string body = "<html><head>"+  //Your mail body with html tag and css class
 "<style> .Headerleft{width:50px; height:100px; float:left; }"+
 ".HeaderRight { width:250px; height:100px; float:right;}"+
".mainBody{width:700px;height:auto;float:left;margin-top: 20px;}"+
".Footer{width:165px;height:175px;float:right;  margin-top: 200px;aligne:Center;}"+
".temp{width:700px; height:450px;border:2px solid;border-radius:5px;padding: 10px 10px 50px;}"+
"hr{width:700;margin-top: 100px;}"+
"</style></head>"+
"<Title>asp.net with c#</title>"+
"<body ><div class='temp'><div><div class='Headerleft'>"+  
"<img src='image path'></img></div>"+
"<div class='HeaderRight'><b>Address:</b>"+
"<t>001,Wing 'C', Shreeji123 plaza,<br>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp"+
" Anand Mahal Road, Adajan,<br> &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp Surat - 395009"+
"<br><b>Tel No</b>:0261-xxxxxx,4xxxxxx0</div></div>"+
"<hr><div class='mainBody'> &nbsp &nbsp &nbsp <br>Dear , <b>" + name + "</b>"+
"<br><br> &nbsp &nbsp &nbsp   ASp.net with C#...... ASp.net with C#......... ASp.net with C#."+
"....... ASp.net with C# ............<b>"+amount+",</b> -  ASp.net with C#...... ASp.net with C#........."+
"ASp.net with C#........ ASp.net with C# ............  ASp.net with C#...... ASp.net with C#........."+
" ASp.net with C#........ ASp.net with C# ............</div>" +
"<div class='Footer'><b>Thanks & Regards</b><br><br>Asp.net with C#.</div></div>"+
                        "</body></html>";

        MailMessage mailObj = new MailMessage(From, Recipients);//it is for From and To parameter.
        mailObj.Subject = subject;  //Your subject which is appear in recipients inbox
        mailObj.IsBodyHtml = true;  //this property true for html enable in mail
        mailObj.Body = body;  //  asigne  Your mail body for your message
        SmtpClient smtpClient = new SmtpClient(smtpHost, Convert.ToInt32(port));
        smtpClient.Credentials = new System.Net.NetworkCredential("abc@xyz.com", "abcxyz");
    // above line Your Mail Id and password
        smtpClient.Send(mailObj);
        }

--> this function call where you want to send mail

No comments:

Post a Comment



Asp.net tutorials