使用VB.net 2010向Gmail帐户发送电子邮件

时间:2015-08-14 16:30:04

标签: sendmail vb.net-2010

我在尝试让我的程序用户向我的Gmail帐户发送电子邮件时遇到问题,这是我的设置:

enter image description here

这是我的代码:

Imports System.Net.Mail

Public Class Form1
Private Sub TextBox2_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txtBody.TextChanged
    lblCharCount.Text = String.Format("Letter Count: {0}", txtBody.Text.Length)
End Sub

Private Sub btnSend_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSend.Click
    If YourEmailTextBox.Text <> "" AndAlso txtBody.Text <> "" Then
        Dim message As New MailMessage
        Dim smtp As New SmtpClient
        message.To.Add(YourEmailTextBox.Text.Trim())
        message.From = New MailAddress(YourEmailTextBox.Text, "heading")
        message.Subject = txtSubject.Text
        message.Body = txtBody.Text
        smtp.Host = "smtp.gmail.com"
        smtp.Port = "587"
        smtp.EnableSsl = True
        smtp.Credentials = New Net.NetworkCredential("MyGmailAccount@gmail.com", "MyPassword")
        Try
            smtp.Send(message)
            MsgBox("your message has been sent")

        Catch ex As Exception
            MsgBox(ex.Message)
            Exit Sub
        End Try
    Else
        MsgBox("Please enter a message into the body and the email address of the recipient", MsgBoxStyle.Information)
    End If
End Sub
End Class

这就是问题所在:

enter image description here

一些帮助请...