如何在Gmail中进行自动化

时间:2015-09-07 02:59:17

标签: vb.net gmail

在使用Gmail向特定用户发送电子邮件时,我遇到了问题。实际上我的项目是从一个位置复制一个Excel文件并将其发送给特定的人。文件已成功复制,但无法发送电子邮件。将出现一个消息框,显示

  

System.Net.Mail.SmtpException:SMTP服务器需要安全连接,或者客户端未经过身份验证。服务器响应是:

     

5.7.0必须首先发出STARTTLS命令。 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

1 个答案:

答案 0 :(得分:0)

您忘记启用SSL ...

SmtpServer.EnableSsl = True