VB .net Sending Email System using yahoo or google failed to send

Ritchie Liz 36 Reputation points
2021-01-29T08:39:27.093+00:00

Good Day, I have a system for sending email using VB .Net since 2012. But last week this program failed to send email. Is there an update from microsoft related on this? I try both yahoo or gmail server but still not working.

This is my Code:

Dim Smtp_Server As New SmtpClient
Dim e_mail As New MailMessage()
Smtp_Server.UseDefaultCredentials = False
Smtp_Server.Credentials = New Net.NetworkCredential(TextBox_SenderEADD.Text, TextBox_Password.Text)
'Smtp_Server.Port = 587
Smtp_Server.Port = 465
Smtp_Server.EnableSsl = True
'Smtp_Server.Host = "smtp.mail.yahoo.com"
Smtp_Server.Host = "smtp.gmail.com"

e_mail = New MailMessage()
e_mail.From = New MailAddress(TextBox_SenderEADD.Text)
e_mail.To.Add(TextBox_Eadd.Text)
If MailSubject = "Senttoall" Then
e_mail.Subject = TextBox_sendtoallsubject.Text
Else
e_mail.Subject = "RCDC Billing Statement for " & Format(DateTimePicker1.Value, "MMMM yyyy") & ""
End If
e_mail.IsBodyHtml = False
e_mail.Body = TextBox_Message.Text
Smtp_Server.Send(e_mail)

Developer technologies VB
{count} vote

Accepted answer
  1. Xingyu Zhao-MSFT 5,381 Reputation points
    2021-02-01T06:16:03.543+00:00

    Hi @Ritchie Liz ,

    Gmail will prevent access for your e-mail account from custom applications. You need to turn on "Allow less secure apps".
    Also see: Gmail Error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
    Hope it could be helpful.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Castorix31 90,681 Reputation points
    2021-01-29T09:30:14.957+00:00

    I just tested a similar code with gmail (Port = 587) and it worked after I updated this parameter :

    lesssecureapps

    2 people found this answer helpful.
    0 comments No comments

  2. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-01-29T12:31:46.817+00:00

    Hello,

    If all worked on Yahoo and GMail before then there may be something blocking locally e.g. Windows Defender or a anti-virus application. You can try using SendCompleted event of the SmtpClient object, in the code below on error the error message is written to the IDE, either immediate or output window depending on how you have things setup. One unknown is how MailSubject is used.

    Mail code is in a class as this a) keeps form code clean b) is now reusable. The event MessageSendCompleted has a local variable mailMessage which is not used yet if needed provides access to the MailMessage in the SendSingleMessage method.

    Imports System.ComponentModel
    Imports System.Net
    Imports System.Net.Mail
    
    Public Class MailOperations
    
        Public Shared MailSubject As String
    
        Public Shared Async Sub SendSingleMessage(
           userName As String,
           password As String,
           message As String,
           fromAddress As String,
           tooAddress As String,
           subject As String,
           billDate As DateTime)
    
            Dim Smtp_Server = New SmtpClient("", 0)
            Dim e_mail As New MailMessage()
            Smtp_Server.UseDefaultCredentials = False
            Smtp_Server.Credentials = New NetworkCredential(userName, password)
            'Smtp_Server.Port = 587
            Smtp_Server.Port = 465
            Smtp_Server.EnableSsl = True
            'Smtp_Server.Host = "smtp.mail.yahoo.com"
            Smtp_Server.Host = "smtp.gmail.com"
            e_mail = New MailMessage()
            e_mail.From = New MailAddress(fromAddress)
            e_mail.To.Add(tooAddress)
    
            If MailSubject = "Senttoall" Then
                e_mail.Subject = subject
            Else
                e_mail.Subject = $"RCDC Billing Statement for {billDate.ToString("MMMM yyyy")}"
            End If
    
            e_mail.IsBodyHtml = False
            e_mail.Body = message
    
            AddHandler Smtp_Server.SendCompleted, AddressOf MessageSendCompleted
    
            Dim token = New Object
    
            Await Task.Run(
                Sub()
    
                    Task.Delay(1)
                    Smtp_Server.SendAsync(e_mail, token)
    
                End Sub)
    
        End Sub
    
        Private Shared Sub MessageSendCompleted(sender As Object, eventArguments As AsyncCompletedEventArgs)
    
            Dim tempVar As Boolean = TypeOf eventArguments.UserState Is MailMessage
    
            Dim mailMessage As MailMessage = If(tempVar, CType(eventArguments.UserState, MailMessage), Nothing)
    
            If eventArguments.Error IsNot Nothing Then
                If TypeOf eventArguments.Error Is SmtpException Then
                    Debug.WriteLine($"Smtp error: {eventArguments.Error.Message}")
                Else
                    Debug.WriteLine($"General error: {eventArguments.Error.Message}")
                End If
            End If
        End Sub
    End Class
    

    Form code

    Public Class Form1
        Private Sub SendButton_Click(sender As Object, e As EventArgs) Handles SendButton.Click
    
            MailOperations.MailSubject = ""
    
            MailOperations.SendSingleMessage(
                TextBox_SenderEADD.Text,
                TextBox_Password.Text,
                TextBox_Message.Text,
                TextBox_SenderEADD.Text,
                TextBox_Eadd.Text, TextBox_sendtoallsubject.Text,
                DateTimePicker1.Value)
    
        End Sub
    End Class
    
    2 people found this answer helpful.
    0 comments No comments

  3. Ritchie Liz 36 Reputation points
    2021-02-01T05:25:17.317+00:00

    62294-email-error.jpg

    I try to catch the message error. This was the error appeared during sending email using Smtp_Server.Port = 587

    1 person found this answer helpful.

  4. Ritchie Liz 36 Reputation points
    2021-02-02T01:23:28.383+00:00

    Thanks. I turn on "Allow less secure apps". It works now.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.