Click here to Skip to main content
15,886,578 members
Articles / Web Development / ASP.NET

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.9K   4.4K   65  
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.
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Configuration
Imports System.Drawing
Imports System.Data
Imports System.IO
Imports System.Web
Imports System.Web.Mail
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls

Public Class clsmymail
    Inherits System.Web.UI.Page
    Protected WithEvents TextBox4 As System.Web.UI.WebControls.TextBox
    Protected WithEvents TextBox3 As System.Web.UI.WebControls.TextBox
    Protected WithEvents TextBox2 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Label5 As System.Web.UI.WebControls.Label
    Protected WithEvents Label4 As System.Web.UI.WebControls.Label
    Protected WithEvents Label3 As System.Web.UI.WebControls.Label
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    Protected WithEvents Button2 As System.Web.UI.WebControls.Button
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents Label2 As System.Web.UI.WebControls.Label
    Protected WithEvents lblMessage As System.Web.UI.WebControls.Label
    Protected WithEvents Textbox1 As System.Web.UI.WebControls.TextBox
    Protected WithEvents pnlContentMgmt As System.Web.UI.WebControls.Panel
    Protected WithEvents txtcc As System.Web.UI.WebControls.TextBox
    Protected WithEvents Label6 As System.Web.UI.WebControls.Label
    Protected WithEvents Label7 As System.Web.UI.WebControls.Label
    Protected WithEvents TextBox5 As System.Web.UI.WebControls.TextBox
    Protected WithEvents txtbcc As System.Web.UI.WebControls.TextBox
    Protected WithEvents attachFile1 As System.Web.UI.HtmlControls.HtmlInputFile
    Protected hdnmsg As System.Web.UI.HtmlControls.HtmlInputHidden

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()


    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        attachFile1.Visible = False
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        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)
        If (attach1 <> "") Then
            File.Delete(Server.MapPath(attach1))
        End If
        Response.Flush()
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        txtcc.Text = ""
        txtbcc.Text = ""
        lblMessage.Visible = True
        lblMessage.ForeColor = Color.Red
        lblMessage.Text = "Your email has been sent"
    End Sub

    Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        attachFile1.Visible = True
    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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