Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
20 Aug 2005 1  
Email Application with text editor for formatting text and Attachments

Sample screenshot

Introduction

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

  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 the IIS. Locate the SMTP virtual server, and right-click 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 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebMailMailMessageMembersTopic.asp

 

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

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)

Sample screenshot 

 

Steps to add the Text Editor:-

In TextEdit.js the following Code displays the ColorPalette

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.

 

Add the following code in SendEmail.aspx page

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

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

 

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.

 

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

 

Actual DotNet 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 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 �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.

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

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

Enhancements that you 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 foget to change the Form tag property to encType="multipart/form-data"
  • Enable your browser for JavaScript because the colorpalette is based on JavaScript.
  • Configure your Default SMTP Virtual Server as SmtpMail.SmtpServer = "127.0.0.1" or Localhost.
  • Import 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