Click here to Skip to main content
13,505,095 members
Click here to Skip to main content
Add your own
alternative version


40 bookmarked
Posted 20 Aug 2005

Sending Email from Asp.Net Using Formatted Text Editor and Attachments in single application.

, 20 Aug 2005
Rate this:
Please Sign up or sign in to vote.
Email Application with text editor for formatting text and Attachments

Sample screenshot


Well till now you have seen sending email from Asp.Net But not formatted text and attachments in one application. Here is what I bring forward to you complete email sending with formatting text editor like Colored, Bold, Italicized, Indented you can even have the Links and pictures and attachments in single application.

The basic formatting tools and attachments are integrated in one application and also demonstrates using SMTP server and how to configure the default SMTP server. The System.Web.Mail namespace provides classes for sending email in .NET. The classes involved are MailMessage which manages the content of the mail message and MailAttachment which manages the mail attachments. And formatting of the text is done by TextEdit.js and MsgBody.htm files which are included in the SendEmail.aspx page.

Configuring SMTP server<?xml:namespace prefix = o /><o:p>

  1. To configure "Default SMTP Virtual Server", right-click on it, go into "Properties", and select "Access" tab, and then click the "Relay" button. With "only the list below" radio button selected, you should see the local IP address: "", if it's not there, you need to add it. <o:p>
  2. If you are using "localhost" or "" as the SmtpMail.SmtpServer, make sure "Anonymous access is allowed". To allow access, open up the IIS. Locate the SMTP virtual server, and right-click select Properties. <o:p>
  3. You need to replace "localhost" with the name or IP address of your SMTP mail server. On a Windows desktop computer, "localhost" is the default value and usually works.<o:p>

How the code Works<o:p>

MailMessage has all required properties such as To, Subject, BCC, CC etc. For a complete list of method and properties, that you can make use of, please visit<o:p>


Now before writing the actual code for sending mail we have to write the code which formats the text or prepare the text editor. For text editor we need 3 files<o:p>

1)     MsgBody.htm  (This file holds the text which is to be edited.)<o:p>

2)     Textedit.js (This file contains the code which formats the text)<o:p>

3)     ColorPalette.htm (An html color palette is created which is used to color the text as shown below)<o:p>

<?xml:namespace prefix = v /><v:shapetype id=_x0000_t75 stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><v:stroke joinstyle="miter"><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"><v:f eqn="sum @0 1 0"><v:f eqn="sum 0 0 @1"><v:f eqn="prod @2 1 2"><v:f eqn="prod @3 21600 pixelWidth"><v:f eqn="prod @3 21600 pixelHeight"><v:f eqn="sum @0 0 1"><v:f eqn="prod @6 1 2"><v:f eqn="prod @7 21600 pixelWidth"><v:f eqn="sum @8 21600 0"><v:f eqn="prod @7 21600 pixelHeight"><v:f eqn="sum @10 21600 0"><v:path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"><o:lock aspectratio="t" v:ext="edit"><v:shapetype id=_x0000_t75 stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600">Sample screenshot <v:stroke joinstyle="miter"><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"><v:f eqn="sum @0 1 0"><v:f eqn="sum 0 0 @1"><v:f eqn="prod @2 1 2"><v:f eqn="prod @3 21600 pixelWidth"><v:f eqn="prod @3 21600 pixelHeight"><v:f eqn="sum @0 0 1"><v:f eqn="prod @6 1 2"><v:f eqn="prod @7 21600 pixelWidth"><v:f eqn="sum @8 21600 0"><v:f eqn="prod @7 21600 pixelHeight"><v:f eqn="sum @10 21600 0"><v:path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"><o:lock aspectratio="t" v:ext="edit"><o:p>


Steps to add the Text Editor:-<o:p>

In TextEdit.js the following Code displays the ColorPalette<o:p>

Here I used a IFrame for this purpose and sets its property to “on”. The IFrane element functions as a document within document. we are typing the text and all formats in the inner text of the <SPAN style="FONT-SIZE: 10pt">iframe and retrieving the innerTextof the IFrame.<o:p>



Add the following code in SendEmail.aspx page <o:p>

<SCRIPT language="javascript" src="TextEdit.js"></SCRIPT><o:p>

This file executes all the contents by using the <SPAN style="FONT-SIZE: 10pt">execCommand method in JavaScript.<o:p>


This below line of code in SendEmail.aspx.vb page Button Click event triggers the JavaScript to load the formatted text as the message Body.<o:p>


Button1.Attributes.Add("onClick", "javascript:fillTxt();")<o:p>


Actual DotNet Code<o:p>

We did the background work of formatting text Now we start the actual coding of sending mail by importing the namespace, "System.Web.Mail". Then, in the ButtonClick event, we create an instance of MailMessage object. It is through the MailMessage object, we set all the properties such as To, From, Subject, Body etc. We can either send a text message or a html message. We need to specify the bodyformat in the BodyFormat property. One we set all the properties, it is ready to send the email. Before sending the email, you have to set another important property, ie; SmtpServer. You have to set this property. You should assign the name of your SMTP server to this property. In most cases you can assign this as "localhost" or “”. If you do not set this property, then you will not be able to send email from an ASP .NET page. Finally we send the email using SmtpMail.Send.<o:p>

Button1.Attributes.Add("onClick", "javascript:fillTxt();")<o:p>

Dim attach1 As String = ""<o:p>

Dim strFileName As String = ""<o:p>

Dim message As New MailMessage()<o:p>

