-1

Here I have a problem when sending an email to a specific person using Gmail. Actually my project is copying an Excel file from a location and sending it to a specific person. The file is successfully copied, but the email cannot be send. A message box appears, which is showing

System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. the server response was:

5.7.0 Must issue a STARTTLS command first. qh5sm9940329pbb.71 - gsmtp"

Sub Main()
    CopyFile = "\\192.168.5.1\fbar\TOOLS\ProbingApps\ProbingSystem\testMail.xlsx"

    MyHour = Format(Now, "hh")
    MyMinutes = Format(Now, "mm")
    MySec = Format(Now, "ss")
    MyDate = Format(Now, "dd")
    MyMonth = Format(Now, "MM")
    myYear = Format(Now, "yyyy")
    Mydirectory = "C:\Users\Thaneskumar\Desktop\test2\"
    MyExtension = ".xlsx"
    MyFileName = Mydirectory + MyHour + MyMinutes + MySec + "_" + MyDate + _
        MyMonth + "_" + myYear + MyExtension

    CopyTo = MyFileName

    If System.IO.File.Exists(CopyFile) = True Then
        System.IO.File.Copy(CopyFile, CopyTo)
        MsgBox("File Copied")
    End If
    automail()
    MsgBox("Mail send")
End Sub

Public Sub automail()
    Try
        Dim SmtpServer As New SmtpClient()
        Dim mail As New MailMessage()
        SmtpServer.Credentials = New _
            Net.NetworkCredential("cgthanes44@gmail.com", "******")
        SmtpServer.Port = 587
        SmtpServer.Host = "smtp.gmail.com"
        mail = New MailMessage()
        mail.From = New MailAddress("cgthanes44@gmail.com")
        mail.To.Add("cgthanes33@yahoo.com")
        mail.Subject = "Test Mail"
        Dim attach As New Attachment(MyFileName)
        mail.Attachments.Add(attach)
        mail.Body = "This is for testing SMTP mail from GMAIL"
        SmtpServer.Send(mail)
        MsgBox("mail send")
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
cgkumar
  • 19
  • 1
  • 7

1 Answers1

0

You forgot to enable SSL...

SmtpServer.EnableSsl = True
Trevor
  • 7,777
  • 6
  • 31
  • 50
  • Yes. Thanks. But now I m have other error. Which is "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". Please Help me...Anyway thanks for the fast reply.... – cgkumar Sep 07 '15 at 03:16
  • Do you have two step verification. If so you need to generate an app specific password. – Trevor Sep 07 '15 at 03:23
  • "generate an app specific password" what that means? – cgkumar Sep 07 '15 at 03:25
  • Sometimes Google will send a short code to your phone or such for verification when logging into your account. It is good for security reasons. – Trevor Sep 07 '15 at 03:27
  • Heres another answer of mine that may be of use: http://stackoverflow.com/questions/23360415/fail-sending-email-unable-to-connect-the-remote-server/23362096#23362096 – Trevor Sep 07 '15 at 03:31
  • that means my computer firewall is block for send the email? – cgkumar Sep 07 '15 at 03:38
  • Thats possible but i can not confirm that from here – Trevor Sep 07 '15 at 03:40
  • I turn off the firewall and test to send the email, but it still show same error. What I m need to do? Please help me... – cgkumar Sep 07 '15 at 03:47
  • After I add the commend, still appear the same error. "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". – cgkumar Sep 08 '15 at 00:43
  • it also mention that have problem at line "SmtpServer.Send(mail) " – cgkumar Sep 08 '15 at 00:47