Click here to Skip to main content
12,882,397 members (26,250 online)
Click here to Skip to main content

Stats

814.4K views
5.6K downloads
91 bookmarked
Posted 3 Mar 2004

Send mail using System.Web.Mail namespace

, 23 Apr 2013 CPOL
This tutorial explains how to send mails with attachments
WebMail
bin
WebMail.exe
Interop.POPMailProject.dll
AxInterop.POPMailProject.dll
obj
Debug
temp
TempPE
WebMail.vbproj.user
Imports System.Web.Mail
Public Class Form2
    Inherits System.Windows.Forms.Form
    Dim obj As _
    System.Web.Mail.SmtpMail ' Variable which will send the mail
    Dim Attachment As System.Web.Mail.MailAttachment    'Variable to store the attachments
    Dim Mailmsg As New System.Web.Mail.MailMessage()    'Variable to create the message to send
#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents txtSMTPServer As System.Windows.Forms.TextBox
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents txtFrom As System.Windows.Forms.TextBox
    Friend WithEvents txtFromDisplayName As System.Windows.Forms.TextBox
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents Label7 As System.Windows.Forms.Label
    Friend WithEvents txtTo As System.Windows.Forms.TextBox
    Friend WithEvents OFD As System.Windows.Forms.OpenFileDialog
    Friend WithEvents lstAttachment As System.Windows.Forms.ListBox
    Friend WithEvents btnAdd As System.Windows.Forms.Button
    Friend WithEvents btnRemove As System.Windows.Forms.Button
    Friend WithEvents Label8 As System.Windows.Forms.Label
    Friend WithEvents Label9 As System.Windows.Forms.Label
    Friend WithEvents Label10 As System.Windows.Forms.Label
    Friend WithEvents chkFormat As System.Windows.Forms.CheckBox
    Friend WithEvents btnSend As System.Windows.Forms.Button
    Friend WithEvents txtSubject As System.Windows.Forms.TextBox
    Friend WithEvents txtMessage As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.txtSMTPServer = New System.Windows.Forms.TextBox()
        Me.txtFrom = New System.Windows.Forms.TextBox()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.txtFromDisplayName = New System.Windows.Forms.TextBox()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.Label4 = New System.Windows.Forms.Label()
        Me.Label7 = New System.Windows.Forms.Label()
        Me.txtTo = New System.Windows.Forms.TextBox()
        Me.OFD = New System.Windows.Forms.OpenFileDialog()
        Me.lstAttachment = New System.Windows.Forms.ListBox()
        Me.btnAdd = New System.Windows.Forms.Button()
        Me.btnRemove = New System.Windows.Forms.Button()
        Me.Label8 = New System.Windows.Forms.Label()
        Me.txtSubject = New System.Windows.Forms.TextBox()
        Me.Label9 = New System.Windows.Forms.Label()
        Me.txtMessage = New System.Windows.Forms.TextBox()
        Me.Label10 = New System.Windows.Forms.Label()
        Me.chkFormat = New System.Windows.Forms.CheckBox()
        Me.btnSend = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(24, 32)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(72, 16)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "SMTP Server"
        '
        'txtSMTPServer
        '
        Me.txtSMTPServer.Location = New System.Drawing.Point(144, 32)
        Me.txtSMTPServer.Name = "txtSMTPServer"
        Me.txtSMTPServer.Size = New System.Drawing.Size(280, 20)
        Me.txtSMTPServer.TabIndex = 1
        Me.txtSMTPServer.Text = ""
        '
        'txtFrom
        '
        Me.txtFrom.Location = New System.Drawing.Point(144, 64)
        Me.txtFrom.Name = "txtFrom"
        Me.txtFrom.Size = New System.Drawing.Size(232, 20)
        Me.txtFrom.TabIndex = 3
        Me.txtFrom.Text = ""
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(24, 64)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(72, 16)
        Me.Label2.TabIndex = 2
        Me.Label2.Text = "From"
        '
        'txtFromDisplayName
        '
        Me.txtFromDisplayName.Location = New System.Drawing.Point(144, 96)
        Me.txtFromDisplayName.Name = "txtFromDisplayName"
        Me.txtFromDisplayName.Size = New System.Drawing.Size(232, 20)
        Me.txtFromDisplayName.TabIndex = 5
        Me.txtFromDisplayName.Text = ""
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(24, 96)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(104, 32)
        Me.Label3.TabIndex = 4
        Me.Label3.Text = "From Display name"
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(32, 96)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(104, 32)
        Me.Label4.TabIndex = 4
        Me.Label4.Text = "From Display name"
        '
        'Label7
        '
        Me.Label7.Location = New System.Drawing.Point(24, 128)
        Me.Label7.Name = "Label7"
        Me.Label7.Size = New System.Drawing.Size(72, 16)
        Me.Label7.TabIndex = 6
        Me.Label7.Text = "To"
        '
        'txtTo
        '
        Me.txtTo.Location = New System.Drawing.Point(144, 128)
        Me.txtTo.Name = "txtTo"
        Me.txtTo.Size = New System.Drawing.Size(232, 20)
        Me.txtTo.TabIndex = 7
        Me.txtTo.Text = ""
        '
        'OFD
        '
        Me.OFD.DefaultExt = "*.*"
        Me.OFD.InitialDirectory = "c:\"
        Me.OFD.Multiselect = True
        '
        'lstAttachment
        '
        Me.lstAttachment.Location = New System.Drawing.Point(144, 192)
        Me.lstAttachment.Name = "lstAttachment"
        Me.lstAttachment.Size = New System.Drawing.Size(232, 82)
        Me.lstAttachment.TabIndex = 10
        '
        'btnAdd
        '
        Me.btnAdd.Location = New System.Drawing.Point(392, 192)
        Me.btnAdd.Name = "btnAdd"
        Me.btnAdd.Size = New System.Drawing.Size(120, 32)
        Me.btnAdd.TabIndex = 11
        Me.btnAdd.Text = "Add attachment"
        '
        'btnRemove
        '
        Me.btnRemove.Location = New System.Drawing.Point(392, 232)
        Me.btnRemove.Name = "btnRemove"
        Me.btnRemove.Size = New System.Drawing.Size(120, 32)
        Me.btnRemove.TabIndex = 12
        Me.btnRemove.Text = "Remove attachment"
        '
        'Label8
        '
        Me.Label8.Location = New System.Drawing.Point(24, 192)
        Me.Label8.Name = "Label8"
        Me.Label8.Size = New System.Drawing.Size(72, 32)
        Me.Label8.TabIndex = 13
        Me.Label8.Text = "Attachments"
        '
        'txtSubject
        '
        Me.txtSubject.Location = New System.Drawing.Point(144, 288)
        Me.txtSubject.Name = "txtSubject"
        Me.txtSubject.Size = New System.Drawing.Size(368, 20)
        Me.txtSubject.TabIndex = 15
        Me.txtSubject.Text = ""
        '
        'Label9
        '
        Me.Label9.Location = New System.Drawing.Point(24, 288)
        Me.Label9.Name = "Label9"
        Me.Label9.Size = New System.Drawing.Size(104, 16)
        Me.Label9.TabIndex = 14
        Me.Label9.Text = "Subject"
        '
        'txtMessage
        '
        Me.txtMessage.Location = New System.Drawing.Point(144, 320)
        Me.txtMessage.Multiline = True
        Me.txtMessage.Name = "txtMessage"
        Me.txtMessage.ScrollBars = System.Windows.Forms.ScrollBars.Both
        Me.txtMessage.Size = New System.Drawing.Size(368, 144)
        Me.txtMessage.TabIndex = 17
        Me.txtMessage.Text = ""
        '
        'Label10
        '
        Me.Label10.Location = New System.Drawing.Point(24, 320)
        Me.Label10.Name = "Label10"
        Me.Label10.Size = New System.Drawing.Size(104, 16)
        Me.Label10.TabIndex = 16
        Me.Label10.Text = "Message"
        '
        'chkFormat
        '
        Me.chkFormat.Location = New System.Drawing.Point(528, 336)
        Me.chkFormat.Name = "chkFormat"
        Me.chkFormat.Size = New System.Drawing.Size(112, 24)
        Me.chkFormat.TabIndex = 19
        Me.chkFormat.Text = "Send AS HTML"
        '
        'btnSend
        '
        Me.btnSend.Location = New System.Drawing.Point(528, 424)
        Me.btnSend.Name = "btnSend"
        Me.btnSend.Size = New System.Drawing.Size(104, 32)
        Me.btnSend.TabIndex = 20
        Me.btnSend.Text = "Send"
        '
        'Form2
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(664, 525)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnSend, Me.chkFormat, Me.txtMessage, Me.Label10, Me.txtSubject, Me.Label9, Me.Label8, Me.btnRemove, Me.btnAdd, Me.lstAttachment, Me.Label7, Me.txtTo, Me.txtFromDisplayName, Me.Label3, Me.txtFrom, Me.Label2, Me.txtSMTPServer, Me.Label1, Me.Label4})
        Me.Name = "Form2"
        Me.Text = "Send email"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        'Show open dialogue box to select the files to attach
        Dim Counter As Integer
        OFD.CheckFileExists = True
        OFD.Title = "Select file(s) to attach"
        OFD.ShowDialog()
        For Counter = 0 To UBound(OFD.FileNames)
            lstAttachment.Items.Add(OFD.FileNames(Counter))
        Next
    End Sub

    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        'Remove the attachments
        If lstAttachment.SelectedIndex > -1 Then
            lstAttachment.Items.RemoveAt(lstAttachment.SelectedIndex)
        End If
    End Sub

    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        Dim Counter As Integer

        'Validate the data
        If txtSMTPServer.Text = "" Then
            MsgBox("Enter the SMTP server info ...!!!", MsgBoxStyle.Information, "Send Email")
            Exit Sub
        End If

        If txtFrom.Text = "" Then
            MsgBox("Enter the From email address ...!!!", MsgBoxStyle.Information, "Send Email")
            Exit Sub
        End If

        If txtTo.Text = "" Then
            MsgBox("Enter the Recipient email address ...!!!", MsgBoxStyle.Information, "Send Email")
            Exit Sub
        End If

        If txtSubject.Text = "" Then
            MsgBox("Enter the Email subject ...!!!", MsgBoxStyle.Information, "Send Email")
            Exit Sub
        End If

        'Set the properties

        obj.SmtpServer = txtSMTPServer.Text
        'Multiple recepients can be specified using ; as the delimeter
        Mailmsg.To = txtTo.Text
        Mailmsg.From = "\" & txtFromDisplayName.Text & "\ <" & txtFrom.Text & ">"

        'Specify the body format
        If chkFormat.Checked = True Then
            Mailmsg.BodyFormat = MailFormat.Html   'Send the mail in HTML Format
        Else
            Mailmsg.BodyFormat = MailFormat.Text
        End If

        'If you want you can add a reply to header 
        'Mailmsg.Headers.Add("Reply-To", "Manoj@geinetech.net")
        'custom headersare added like this
        'Mailmsg.Headers.Add("Manoj", "TestHeader")
        Mailmsg.Subject = txtSubject.Text
        For Counter = 0 To lstAttachment.Items.Count - 1
            Attachment = New MailAttachment(lstAttachment.Items(Counter))
            Mailmsg.Attachments.Add(Attachment)
        Next

        Mailmsg.Body = txtMessage.Text
        obj.Send(Mailmsg)
    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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

ManojRajan
Web Developer
United States United States
I'm ManojRajan, Working as a consultant architect in Tennessee. I have more than 8 years of experience in Microsoft Technologies.

You may also be interested in...

Pro
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170422.1 | Last Updated 23 Apr 2013
Article Copyright 2004 by ManojRajan
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid