在vb 2010中发送邮件失败

时间:2012-11-17 21:01:56

标签: vb.net email smtp smtpclient

尝试获取一个非常基本的程序给我发电子邮件。我有3个文本框。

TextBox1是一个ReadOnly TextBox,其中包含我的电子邮件 TextBox2用于“您的电子邮件” TextBox3用于您的消息

还有一个按钮(Button1)表示“发送”

这是我整个项目的代码。出于安全考虑,我在此问题中删除了我的电子邮件和密码。

当我运行程序并在TextBox2中键入“Test1234@live.com”并在TextBox3中输入“TestEmail1234”并按下send时,它会将我带回编码页面并说 “SmtpException未得到处理。 发送邮件失败“

Imports System.Net.Mail

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim smtpServer As New SmtpClient()
        Dim mail As New MailMessage()

        smtpServer.Credentials = New Net.NetworkCredential("myemail@gmail.com", "mypassword")
        'using Gmail
        smtpServer.Port = 587
        smtpServer.Host = "smtp.gmail.com"
        smtpServer.EnableSsl = True

        mail = New MailMessage()
        mail.From = New MailAddress("my email@gmail.com", TextBox1.Text)
        mail.To.Add(TextBox1.Text)
        mail.Subject = TextBox2.Text
        mail.Body = TextBox3.Text

        smtpServer.Send(mail)

    End Sub
End Class

1 个答案:

答案 0 :(得分:1)

有一些问题,我只会给你一个工作脚本

Imports System.Net.Mail
Public Class Form1
Dim message As New MailMessage
Dim smtp As New SmtpClient
Dim instance As IDisposable


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim Mail As New MailMessage
    Mail.Subject = "test email"
    Mail.To.Add("recieveradress@gmail.com")
    Mail.From = New MailAddress("senderadress@gmail.com")
    Mail.Body = "This is an email!"
    Dim SMTP As New SmtpClient("smtp.gmail.com")
    SMTP.EnableSsl = True
    SMTP.Credentials = New System.Net.NetworkCredential("senderaddress@gmail.com", "password")
    SMTP.Port = 587
    SMTP.Send(Mail)
    MsgBox("Sent Successfully")

End Sub
End Class