Vb.net 无法通过Office 365服务器发送电子邮件?
最近,我们的组织从内部电子邮件系统迁移到Microsoft Office 365。我有几个用vb.net编写的应用程序,它们在填写表单后发送电子邮件。 电子邮件在使用我们的内部服务器而不是Office365时工作。 我尝试了端口587、465甚至25。 我们的中央IT部门告诉我,我需要在代码中指定TLS。 但它们根本不在.NET中编码 下面是我的代码。下面是错误 公共子发送交换()Vb.net 无法通过Office 365服务器发送电子邮件?,vb.net,email,office365,Vb.net,Email,Office365,最近,我们的组织从内部电子邮件系统迁移到Microsoft Office 365。我有几个用vb.net编写的应用程序,它们在填写表单后发送电子邮件。 电子邮件在使用我们的内部服务器而不是Office365时工作。 我尝试了端口587、465甚至25。 我们的中央IT部门告诉我,我需要在代码中指定TLS。 但它们根本不在.NET中编码 下面是我的代码。下面是错误 公共子发送交换() Dim smtp=新的SmtpClient(“smtp.office365.com”) 使用smtp .Deliv
Dim smtp=新的SmtpClient(“smtp.office365.com”)
使用smtp
.DeliveryMethod=SmtpDeliveryMethod.Network
ServicePointManager.SecurityProtocol=SecurityProtocolType.Tls12
.Port=Convert.ToInt32(“587”)-也尝试过不使用ToInt32
.UseDefaultCredentials=False
.EnableSsl=True
.Credentials=新的网络凭据(“@uwo.ca”,”)
.超时=300000
以
将myMail设置为新邮件消息
用我的邮件
.From=新邮件地址(“webedu1@uwo.ca“,“西方教育-客户支持”)
.To.Add(新邮件地址(“jbentley@uwo.ca(“朱利安·本特利”))
.Subject=“电子邮件的主题”
.Body=“这就是身体”
以
'Response.Write(“
第397行的Response.End处于活动状态”)
"答复:完
尝试
smtp.Send(myMail)
特例
Response.Write(“
错误
”和ex.toString&”)
结束尝试
端接头
错误
System.Net.Mail.SmtpException:发送邮件失败。-->System.Net.WebException:无法连接到远程服务器-->System.Net.Sockets.SocketException:连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机未能响应System.Net.Sockets.Socket.DoConnection上的40.97.92.242:587(EndPoint endPointSnapshot、SocketAddress SocketAddress SocketAddress)位于System.Net.ServicePoint.ConnectSocketInternal(布尔连接失败、套接字s4、套接字s6、套接字和套接字、IPAddress和地址、ConnectSocketState状态、IAsyncResult asyncResult、异常和异常)----内部异常堆栈跟踪的结束----at System.Net.ServicePoint.GetConnection(PooledStream PooledStream、对象所有者、布尔异步、IPAddress&address、Socket&abortSocket、Socket&abortSocket6)在System.Net.PooledStream.Activate(Object owningObject、Boolean async、GeneralAsyncDelegate asyncCallback)在System.Net.ConnectionPool.GetConnection(Object owningObject、GeneralAsyncDelegate asyncCallback、Int32 creationTimeout)在System.Net.Mail.SmtpConnection.GetConnection(ServicePoint ServicePoint)的System.Net.Mail.SmtpTransport.GetConnection(ServicePoint ServicePoint)的System.Net.Mail.SmtpClient.GetConnection()的System.Net.Mail.SmtpClient.Send(MailMessage消息)--内部异常堆栈跟踪的结束---在System.Net.Mail.SmtpClient.Send(MailMessage消息)在I:\Dev\www.edu.uwo.ca\intranet\intranetSub.master.vb中的intranet\u intranetSub.send\u exchange():第397行更新-我的问题已“解决”。我在我们的开发环境中工作,该环境位于私有IP上。因此,它在使用我们的邮件服务器时有效,但在使用外部邮件服务器时无效。一旦我将应用程序移动到一个面向外部的系统,它就可以正常工作。
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