First of all, you need need to set something in your gmail account to enable sending email using asp.net
1. Open you gmail account and go to setting
2. Click on Forwarding and POP/IMAP
3. Choose Enable POP for mail that arrives from now on
4. Click save changes

Here is the code for sending email from asp.net by using gmail account

create class sendmail.vb


Imports Microsoft.VisualBasic
Imports System
Imports System.Web.Mail

Public Class sendEmail

    Public Shared Function SendEmail(ByVal pGmailEmail As String, ByVal pGmailPassword As String, ByVal pTo As String, ByVal pSubject As String, ByVal pBody As String, ByVal pFormat As System.Web.Mail.MailFormat, _
     ByVal pAttachmentPath As String) As Boolean
        Try
            Dim myMail As New System.Web.Mail.MailMessage()
            myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", "smtp.gmail.com")
            myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465")
            myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", "2")

            myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1")
            'Use 0 for anonymous
            myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", pGmailEmail)
            myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", pGmailPassword)
            myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true")
            myMail.From = pGmailEmail
            myMail.[To] = pTo
            myMail.Subject = pSubject
            myMail.BodyFormat = pFormat
            myMail.Body = pBody

            If pAttachmentPath.Trim() <> "" Then
                Dim MyAttachment As New MailAttachment(pAttachmentPath)
                myMail.Attachments.Add(MyAttachment)
                myMail.Priority = System.Web.Mail.MailPriority.High
            End If

            System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com:465"
            System.Web.Mail.SmtpMail.Send(myMail)

            Return True
        Catch ex As Exception
            Throw
        End Try
    End Function

End Class

And this is the code to use the function

SendEmail("youraccount@gmail.com", "yourownGmailPassword", "yourfriend@yahoo.com", "HAHAHAHA", "AAAA", MailFormat.Text, Server.MapPath("Reports/Invoice.pdf"))

  • Share/Bookmark