Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / system

Using Gmail Account to Send Emails With Attachment

4.75/5 (4 votes)
8 Apr 2011CPOL 15.2K  
Man, that works great! I just converted it to VB.NET and also showed the Imports/Using that was needed, thanks to David's alternate. This was just what I was looking for, Thanks Abdur.Imports System.NetImports System.Net.MailPublic Class frmMain Private Sub frmMain_Load(sender As...
Man, that works great! I just converted it to VB.NET and also showed the Imports/Using that was needed, thanks to David's alternate. This was just what I was looking for, Thanks Abdur.
VB
Imports System.Net
Imports System.Net.Mail

Public Class frmMain
    Private Sub frmMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub BtnSend_Click(sender As System.Object, e As System.EventArgs) Handles btnSend.Click
        Dim MailFrom As MailAddress = New MailAddress("YourName@Gmail.com")
        Dim MailTo As MailAddress = New MailAddress("TheirName@Gmail.com")
        Dim NewMsg As MailMessage = New MailMessage(MailFrom, MailTo)

        NewMsg.Subject = "Test From VB.Net"
        NewMsg.Body = "Ok, Did I translate from C# to VB?"

        'For File Attachment, more files can also be attached
        Dim Att As Attachment = New Attachment("c:\\Picture.jpg")
        NewMsg.Attachments.Add(Att)
        Dim Smtp As SmtpClient = New SmtpClient("smtp.gmail.com", 587)
        Smtp.UseDefaultCredentials = False
        Smtp.Credentials = New NetworkCredential("YourName@gmail.com", "YourPassword")
        Smtp.EnableSsl = True
        Smtp.Send(NewMsg)

        '   //Alternative Short Method

        'Dim Smtp As SmtpClient = New SmtpClient("smtp.gmail.com", 587)

        'Smtp.UseDefaultCredentials = False
        'Smtp.Credentials = New NetworkCredential("username", "password")
        'Smtp.EnableSsl = True
        'Smtp.Send("sender@gamil.com", "receiver", "subject", "Email Body")

    End Sub
End Class

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)