电子邮件不能使用主机上的Gmail smtp发送

时间:2014-06-19 12:21:47

标签: vb.net email smtp gmail

我已使用此代码发送电子邮件。当我在LOCALHOST中测试此代码时,它可以正常工作,但在主机中,我收到此错误:

  

发送mail.few失败

我的代码是:

Dim MailMsg As New MailMessage(New Net.Mail.MailAddress("MYUSERNAME@gmail.com"), New Net.Mail.MailAddress("myname@gmail.com"))
MailMsg.Subject = "test"
MailMsg.Body = "test"
MailMsg.IsBodyHtml = True
Dim SmtpMail As New Net.Mail.SmtpClient
Dim SMTPUserInfo As New Net.NetworkCredential()
SMTPUserInfo.UserName = "MYUSERNAME"
SMTPUserInfo.Password = "MYPASSWORD"
SmtpMail.UseDefaultCredentials = False
SmtpMail.Credentials = SMTPUserInfo
SmtpMail.Host = "smtp.gmail.com"
SmtpMail.Port = 587
SmtpMail.EnableSsl = True
SmtpMail.Send(MailMsg)

1 个答案:

答案 0 :(得分:0)

    'Start by creating a mail message object
    Dim MyMailMessage As New MailMessage()

    'From requires an instance of the MailAddress type
    MyMailMessage.From = New MailAddress("USERNAME@gmail.com")

    'To is a collection of  types
    MyMailMessage.To.Add("DESTINATION@EXAMPLE.COM")

    MyMailMessage.Subject = "XXXXXXX" & x
    MyMailMessage.Body = "XXXXXXXXXXXXXXXXXXX "

    Dim attach As New Attachment(fileName)

    MyMailMessage.Attachments.Add(attach)

    'Create the SMTPClient object and specify the SMTP GMail server
    Dim SMTPServer As New SmtpClient("smtp.gmail.com")
    SMTPServer.Port = 587
    SMTPServer.Credentials = New System.Net.NetworkCredential("USERNAME@gmail.com", "PASSWD")
    SMTPServer.EnableSsl = True
    SMTPServer.Send(MyMailMessage)

    SMTPServer.Dispose()

    MyMailMessage.Dispose()