11/03/2013

How to send email with attachment? Vb.Net

Hey, I have designed my windows form for sending email with attachment document to any email id. my code is configured for gmail only and it is working great. This VB.Net code is also solve your problem for validated email pattern.


Windows form for sending email
 beside above controls , I have also used open file dailog control  and the code which I have used is as under

Imports System.Net.Mail
Imports System.Text.RegularExpressions
Imports Microsoft.Office.Interop


Public Class frmemail
    
    Private Sub BtnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSend.Click
        Dim SmtpServer As New SmtpClient()
        Dim mail As New MailMessage()
        SmtpServer.Credentials = New Net.NetworkCredential(" your gmail userid", "gmail password")
        SmtpServer.Host = "smtp.gmail.com" 'so what goes here?'
        SmtpServer.Port = 587 'or apparently you can use 587, do i actually have to define the port, and would the port depend on the host?'
        SmtpServer.EnableSsl = True
        mail.From = New MailAddress("yourgmailid@gmail.com")
        mail.To.Add(txtto.Text)
        mail.Subject = txtsubject.Text
        mail.Body = txtbody.Text
        If attachment = True Then
            'here is where we are adding the attachment.

            Dim attachment As New Attachment(Txtattachment.Text) 'create the attachment

            mail.Attachments.Add(attachment) 'add the attachment

        End If
        
        
    End Sub
    

    Private Sub BtnAttachment_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAttachment.Click
        attachment = True
        Txtattachment.Visible = True
        Dim OpenFileDialog1 As New OpenFileDialog
        With OpenFileDialog1
            .CheckFileExists = True
            .ShowReadOnly = False
            .Filter = "All Files|*.*|Bitmap Files (*)|*.bmp;*.gif;*.jpg"
            .FilterIndex = 1
            If .ShowDialog = DialogResult.OK Then
                Txtattachment.Text = OpenFileDialog1.FileName
            End If
        End With
    End Sub

    Private Sub frmemail_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Txtattachment.Visible = False
    End Sub
    Public Function EmailAddressCheck(ByVal emailAddress As String) As Boolean
        Dim pattern As String = "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]" & _
        "*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]{2,3}$"
        Dim emailAddressMatch As Match = Regex.Match(emailAddress, pattern)
        If emailAddressMatch.Success Then
            EmailAddressCheck = True

        Else
            EmailAddressCheck = False

        End If
    End Function


    Private Sub txtto_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtuserid.Validated
        Dim email As String = txtuserid.Text
        If EmailAddressCheck(email) = False Then

            Dim result As DialogResult _
            = MessageBox.Show("Enter a valid Email Address." & _
                                       " Do you want re-enter it?", "Invalid Email Address", _
                                       MessageBoxButtons.YesNo, MessageBoxIcon.Error)
            If result = Windows.Forms.DialogResult.Yes Then
                txtto.Focus()
                txtto.ForeColor = Color.Tomato
           
            End If
        Else
            txtto.ForeColor = Color.Black

        End If


    End Sub