简体   繁体   English

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

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

I'm trying to send an email via an outlook email account through a vb.net program. 我正在尝试通过vb.net程序通过Outlook电子邮件帐户发送电子邮件。 When I run the code I get an error telling me that I don't have a secure connection. 运行代码时,我收到一条错误消息,提示我没有安全连接。 I've been searching online and have tried all the adjustments I've found but I'm still not having any luck. 我一直在网上搜索,并尝试了所有发现的调整,但是仍然没有运气。

Here is my code at the moment( specific email addresses and passwords are changed ): 这是我目前的代码( 更改了特定的电子邮件地址和密码 ):

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("sample@outlook.com", "Password")
        SmtpServer.Port = 587
        SmtpServer.EnableSsl = True
        mail = New MailMessage()
        mail.From = New MailAddress("sample@outlook.com")
        mail.To.Add("recipient@gmail.com")
        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

Any suggestions and help would be greatly appreciated. 任何建议和帮助将不胜感激。 Thanks :) 谢谢 :)

EDIT: I've also tried SmtpServer.Port = 25 编辑:我也尝试过SmtpServer.Port = 25

EDIT 2: Error message reads: 编辑2:错误消息显示:

System.Net.Mail.SmtpExeption: The SMTP server requires a secure connection or the client was not authenticated. System.Net.Mail.SmtpExeption:SMTP服务器需要安全连接,或者客户端未通过身份验证。 The server response was: 5.7.57 SMTP; 服务器响应为: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 客户端未通过身份验证以在System.Net.Mail.MailCommand.Sent(SmtpConnection conn,Byte []命令,MailAddress来自,在System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode,字符串响应)处发送电子邮件, Windows.Application.Form1.LEmail_Click(System.Net.Mail.SmtpClient.Send(MailMessage消息)上的System.Net.Mail.SmtpTransport.SendMail(MailAddress发送者,MailAddressCollection收件人,字符串deliveryNotify,布尔值allowUnicode,SmtpFailedRecipientException&异常) C:\\ Users \\ Monica \\ Documents \\ Current \\ Visual Studio \\ WindowsApplication \\ Form1.vb中的对象发送者EventArgs e):行553

EDIT 3: Second Error: 编辑3:第二个错误:

System.Net.Mail.SmtpException: Mailbox unavailable. System.Net.Mail.SmtpException:邮箱不可用。 The server response was: 5.3.4 Requested action not taken; 服务器响应为:5.3.4未执行请求的操作; We noticed some unusual activity in your Hotmail account. 我们注意到您的Hotmail帐户中发生了一些异常活动。 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 在系统上System.Net.Mail.DataStopCommand.Send(SmtpConnection conn)在System.Net.Mail.SmtpConnection.OnClose(Object sender,EventArgs args)在System.Net.Mail.DataStopCommand.CheckResponse(SmtpStatusCode statusCode,String serverResponse) C:\\ User中WindowsApplication.Form1.LEmail_Click(Object sender,EventArgs e)的System.Net.Mail.SmtpClient.Send(MailMessage消息)的System.Net.MailWriter.Close()的.Net.CloseableStream.Close() \\ Monica \\ Documents \\ Current \\ Visual Studio \\ WindowsApplication \\ Form1.vb:第553行

Add this: 添加:

SmtpServer.UseDefaultCredentials = false 

before this line: 在此行之前:

SmtpServer.Credentials = New Net.NetworkCredential("sample@outlook.com", "Password")

[Whoever designed this API obviously wasn't thinking like a consumer!] [设计此API的人显然没有像消费者那样思考!]

change the smtpserver from smtp.outlook.com to smtp-mail.outlook.com 将smtpserver从smtp.outlook.com更改为smtp-mail.outlook.com

web.config settings web.config设置

<mailSettings>
  <smtp deliveryMethod="Network" from="yourname@outlook.com">
    <network host="smtp-mail.outlook.com" userName="yourname@outlook.com" password="passwordhere" port="587" enableSsl="true"/>
  </smtp>
</mailSettings> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM