Exforsys.com
 

Sponsored Links

 

Microsoft.NET Tutorials

 
Home Tech Articles Microsoft.NET
 

Email Sending in ASP.net 2.0

 

Email Sending in ASP.net 2.0

In this ASP.net 2.0 tutorial you will learn how to send email in ASP.net 2.0 - This article will focus on following concept, Simplest way of sending email, Writing HTML Email and Creating Email with attachment.


Simplest Way of Sending Email

    try
    {
        SmtpClient smtpMailObj = new SmtpClient();
        //eg:localhost, 192.168.0.x, replace with your server name
        smtpMailObj.Host = "myMailServer";
        smtpMailObj.Send(txtFrom.Text, txtTo.Text, txtSubject.Text, txtComment.Text);
        Response.Write("Your Message has been sent successfully");
    }
    catch (Exception ex)
    {
        Response.Write("Message Delivery Fails");
    }


Sending Email with MailMessage Object


        MailMessage Mail = new MailMessage();

    MailAddress ma = new MailAddress(txtFrom.Text,txtName.Text);
    Mail.From = ma;
    Mail.To.Add(txtTo.Text);

    Mail.Subject = txtSubject.Text;

    Mail.Body = txtComment.Text;

    try
    {
        SmtpClient smtpMailObj = new SmtpClient();
        //eg:localhost, 192.168.0.x, replace with your server name
        smtpMailObj.Host = "myMailServer";
        smtpMailObj.Send(Mail);
        Response.Write("Your Message has been sent successfully");
    }
    catch (Exception ex)
    {
        Response.Write("Message Delivery Fails");
    }


Sending Email with CC and BCC options


    MailMessage Mail = new MailMessage();

    MailAddress ma = new MailAddress(txtFrom.Text,txtName.Text);
    Mail.From = ma;
    Mail.To.Add(txtTo.Text);


    if(txtCC.Text.Trim().Length != 0)
        Mail.CC.Add(txtCC.Text);

    if(txtBCC.Text.Trim().Length != 0)
        Mail.Bcc.Add(txtBCC.Text);


    Mail.Subject = txtSubject.Text;

    Mail.Body = txtComment.Text;

    try
    {
        SmtpClient smtpMailObj = new SmtpClient();
        //eg:localhost, 192.168.0.x, replace with your server name
        smtpMailObj.Host = "myMailServer";
        smtpMailObj.Send(Mail);
        Response.Write("Your Message has been sent successfully");
   }
    catch (Exception ex)
   {
        Response.Write("Message Delivery Fails");
    }


Sending Email with Reply-To options


    MailMessage Mail = new MailMessage();
   
    MailAddress ma = new MailAddress(txtFrom.Text,txtName.Text);
    Mail.From = ma;
    Mail.To.Add(txtTo.Text);

    if(txtCC.Text.Trim().Length != 0)
    Mail.CC.Add(txtCC.Text);

    if(txtBCC.Text.Trim().Length != 0)
    Mail.Bcc.Add(txtBCC.Text);

    Mail.Subject = txtSubject.Text;

    Mail.Body = txtComment.Text;

    //Setting Reply address.
    if (txtReplyTo.Text.Trim().Length != 0)
    {
        Mail.Headers.Add("Reply-To", txtReplyTo.Text);
    }
    else
    {
        Mail.Headers.Add("Reply-To", txtFrom.Text);
    }


    try
    {
        SmtpClient smtpMailObj = new SmtpClient();
        //eg:localhost, 192.168.0.x, replace with your server name
        smtpMailObj.Host = "myMailServer";
        smtpMailObj.Send(Mail);
        Response.Write("Your Message has been sent successfully");
    }
    catch (Exception ex)
    {
        Response.Write("Message Delivery Fails");
    }


Sending Email with HTML output options


    MailMessage Mail = new MailMessage();

    MailAddress ma = new MailAddress(txtFrom.Text,txtName.Text);
    Mail.From = ma;
    Mail.To.Add(txtTo.Text);
   
    if(txtCC.Text.Trim().Length != 0)
    Mail.CC.Add(txtCC.Text);

    if(txtBCC.Text.Trim().Length != 0)
    Mail.Bcc.Add(txtBCC.Text);

    Mail.Subject = txtSubject.Text;
  
    //Note: When you make "IsBodyHtml" to true make
    //ValidateRequest="false" in page derective
    //As to make HTML content passed.
    Mail.IsBodyHtml = true;
    Mail.Body = txtComment.Text;


    //Setting Reply address.
    if (txtReplyTo.Text.Trim().Length != 0)
    {
        Mail.Headers.Add("Reply-To", txtReplyTo.Text);
    }
    else
    {
        Mail.Headers.Add("Reply-To", txtFrom.Text);
    }

    try
    {
        SmtpClient smtpMailObj = new SmtpClient();
        //eg:localhost, 192.168.0.x, replace with your server name
        smtpMailObj.Host = "myMailServer";
        smtpMailObj.Send(Mail);
        Response.Write("Your Message has been sent successfully");
    }
    catch (Exception ex)
    {
        Response.Write("Message Delivery Fails");
    }


Sending Email with Attachment facility


    MailMessage Mail = new MailMessage();

    MailAddress ma = new MailAddress(txtFrom.Text,txtName.Text);
    Mail.From = ma;
    Mail.To.Add(txtTo.Text);

    if(txtCC.Text.Trim().Length != 0)
        Mail.CC.Add(txtCC.Text);

    if(txtBCC.Text.Trim().Length != 0)
        Mail.Bcc.Add(txtBCC.Text);

    Mail.Subject = txtSubject.Text;

    if (txtAttachment.Text.Trim().Length != 0)
    {
        string sAttach = txtAttachment.Text.Replace("\\","
\\\\");
        Mail.Attachments.Add(new Attachment(sAttach));
    }


    //Note: When you make "IsBodyHtml" to true make
    //ValidateRequest="false" in page derective
    //As to make HTML content passed.
    Mail.IsBodyHtml = true;
    Mail.Body = txtComment.Text;

    //Setting Reply address.
    if (txtReplyTo.Text.Trim().Length != 0)
    {
        Mail.Headers.Add("Reply-To", txtReplyTo.Text);
    }
    else
    {
        Mail.Headers.Add("Reply-To", txtFrom.Text);
    }

    try
    {
        SmtpClient smtpMailObj = new SmtpClient();
        //eg:localhost, 192.168.0.x, replace with your server name
        smtpMailObj.Host = "myMailServer";
        smtpMailObj.Send(Mail);
        Response.Write("Your Message has been sent successfully");
    }
    catch (Exception ex)
    {
        Response.Write("Message Delivery Fails");
    }


Screenshot

 



Sending Email in Html Format




Viewing output in Outlook


DotNetGuts (DNG)
"Lets Make Programming Easy"

Blog: DotNetGuts.blogspot.com
WebSite: www.DotNetGuts.2ya.com
Group: DotNetGuts@YahooGroups.com



Read Next: Smartnavigation property in ASP.NET



 

 

Comments



Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape