Mark Tabor

Mark Tabor

  • 585
  • 2k
  • 460.2k

send email using SMTP gmail

Jul 25 2016 6:57 AM
I have two methods for sending email to all email addresses even my domain email address the first method works fine for all,while the second method is sending email to all other email addresses except my domain email address why is it so .below are the two methods 

First Method:

Private Sub SendHtmlFormattedEmail(ByVal recepientEmail As String, ByVal subject As String, ByVal body As String)

Dim mailMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage

Try

mailMessage.From = New MailAddress(ConfigurationManager.AppSettings("UserName"))

mailMessage.Subject = subject

mailMessage.Body = body

mailMessage.IsBodyHtml = True

mailMessage.To.Add(New MailAddress(recepientEmail))

Dim files As List(Of HttpPostedFile) = DirectCast(Cache(Me.Key), List(Of HttpPostedFile))

For Each file As HttpPostedFile In files

mailMessage.Attachments.Add(New Attachment(file.InputStream, Path.GetFileName(file.FileName), file.ContentType))

Next

Dim SMTP As New SmtpClient("smtp.gmail.com")

SMTP.EnableSsl = True

SMTP.Credentials = New System.Net.NetworkCredential("[email protected]", "forgot123")

SMTP.Port = 587

SMTP.Send(mailMessage)

smtp.Send(mailMessage)

Catch ex As Exception

ex.Message.ToString()

End Try

End Sub

Second Method:

Private Sub SendEmail()

GetUserIDEmail(Session("LoggedInUserId"))

Dim Mail As New MailMessage

Try

Mail.IsBodyHtml = True

Dim MailText As String = Nothing

'Dim attachment As System.Net.Mail.Attachment

Mail.Subject = " Report Submitted By : " & Session("LoggedInUserFullName") & " " & "User Type :" & GetUserTypeName(CInt(Session("UserTypes")))

MailText = MailText + "<font face='verdana' color='#000000'>" & "Dear User, " & "</font><br/><br/>" & Environment.NewLine

MailText = MailText + "<font face='verdana' color='#000000'>" + " " & "we are glad to inform you that your records has accepted.</font>" & "<br/> ""<font Face='Verdana' font-size:14px;Color='#000000'><b><br/>" + AcceptComments & """</font> " + Environment.NewLine

MailText = MailText + "<font Face='Verdana' font-size:14px;Color='#000000'><b><br/>" & " Regards," & " " & "<br/>" & "<i>Plan Pakistan RBME TEAM<i>" + "</b></font>" & vbCrLf

Mail.IsBodyHtml = True

Mail.Body = MailText

Dim files As List(Of HttpPostedFile) = DirectCast(Cache(Me.Key), List(Of HttpPostedFile))

For Each file As HttpPostedFile In files

Mail.Attachments.Add(New Attachment(file.InputStream, Path.GetFileName(file.FileName), file.ContentType))

Next

Mail.From = New MailAddress("[email protected]")

Dim SMTP As New SmtpClient("smtp.gmail.com")

SMTP.EnableSsl = True

SMTP.Credentials = New System.Net.NetworkCredential("[email protected]", "forgot123")

SMTP.Port = 587

SMTP.Send(Mail)

Catch ex As Exception

ex.Message.ToString()

End Try

End Sub

 

Answers (5)