Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a problem with sending email in vb.net.
When I want to send an email this error will be shows to me:
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 Mil_Maker.Form9.sendEmail() in F:\USB\My Apps\Auto Letter Creater\V2.3\making letter automaticly\making letter automaticly\Form9.vb:line 22
Or:
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
And this is my code:
VB
Sub sendEmail()
        Try
         Dim SmtpServer As New SmtpClient()
    Dim mail As New MailMessage()   
            SmtpServer.Credentials = New Net.NetworkCredential(Trim(TextBox1.Text), Trim(TextBox2.Text))
            SmtpServer.Port = "587"
            If ComboBox2.Text.Contains("gmail") Then
                SmtpServer.EnableSsl = True
            Else
                SmtpServer.EnableSsl = False
            End If
            SmtpServer.Host = ComboBox2.Text
            mail.From = New MailAddress(Trim(TextBox1.Text))
            mail.To.Add(Trim(TextBox3.Text))
            mail.Subject = Trim(TextBox5.Text)
            mail.Body = Trim(Form1.RichTextBox1.Text)
            SmtpServer.Send(mail)
            MsgBox("Mail Sent", MsgBoxStyle.Information, "Mail Sent")
            Me.Close()
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try

    End Sub

I want to send email from gmail but this code just work for yahoo mails.
How I can solve this problem???
Please help me.
Thank You.
Posted
Updated 22-Aug-13 7:52am
v6
Comments
joshrduncan2012 22-Aug-13 12:08pm    
What's the difference between Trim(TextBox1.Text)) and TextBox1.Text.Trim()?

You can use this class to send email through gmail... or any other mail server you have the credentials for. I used it to send mail through a gmail account set up as a relay.

VB
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Net.Mail

Public Class EmailRelay
    Public smtpServer As String
    Public userName As String
    Public password As String

    Public Sub new(ByVal _smtpServer As String, ByVal _userName As String, ByVal _password As String)
        smtpServer  = _smtpServer
        userName    = _userName
        password    = _password
    End Sub

    Public Shared Function ServerCertificateValidationCallback(ByVal sender As Object, _
                                                               ByVal cert As System.Security.Cryptography.X509Certificates.X509Certificate, _
                                                               ByVal chain As System.Security.Cryptography.X509Certificates.X509Chain, _
                                                               ByVal sslPolicyErrors As Net.Security.SslPolicyErrors) As Boolean
        Return sslPolicyErrors = Net.Security.SslPolicyErrors.None
    End Function

    Public Function SendMail(ByVal fromAddress As String, ByVal toAddress as String, 
                                ByVal subject As String, ByVal body As String, 
                                Optional ByVal fromEmailName As String = "",
                                Optional Byval useSSL As Boolean = True, 
                                Optional ByRef errMsg As String = "") As Boolean

        Dim mm As New MailMessage()
		Dim smtp As New SmtpClient()
        Dim emailFrom as New MailAddress(fromAddress, fromEmailName) 
        Dim NetworkCred As New System.Net.NetworkCredential()

        Try
            NetworkCred.UserName = userName  ' "yourRelayEmailAcct@gmail.com"
            NetworkCred.Password = password  

            With mm
                .[To].Add(New MailAddress(toAddress))
                .From                   = emailFrom
                .Subject                = subject
                .Body                   = body
                .IsBodyHtml             = True
            End With

            With smtp
                .Host                   = smtpServer ' "smtp.gmail.com"
                .EnableSsl              = useSSL ' You can also add this in the webconfig
                .UseDefaultCredentials  = True
                .Credentials            = NetworkCred
                .Port                   = 587
                .Send(mm)
            End With

        Catch ex As Exception
            errMsg = ex.Message
            Return False
        End Try
        
        Return True
        
    End Function

End Class
 
Share this answer
 
Comments
Pouya Mozafar 23-Aug-13 6:49am    
I thing the gmail's ssl is not true for some users because my code is work for some of users not all of them!
pdoxtader 23-Aug-13 9:02am    
When I used this to relay mail through the Gmail account, I believe I left useSSL at it's default value (True). When I used it to send mail through my (current) company's mail server, I had to set it to False. You'll have to experiment with it and see what works with each mail server you're trying to use... some will support SSl, and some won't (depending on the server configuration).

As far as some users requiring it, and others not... I only ever used THIS code with two Gmail accounts, so I can't speak to that. But it doesn't sound likely to me.
Check my tutorial. You just have to do some changes. Like server (for gmail because I used hotmail, outlook e live accounts server) and apply you textboxs in code.

Take a look: Tutorial here
 
Share this answer
 
Comments
Pouya Mozafar 25-Aug-13 4:52am    
I cannot go to that site give me the code here.
Thanks
Ok no problem man.

VB
Imports System.Net.Mail


VB
Try

                    Dim Mail As New MailMessage
                    Mail.Subject = Subject.Text
                    Mail.To.Add(To.Text)
                    Mail.From = New MailAddress("your_email@hotmail.com")
                    Mail.Body = Body.Text

                    Dim SMTP As New SmtpClient("smtp.live.com") 'server of Outlook, Live and Hotmail accounts

                    SMTP.EnableSsl = True
                    SMTP.Credentials = New System.Net.NetworkCredential("your_email@hotmail.com", "your_password")
                    SMTP.Port = 587 ‘port for Microsoft accounts
                    SMTP.Send(Mail)
                    MsgBox("Message has been sent with sucess", vbOKOnly + vbInformation, "YUPII!") ‘Finally show a MessageBox
                   
                Catch ex As Exception
                    MsgBox(ex.Message, vbOK + vbCritical, "Error")
                End Try
 
Share this answer
 
Comments
Pouya Mozafar 25-Aug-13 5:08am    
It is not work...!
It says:
Error
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
FabioDaniel 25-Aug-13 5:10am    
Make sure you replaced the e-mail and password correctly. Then try it again.
Pouya Mozafar 25-Aug-13 5:14am    
Again it is not work...
The error is the same....If you sure your code is true please give me your email to talk about problem...
How I can send an email from gmail without errors ?for some users the email will be send from gmail but for another my code is not work and send?
does it have any special code?
SharmaRajesh1 20-Sep-22 3:39am    
its showing error failure sending mail.kindly help
VB
Try
 
                    Dim Mail As New MailMessage
                    Mail.Subject = "Subject"
                   Mail.To.Add("e-mail will receive it") 'Replace here
                    Mail.From = New MailAddress("your_email@gmail.com")
                    Mail.Body = "Hello"
 
                    Dim SMTP As New SmtpClient("smtp.gmail.com") 'server of GMAIL

                    SMTP.EnableSsl = True
                    SMTP.Credentials = New System.Net.NetworkCredential("your_email@gmail.com", "your_password") 'replace here
                    SMTP.Port = 587 ‘port GMAIL
                    SMTP.Send(Mail)
                    MsgBox("Message has been sent with sucess", vbOKOnly + vbInformation, "Sent") ‘Finally show a MessageBox
                   
                Catch ex As Exception
                    MsgBox(ex.Message, vbOK + vbCritical, "Error")
                End Try
 
Share this answer
 
Try this:

VB
Dim SMTPClientObj As New Net.Mail.SmtpClient
SMTPClientObj.UseDefaultCredentials = False
SMTPClientObj.Credentials = New System.Net.NetworkCredential("myusername@gmail.com", "mypwd")
SMTPClientObj.Host = "smtp.gmail.com"
SMTPClientObj.Port = 587
SMTPClientObj.EnableSsl = True
SMTPClientObj.Send("myusername@gmail.com","yourusername@gmail.com","test","testbody")
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900