Click here to Skip to main content
15,868,141 members
Articles / Desktop Programming / System
Alternative
Tip/Trick

Using Gmail Account to Send Emails With Attachment

Rate me:
Please Sign up or sign in to vote.
4.75/5 (4 votes)
8 Apr 2011CPOL 13.7K   5   5
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThanks A LOT ... Pin
Lenin Paily (6907069)29-Sep-13 23:09
Lenin Paily (6907069)29-Sep-13 23:09 
GeneralReason for my vote of 4 Very useful...thanks a lot for shari... Pin
Henco Eloff19-Jun-11 23:08
Henco Eloff19-Jun-11 23:08 
GeneralReason for my vote of 5 I'm a "curly brace" programmer but I... Pin
Keith.Badeau8-May-11 1:33
Keith.Badeau8-May-11 1:33 
GeneralIn a way of speaking anything that is done differently(even ... Pin
charles henington4-Apr-11 23:11
charles henington4-Apr-11 23:11 
GeneralThough this cannot be considered as an alternative yet, I gu... Pin
Sandeep Mewara29-Mar-11 9:08
mveSandeep Mewara29-Mar-11 9:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.