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. ( 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
Thanks

Recommended Answers

All 2 Replies

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
Be a part of the DaniWeb community

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