vb.net 在 vb 2010 中发送邮件失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13434738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:11:35  来源:igfitidea点击:

Failure sending mail in vb 2010

vb.netemailsmtpsmtpclient

提问by KevinWolf228

Trying to get a very basic program to send me an email. I have 3 text boxes.

试图获得一个非常基本的程序来向我发送电子邮件。我有 3 个文本框。

TextBox1 is a ReadOnly TextBox with my email in it TextBox2 is for "Your Email" TextBox3 is for your message

TextBox1 是一个只读文本框,其中包含我的电子邮件 TextBox2 用于“您的电子邮件” TextBox3 用于您的消息

And there is a Button (Button1) That says "Send"

还有一个按钮(Button1),上面写着“发送”

Here is the code for my entire project. I removed my email and password in this question for security purposes.

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

When I run the program and type in "[email protected]" in TextBox2 and "TestEmail1234" into TextBox3 and press send, it takes me back to the coding page and says "SmtpException was unhandled. Failure sending mail"

当我运行程序并在 TextBox2 中键入“[email protected]”和在 TextBox3 中键入“TestEmail1234”并按发送时,它会将我带回编码页面并显示“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("[email protected]", "mypassword")
        'using Gmail
        smtpServer.Port = 587
        smtpServer.Host = "smtp.gmail.com"
        smtpServer.EnableSsl = True

        mail = New MailMessage()
        mail.From = New MailAddress("my [email protected]", TextBox1.Text)
        mail.To.Add(TextBox1.Text)
        mail.Subject = TextBox2.Text
        mail.Body = TextBox3.Text

        smtpServer.Send(mail)

    End Sub
End Class

回答by user1797443

There are a few problems, I'll just give you a working script

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

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("[email protected]")
    Mail.From = New MailAddress("[email protected]")
    Mail.Body = "This is an email!"
    Dim SMTP As New SmtpClient("smtp.gmail.com")
    SMTP.EnableSsl = True
    SMTP.Credentials = New System.Net.NetworkCredential("[email protected]", "password")
    SMTP.Port = 587
    SMTP.Send(Mail)
    MsgBox("Sent Successfully")

End Sub
End Class