如何使用VB.NET从Outlook帐户发送电子邮件?

How do I use VB.NET to send an email from an Outlook account?

我正在尝试通过vb.net程序通过Outlook电子邮件帐户发送电子邮件。 运行代码时,我收到一条错误消息,提示我没有安全连接。 我一直在网上搜索,并尝试了所有发现的调整,但是仍然没有运气。

这是我目前的代码(更改了特定的电子邮件地址和密码):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Private Sub LEmail_Click(sender As Object, e As EventArgs) Handles LEmail.Click
    Try
        Dim SmtpServer As New SmtpClient("smtp.outlook.com")
        Dim mail As New MailMessage()
        SmtpServer.Credentials = New Net.NetworkCredential("[email protected]","Password")
        SmtpServer.Port = 587
        SmtpServer.EnableSsl = True
        mail = New MailMessage()
        mail.From = New MailAddress("[email protected]")
        mail.To.Add("[email protected]")
        mail.Subject ="Test Mail"
        mail.Body ="This is for testing SMTP mail from OUTLOOK"
        SmtpServer.UseDefaultCredentials = False
        SmtpServer.Send(mail)
        MessageBox.Show("mail sent")
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

任何建议和帮助将不胜感激。 谢谢 :)

编辑:我也尝试过SmtpServer.Port = 25

编辑2:错误消息显示:

System.Net.Mail.SmtpExeption: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM as System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.MailCommand.Sent(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at WindowsApplication.Form1.LEmail_Click(Object sender, EventArgs e) in C:\\Users\\Monica\\Documents\\Current\\Visual Studio\\WindowsApplication\\Form1.vb:line 553

编辑3:第二个错误:

System.Net.Mail.SmtpException: Mailbox unavailable. The server response was: 5.3.4 Requested action not taken; We noticed some unusual activity in your Hotmail account. To help protect you, we've temporarily blocked your account. at System.Net.Mail.DataStopCommand.CheckResponse(SmtpStatusCode statusCode, String serverResponse) at System.Net.Mail.DataStopCommand.Send(SmtpConnection conn) at System.Net.Mail.SmtpConnection.OnClose(Object sender, EventArgs args) at System.Net.CloseableStream.Close() at System.Net.MailWriter.Close() at System.Net.Mail.SmtpClient.Send(MailMessage message) at WindowsApplication.Form1.LEmail_Click(Object sender, EventArgs e) in C:\\User\\Monica\\Documents\\Current\\Visual Studio\\WindowsApplication\\Form1.vb:line 553


将smtpserver从smtp.outlook.com更改为smtp-mail.outlook.com

web.config设置

1
2
3
4
5
<mailSettings>
  <smtp deliveryMethod="Network" from="[email protected]">
    <network host="smtp-mail.outlook.com" userName="[email protected]" password="passwordhere" port="587" enableSsl="true"/>
  </smtp>
</mailSettings>


添加:

1
SmtpServer.UseDefaultCredentials = false

在此行之前:

1
SmtpServer.Credentials = New Net.NetworkCredential("[email protected]","Password")

[设计此API的人显然没有像消费者那样思考!]