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

    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
        Catch ex As Exception
        End Try

    End Sub
End Class

This is the error i am having :

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

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. ( shtrees@gmail.com )
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

Recommended Answers

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

Jump to Post

All 2 Replies

Imports System.Net.Mail
Public Class FrmMailSystem
    Dim SmtpServer As New SmtpClient()
    Dim mail As New MailMessage()

    Sub sendEmail()

            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.Subject = Trim(txtSubject.Text)
            mail.Body = Trim(txtBody.Text)
            MsgBox("Mail Sent", MsgBoxStyle.Information, "Mail Sent")
        Catch ex As Exception
        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")
        ElseIf txtPassword.Text = "" Then
            MsgBox("Please Enter Your Yahoo Password", MsgBoxStyle.Exclamation, "Error")
        ElseIf txtTo.Text = "" Then
            MsgBox("Please Enter Address To Address", MsgBoxStyle.Exclamation, "Error")
        ElseIf txtSubject.Text = "" Then
            MsgBox("Please Enter Subject", MsgBoxStyle.Exclamation, "Error")
        ElseIf txtBody.Text = "" Then
            MsgBox("Please Enter Body", MsgBoxStyle.Exclamation, "Error")
        End If
    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.