If (attachFile1.PostedFile.FileName <> "") Then<o:p>

Dim ulFile As HttpPostedFile = attachFile1.PostedFile<o:p>

     Dim nFileLen As Int64 = ulFile.ContentLength<o:p>

     If (nFileLen > 0) Then<o:p>

strFileName= Path.GetFileName(attachFile1.PostedFile.FileName)<o:p>

             strFileName = "Uploads/" + strFileName<o:p>


Dim attach As MailAttachment = New MailAttachment(Server.MapPath(strFileName))<o:p>


                attach1 = strFileName<o:p>

      End If<o:p>

End If<o:p>

        message.From = TextBox2.Text<o:p>

        message.To = TextBox3.Text<o:p>

        message.Cc = txtcc.Text<o:p>

        message.Bcc = txtbcc.Text<o:p>

        message.Subject = TextBox4.Text<o:p>

        message.Body = hdnmsg.Value<o:p>

        message.BodyFormat = MailFormat.Html<o:p>

  SmtpMail.SmtpServer = ""            <o:p>


lblMessage.Text = "Your email has been sent"<o:p>


Now, we send the email with the attachments:<o:p>


To send attachments, we need to add attachments using the Add method, which is available in the Attachments object. The only thing that we need to add to the above example is <o:p>

Dim attach As MailAttachment = New MailAttachment(Server.MapPath(strFileName))<o:p>

Enhancements that you add to the above examples.<o:p>

We can add any number of attachments to an email. To send multiple attachments, just repeat the line Msg.Attachments.Add with the file that needs to be attached.<o:p>


Points to remember:-<o:p>

  • Make sure that you have created a Folder “ Uploads” in the root and have full access to this folder.<o:p>
  • Do not foget to change the Form tag property to encType="multipart/form-data"<o:p>
  • Enable your browser for JavaScript because the colorpalette is based on JavaScript.<o:p>
  • Configure your Default SMTP Virtual Server as SmtpMail.SmtpServer = "" or Localhost.
  • Import namespace system.web.mail


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Mohd Abdul Moyeed
Web Developer
Saudi Arabia Saudi Arabia
Myself Moyeed Worked as a Programmer in India, Mauritius. Now working as a Web-Developer in Kingdom of Saudi Arabia.

You may also be interested in...


Comments and Discussions

QuestionQuestion Pin
Devendra Sarang23-Jun-14 1:41
memberDevendra Sarang23-Jun-14 1:41 
QuestionError When build the project Pin
Devendra Sarang12-Jun-14 20:49
memberDevendra Sarang12-Jun-14 20:49 
QuestionError occured when send mail from web Pin
Niranjan dotnet7-Oct-12 19:20
memberNiranjan dotnet7-Oct-12 19:20 
Questionhow to send mail from different domains Pin
hashiny.bca5-Jul-11 1:39
memberhashiny.bca5-Jul-11 1:39 
GeneralIts not working .... Pin
s.sathish31110-Jun-11 19:43
members.sathish31110-Jun-11 19:43 
GeneralRe: Its not working .... Pin
Mohd Abdul Moyeed11-Jun-11 2:20
memberMohd Abdul Moyeed11-Jun-11 2:20 
QuestionHi Pin
RavindraSahasrabudhe7-May-08 1:45
memberRavindraSahasrabudhe7-May-08 1:45 
GeneralHello Pin
khushal5-May-08 0:08
memberkhushal5-May-08 0:08 
GeneralNot working Pin
suresh0081-May-08 23:45
membersuresh0081-May-08 23:45 
Questionhow to run this source code? Pin
Gayathri_Sridaran18-Apr-08 0:59
memberGayathri_Sridaran18-Apr-08 0:59 
Generalrun in background Pin
geervanibs3-Apr-08 1:00
membergeervanibs3-Apr-08 1:00 
GeneralGrid To The RichTextBox Pin
chivo204030-May-07 12:56
memberchivo204030-May-07 12:56 
Generalemail with attachments Pin
sra2210-Apr-07 0:53
membersra2210-Apr-07 0:53 
GeneralRe: email with attachments Pin
Mohd Abdul Moyeed10-Apr-07 19:21
memberMohd Abdul Moyeed10-Apr-07 19:21 
Generalsending html document in a mail with Pin
samobeito@hotmail.com22-Feb-07 23:14
membersamobeito@hotmail.com22-Feb-07 23:14 
QuestionBrowser Compatibility? Pin
Mohsin Meghani29-Aug-06 20:06
memberMohsin Meghani29-Aug-06 20:06 
Generalpre populate data in msg text box Pin
nanreddy13-Jan-06 5:41
membernanreddy13-Jan-06 5:41 
GeneralThank You Pin
Chachiisme2-Dec-05 7:11
memberChachiisme2-Dec-05 7:11 
GeneralScript code Pin
Faheem Habib6-Sep-05 23:47
memberFaheem Habib6-Sep-05 23:47 
GeneralMissing images and code Pin
sides_dale20-Aug-05 17:03
membersides_dale20-Aug-05 17:03 
GeneralPlease fix your zip file and screenshot references Pin
fwsouthern20-Aug-05 12:28
memberfwsouthern20-Aug-05 12:28 
GeneralAdvance attachment Pin
Mehrdad Ebrahimi20-Aug-05 10:17
memberMehrdad Ebrahimi20-Aug-05 10:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180417.1 | Last Updated 20 Aug 2005
Article Copyright 2005 by Mohd Abdul Moyeed
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid