Hello
I'm trying to send emails using smtp and getting an error
This is my code :
Imports System.Net.Mail
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SendEmail()
End Sub
Public Sub SendEmail()
Dim client As New SmtpClient()
Dim sendTo As New MailAddress("shtrees@gmail.com")
Dim from As MailAddress = New MailAddress("shtrees@gmail.com")
Dim message As New MailMessage(from, sendTo)
message.IsBodyHtml = True
message.Subject = "HI"
message.Body = "Got it!!"
' Use the same account in app.config to authenticate.
Dim basicAuthenticationInfo As New System.Net.NetworkCredential("shtrees@gmail.com", "THEPASSWORD")
client.Host = "smtp.gmail.com"
client.UseDefaultCredentials = False
client.Credentials = basicAuthenticationInfo
client.EnableSsl = True
Try
client.Send(message)
MsgBox("SUCCESS")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
End Class
This is the error i am having :---------------------------
SMTPSendmail
---------------------------
System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from)
at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at SMTPSendmail.Form1.SendEmail() in C:\Users\A1A4A\Desktop\Projects\Visual Basic 08\SMTPSendmail\SMTPSendmail\Form1.vb:line 23
---------------------------
OK
---------------------------
I tried changing the "To" to a hotmail account but still having the exact same error
I am 100% sure of my email and password. ( [email]shtrees@gmail.com[/email] )
As i am sure that i can send emails to myself ( To and From are the same )
I am using Visual basic 2008
Please help me finding the problem
Thanks
I think to send from gmail you need to specify it (gmail) as the host and refer to smtp.gmail.com
This C# article shows it but it is easy enough to convert the one line of code to VB
http://blog.ysatech.com/post/2010/12/02/ASP-NET-Send-email-using-GMAIL.aspx
Look at line 34 in particular.
Imports System.Net.Mail
Public Class FrmMailSystem
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
Sub sendEmail()
Try
SmtpServer.Credentials = New Net.NetworkCredential(Trim(txtID.Text), Trim(txtPassword.Text))
SmtpServer.Port = txtport.Text
SmtpServer.Host = txthost.Text
mail.From = New MailAddress(Trim(txtID.Text))
mail.To.Add(Trim(txtTo.Text))
mail.Subject = Trim(txtSubject.Text)
mail.Body = Trim(txtBody.Text)
SmtpServer.Send(mail)
MsgBox("Mail Sent", MsgBoxStyle.Information, "Mail Sent")
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
End Sub
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
If txtID.Text = "" Then
MsgBox("Please Enter Your Yahoo ID", MsgBoxStyle.Exclamation, "Error")
txtID.Focus()
ElseIf txtPassword.Text = "" Then
MsgBox("Please Enter Your Yahoo Password", MsgBoxStyle.Exclamation, "Error")
txtPassword.Focus()
ElseIf txtTo.Text = "" Then
MsgBox("Please Enter Address To Address", MsgBoxStyle.Exclamation, "Error")
txtTo.Focus()
ElseIf txtSubject.Text = "" Then
MsgBox("Please Enter Subject", MsgBoxStyle.Exclamation, "Error")
txtSubject.Focus()
ElseIf txtBody.Text = "" Then
MsgBox("Please Enter Body", MsgBoxStyle.Exclamation, "Error")
txtBody.Focus()
Else
sendEmail()
End If
End Sub