Santry Technology Solutions, Content Management, DotNetNuke, SharePoint Consulting
Register | Login
Saturday, July 04, 2009

Sections
  
About Us
  
Partners
Downloads
  
 WWWCoder.com Resource Directory

Email Sending in ASP.net 2.0
10/2/2006 5:37:08 PM

This article will focus on following concept Simplest way of sending email Writing HTML Email 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");
        }

Download File: SendEmail Prac code.zip
Size: 1.85 KB, Type: application/x-zip-compressed


Page Options:
format for printing  Format for Printer
email article  Email Page
add to your favorites   Add to Favorites
How would you rate the quality of this content?
Poor - - Excellent
Comments?
Overall Rating:
Comments Left:
Left on 6/18/2009 1:38:06 AM by Anonymous
Comments: It's Realy very good
Left on 5/27/2009 8:57:11 AM by Anonymous
Comments: aDFSFADFAadf
No ratings available.
Left on 3/9/2009 4:57:15 PM by Anonymous
Comments: jahjdhasjdha
No ratings available.
Left on 1/27/2009 2:43:02 AM by Anonymous
Comments: good
Left on 12/21/2008 6:56:55 PM by Anonymous
Comments: the textbox does not give an option to upload. can it be altered to use file upload. i thinks thats a better idea
No ratings available.
Left on 10/14/2008 1:51:35 AM by Anonymous
Comments: g
Left on 8/31/2008 7:13:59 AM by Anonymous
Comments: poor
Left on 6/17/2008 7:29:11 AM by Anonymous
Comments: thanks for information
Left on 4/25/2008 11:32:17 AM by Anonymous
Comments: thanks much
Left on 1/29/2008 3:20:36 AM by Anonymous
Comments: nice explaination
Left on 1/22/2008 10:16:03 AM by Anonymous
Comments: Hello! Good Site! Thanks you! iuvpacmfqs
Left on 1/22/2008 10:16:00 AM by Anonymous
Comments: Hello! Good Site! Thanks you! mkrtmcjpvgrv
Left on 1/14/2008 4:14:00 PM by Anonymous
Comments: very helpful

C# ASP.NET
http://www.ahmetkaymaz.com

Ahmet Kaymaz
Left on 12/1/2007 11:05:48 PM by Anonymous
Comments: Where did you put password for your email account?
Left on 10/25/2007 5:40:12 AM by Anonymous
Comments: VERY BAD
Left on 10/15/2007 5:34:40 AM by Anonymous
Comments: .
Left on 9/26/2007 4:35:00 AM by Anonymous
Comments: very poor
No ratings available.
Left on 9/26/2007 4:32:28 AM by Anonymous
Comments: you should also mention that from where we could get "mymailserver".
Left on 9/3/2007 10:32:11 PM by Anonymous
Comments: BuzzyCode is a bunch of copy'n'paste script kiddies!!  Want proof??  Just look at the articles on BuzzyCode and compare to the originals on The CodeProject site!

Left on 7/16/2007 7:19:22 AM by Anonymous
Comments: Very nice!
But I'm have not yet found a tutorial that shows how to have x number of fields to data such as post no, city, phone etc.

Almost all tutorial only deals with the usual fields like email from, subject, body etc.

But
Left on 6/13/2007 5:48:23 AM by Anonymous
Comments: I'm not able to use your zip :(
Left on 6/7/2007 4:09:13 AM by Anonymous
Comments: Free Online Php tutorials

http://www.buzzycode.com offers free online PHP tutorials. Best suited for beginners.

Several sample projects are available for download in this site. Learn Php step by step. Many senior Php professionals answer the technical questiosn by users. Visit http://www.buzzycode.com.


Left on 3/30/2007 8:27:15 AM by Anonymous
Comments: Very helpful. Thank you!
Left on 3/25/2007 8:51:38 PM by Anonymous
Comments: very boring

Left on 2/9/2007 5:07:14 AM by Anonymous
Comments:
Left on 11/8/2006 12:32:00 PM by Anonymous
Comments: Great works

Left on 10/28/2006 1:14:13 AM by Anonymous
Comments: Hello DNG your article has helped me., I got this one from dotnetspider.com
No ratings available.
  

 Latest Articles
  

 Latest News
  

 

Spotlight
Syndication

 


 


Digg This
 


DotNetNuke Platinum Benefactor

  
 

 Terms Of Use | Privacy Statement
 Copyright 2008 - Santry Technology Solutions, Box 172, Girard, PA 16417, (814) 774-0970