c# .net Adsense ADO.NET Linq Viruses/security asp.net MVC JQuery Angular-js Node-js SEO Java C++ SQL API Networking vb.net .Net Css JavaScript Generics c#.Net entity framework HTML Website host Website Construction Guide HTTP tutorial W3C tutorial Web Services JSON Psychology Ionic framework Angular ReactJS Python Computer Android
vb.net

how to send email in vb.net using gmail server?

| | Networking

In this article, I will show you how to send email with vb.net using gmail sever.The SMTP (simple mail transfer protocol) server used for sending email with the help of  namespace System.Net.Mail. For gmail account, SMTP authentication is required, the user need to provide email ID and password. Also user needed to give the appropriate smtp port number for gmail server.

Here, I create a simple application to send email to clients from my Gmail account using vb.net. The email contains subject, body and file attachments and email ID for delivery.

Note: If you have problem with your google account (i.e failure sending mail), you need to switch on the less secure access.

https://www.google.com/settings/security/lesssecureapps

send mail vb.net Code: 

    Private Sub btnbrowse_Click(sender As Object, e As EventArgs) Handles btnbrowse.Click 

        Dim result As DialogResult = OpenFileDialog1.ShowDialog()
        If result = Windows.Forms.DialogResult.OK Then
            txtattach.Text =OpenFileDialog1.FileName
        End If 

    End Sub   Private Sub btnUpload_Click(sender As Object, e As EventArgs) Handles btnUpload.Click
        Try

            Dim i As Integer = 0
            For Each item As String In OpenFileDialog1.FileNames
 
                Dim fileName As String = item.Split("\").LastOrDefault().ToString()
                Dim isExists As Integer = (From row As DataGridViewRow In gridFileUpload.Rows Whererow.Cells(0).Value.ToString().Contains(fileName.ToString())).Count()
 
                If isExists = 0 Then
                    gridFileUpload.Rows.Add()
                    Dim gridrow As DataGridViewRow =gridFileUpload.Rows(gridFileUpload.Rows.Count - 1)
 
                    gridrow.Cells(0).Value =fileName.ToString()
                    gridrow.Cells(0).Tag = item
                End If
 
                i = i + 1
 
            Next
 
            If gridFileUpload.Rows.Count > 0 Then gridFileUpload.Visible = True
 
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information + MsgBoxStyle.OkOnly)
        End Try
    End Sub

 
Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
        SendEmailwithAttached(txtToEmail.Text, txtsubject.Text,txtbody.Text)
    End Sub

   Private Function SendEmailwithAttached(ByVal recipient As String, ByVal subject As String, ByVal body As String) As Boolean
        Try
 
            Dim Filenames As String = txtattach.Text
            Dim mm As New MailMessage("sender@gmail.com", recipient)
            mm.Subject = subject
            mm.Body = body
            mm.IsBodyHtml = True
 
            If gridFileUpload.Rows.Count > 0 Then
                For Each row As DataGridViewRow In gridFileUpload.Rows
                    If Not row.Cells("FileName").Tag Is Nothing Then
                        Dim MsgAttach As New Attachment(row.Cells("FileName").Tag)
                       mm.Attachments.Add(MsgAttach)
                    End If
                    ProBarSend.PerformStep()
                Next
            End If
 
            Dim smtp As New SmtpClient()
            smtp.Host = "smtp.gmail.com"
            smtp.EnableSsl = True
 
            Dim NetworkCred As New NetworkCredential()
            NetworkCred.UserName = "YOUR_MAIL_ID"
            NetworkCred.Password = "YOUR_PASSWORD"
            smtp.UseDefaultCredentials = True
 
            smtp.Credentials = NetworkCred
            smtp.Port = 587
            smtp.Send(mm)
            MsgBox("Email Send Successfully to " & "" & recipient.ToString().Substring(0, recipient.Length- 1).ToString() & "", MsgBoxStyle.Information + MsgBoxStyle.OkOnly)
 
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information + MsgBoxStyle.OkOnly)
        End Try
        Return True
    End Function

 

Description:

I used textboxes,buttons,openfileDialog and Datagridview controls. On browse button click, select the files using openfileDialog, and save the file paths on Datagridview using upload button.

 

On send button click,  message along with attached files send to the desired client Email ID.