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