1.我试图写一个代码,使用VB.NET发送电子邮件。
- SMTP服务器是smtp.office365.com。
1.我找到了大量关于如何使用Gmail SMTP服务器实现这一点的资源。它工作正常。但要求是从运行在office365上的公司电子邮件发送电子邮件。
1.该项目是Windows窗体应用程序。我正在使用Visual Studio 2010。
1.我试过什么?
我试过这个代码:
Dim Mail As New MailMessage
Dim SMTP As New SmtpClient("smtp.office365.com")
Mail.Subject = "Test Subject"
Mail.From = New MailAddress("The from Email")
SMTP.Credentials = New System.Net.NetworkCredential("The from Email", "The from Email Password")
Mail.To.Add("The to Email")
Mail.Body = "Hello"
SMTP.EnableSsl = True
SMTP.Port = "587"
ServicePointManager.ServerCertificateValidationCallback = New System.Net.Security.RemoteCertificateValidationCallback(AddressOf customCertValidation)
Try
SMTP.Send(Mail)
MsgBox("done")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
在同样的形式中,我有一个函数定义如下:
Private Shared Function customCertValidation(ByVal sender As Object, _
ByVal cert As X509Certificate, _
ByVal chain As X509Chain, _
ByVal errors As SslPolicyErrors) As Boolean
Return True
End Function
通过运行上面的代码,我得到了这个消息(大约一分钟后):System.Net.Mail. SMTP异常:操作已超时。at System.Net.Mail.SmtpClient.Send(MailMessage消息)
1.另外,Here,他们提到了类似的问题。我尝试使用他们的代码,如下所示:
Imports System.Net.Mail
Public Class Form1
Private Sub SendEmail()
Dim MS365Email As New MailMessage
MS365Email.To.Add("RECIPIENTEMAIL")
MS365Email.From = New MailAddress("SENDEREMAIL")
MS365Email.Subject = "SUBJECT"
MS365Email.IsBodyHtml = True
MS365Email.Body = "BODYTEXT"
Dim MS365client As New SmtpClient("smtp.office365.com", 587)
MS365Email.Priority = MailPriority.Normal
MS365client.EnableSsl = True
MS365client.UseDefaultCredentials = False
Dim xms365 As New Net.NetworkCredential("USERNAME", "PASSWORD")
MS365client.Credentials = xms365
MS365client.DeliveryMethod = SmtpDeliveryMethod.Network
MS365client.SendAsync(MS365Email, Nothing)
End Sub
End Class
它不会抛出任何错误或异常。但没有收到电子邮件。
1.我在期待什么?要么指出修复这些代码的问题,要么分享另一个可以满足要求的代码。
先谢谢你了
1条答案
按热度按时间5vf7fwbs1#
尝试使用启用SSL/TLS,如以下代码所示:
您可能会发现类似的问题,请参阅Send SMTP email using System.Net.Mail via Exchange Online (Office 365)和Send SMTP email testing Microsoft Office 365 in .net了解更多信息。