Click here to Skip to main content
15,881,027 members
Articles / Web Development / ASP.NET
Article

Sending Email from ASP.NET using Formatted Text Editor and Attachments

Rate me:
Please Sign up or sign in to vote.
4.36/5 (19 votes)
24 Aug 20054 min read 122.8K   4.4K   65   17
Complete email sending with formatting text editor with features like colored, bold, italicized, indented etc. You can even have links and pictures and attachments in the mail.

Image 1

Introduction

Well, till now you have seen sending email from an ASP.NET application, but not inluding options for formatted text and attachments. Here is what I bring forward to you: complete email sending with formatting text editor with features like colored, bold, italicized, indented etc. You can even have links and pictures and attachments in the mail.

The basic formatting tools and attachments are integrated in one application and it also demonstrates using an 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 the SMTP server

  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: "127.0.0.1", if it's not there, you need to add it.
  2. If you are using "localhost" or "127.0.0.1" as the SmtpMail.SmtpServer, make sure "Anonymous access is allowed". To allow access, open up IIS console. Locate the SMTP virtual server, and right-click and select Properties.
  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.

How the code works

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 MSDN.

Now before writing the actual code for sending mail, we have to write the code which formats the text or prepares the text editor. For the text editor, we need three files:

  1. MsgBody.htm (This file holds the text which is to be edited.)
  2. Textedit.js (This file contains the code which formats the text.)
  3. ColorPalette.htm (An HTML color palette is created which is used to color the text as shown below.)

Image 2

Steps to add the text editor

In TextEdit.js, the following code displays the color palette. I used an IFrame for this purpose and set its property to “on”. The IFrame element functions as a document within the document. We are typing the text and all formats in the inner text of the IFrame and retrieving the inner text of the IFrame.

VB
NewsBody_rich.document.designMode="On"

Add the following code in the SendEmail.aspx page:

HTML
<SCRIPT language="javascript" src="TextEdit.js"></SCRIPT>

This file executes all the contents by using the execCommand method in JavaScript.

The following line of code in SendEmail.aspx.vb page's button click event triggers the JavaScript to load the formatted text as the message body.

VB
Button1.Attributes.Add("onClick", "javascript:fillTxt();")

Actual .NET Code

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 Button's Click event, we create an instance of the 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 an HTML message. We need to specify the body format in the BodyFormat property. One we set all the properties, we are ready to send the email. Before sending the email, you have to set another important property, i.e., 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 “127.0.0.1”. 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.

VB
Button1.Attributes.Add("onClick", "javascript:fillTxt();")
Dim attach1 As String = ""
Dim strFileName As String = ""
Dim message As New MailMessage()
If (attachFile1.PostedFile.FileName <> "") Then
Dim ulFile As HttpPostedFile = attachFile1.PostedFile
     Dim nFileLen As Int64 = ulFile.ContentLength
     If (nFileLen > 0) Then
strFileName= Path.GetFileName(attachFile1.PostedFile.FileName)
             strFileName = "Uploads/" + strFileName
attachFile1.PostedFile.SaveAs(Server.MapPath(strFileName))
Dim attach As MailAttachment = New MailAttachment(Server.MapPath(strFileName))
                message.Attachments.Add(attach)
                attach1 = strFileName
      End If
End If
        message.From = TextBox2.Text
        message.To = TextBox3.Text
        message.Cc = txtcc.Text
        message.Bcc = txtbcc.Text
        message.Subject = TextBox4.Text
        message.Body = hdnmsg.Value
        message.BodyFormat = MailFormat.Html
  SmtpMail.SmtpServer = "127.0.0.1"            
    SmtpMail.Send(message)
lblMessage.Text = "Your email has been sent"

Now, we send the email with the attachments:

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:

VB
Dim attach As MailAttachment = New MailAttachment(Server.MapPath(strFileName))

Enhancements to add to the above examples

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.

Points to remember

  • Make sure that you have created a folder “Uploads” in the root and have full access to this folder.
  • Do not forget to change the form tag property to encType="multipart/form-data".
  • Enable your browser for JavaScript because the color palette is based on JavaScript.
  • Configure your Default SMTP Virtual Server as SmtpMail.SmtpServer = "127.0.0.1" or localhost.
  • Import the namespace System.Web.Mail.

License

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


Written By
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.

Comments and Discussions

 
GeneralMy vote of 5 Pin
bhalaniabhishek22-Jan-16 23:19
bhalaniabhishek22-Jan-16 23:19 
QuestionMail sent but without Body Pin
Rohit Kumar Mumbai25-Dec-12 19:26
Rohit Kumar Mumbai25-Dec-12 19:26 
QuestionIts workinf fine in IE but not in Firefox??? Pin
Dotnetkanna25-Sep-09 18:36
Dotnetkanna25-Sep-09 18:36 
QuestionNot getting the body text in email Pin
MD8320-May-09 16:37
MD8320-May-09 16:37 
QuestionRe: Not getting the body text in email Pin
Samarjeet Singh@india11-Sep-09 20:45
Samarjeet Singh@india11-Sep-09 20:45 
Generalplz help solving d prblm Pin
chandrika2211-Mar-09 5:33
chandrika2211-Mar-09 5:33 
GeneralI m not getting a mail using this code Pin
Member 261074012-May-08 22:15
Member 261074012-May-08 22:15 
GeneralRe: I m not getting a mail using this code Pin
Mohd Abdul Moyeed12-May-08 22:51
Mohd Abdul Moyeed12-May-08 22:51 
QuestionUpdate in C#? Pin
rghubert25-Oct-07 23:02
professionalrghubert25-Oct-07 23:02 
AnswerRe: Update in C#? Pin
Mohd Abdul Moyeed27-Oct-07 22:28
Mohd Abdul Moyeed27-Oct-07 22:28 
Generalparagraph break vs line break Pin
u2u213-Nov-06 12:16
u2u213-Nov-06 12:16 
Questionplease help me! object expected error Pin
avahia16-Oct-06 5:39
avahia16-Oct-06 5:39 
Generalgood codeing Pin
raj_varun7899-Oct-06 0:58
raj_varun7899-Oct-06 0:58 
GeneralError with NewsBody_rich Pin
jdevenney23-Sep-06 5:11
jdevenney23-Sep-06 5:11 
Generalpreserve text in frame after postback is not working! Pin
hellobaba19-May-06 10:48
hellobaba19-May-06 10:48 
GeneralExcellent demonstration SMTP client Pin
jhomminga25-Apr-06 17:22
jhomminga25-Apr-06 17:22 
Generalwhere do i find the clsmymail' Pin
netwiredsys12-Mar-06 18:34
netwiredsys12-Mar-06 18:34 

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.