最近,我们的组织从内部电子邮件系统迁移到Microsoft Office 365.我有几个用vb.net编写的应用程序,它们在表单完成后发送电子邮件。 使用我们的内部服务器而不是Office365时,电子邮件可以正常工作。 我尝试过端口587,465甚至25。 我们的中央IT部门告诉我,我需要在代码中指定TLS。 但他们根本不用.NET编写代码。
以下是我的代码。在下面就是错误。
Public Sub send_exchange()
Dim smtp = New SmtpClient("smtp.office365.com")
With smtp
.DeliveryMethod = SmtpDeliveryMethod.Network
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
.Port = Convert.ToInt32("587") - have also tried without the TOInt32
.UseDefaultCredentials = False
.EnableSsl = True
.Credentials = New NetworkCredential("<userID>@uwo.ca","<password>")
.Timeout = 300000
End With
Dim myMail As New MailMessage
With myMail
.From = New MailAddress("webedu1@uwo.ca", "Western Education - Client Support")
.To.Add(New MailAddress("jbentley@uwo.ca", "Julian Bentley"))
.Subject = "Subject of email"
.Body = "THis is the body"
End With
'Response.Write("<br>Response.End at line 397 is active")
'Response.End
Try
smtp.Send(myMail)
Catch ex As Exception
Response.Write("<br><font color=red>ERROR<br />" & ex.toString & "</font>")
End Try
End Sub
ERROR System.Net.Mail.SmtpException:发送邮件失败。 ---&GT; System.Net.WebException:无法连接到远程服务器---&gt; System.Net.Sockets.SocketException:连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机无法在System.Net.Sockets.Socket响应40.97.92.242:587 System.Net.ServicePoint.ConnectSocketInternal上的.DoConnect(EndPoint endPointSnapshot,SocketAddress socketAddress)(布尔值connectFailure,套接字s4,套接字s6,套接字&套接字,IPAddress&amp;地址,ConnectSocketState状态,IAsyncResult asyncResult,异常和异常)---结束内部异常堆栈跟踪---在System.Net.PooledStream.Activate(Object owningObject,Boolean)的System.Net.ServicePoint.GetConnection(PooledStream PooledStream,Object owner,Boolean async,IPAddress&amp; address,Socket&amp; abortSocket,Socket&amp; abortSocket6)中Sys上的System.Net.ConnectionPool.GetConnection(Object owningObject,GeneralAsyncDelegate asyncCallback,Int32 creationTimeout)中的async,GeneralAsyncDelegate asyncCallback)位于System.Net.Mail.SmtpClient.Send的System.Net.Mail.SmtpClient.GetConnection()的System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)的tem.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)( MailMessage消息)---内部异常堆栈跟踪的结束---在I:\ Dev \ www.edu.uwo.ca \ intranet中intranet_intranetSub.send_exchange()的System.Net.Mail.SmtpClient.Send(MailMessage消息)处\ intranetSub.master.vb:第397行