我创建了联系表格。我只需要从表单中获取用户数据并将其发送到我自己的电子邮件地址即可。我将我的Outlook登录名放在了vb代码中,但是我仍然收到发送电子邮件错误的失败消息。
我已经仔细检查了我的Outlook凭据,并且我也尝试了使用gmail帐户的脚本。我的脚本不好吗?还是这是防火墙问题?
Imports System.Net.Mail
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim Smtp_Server As New SmtpClient
Dim e_mail As New MailMessage()
Smtp_Server.Host = "smtp.outlook.com"
Smtp_Server.Port = 587
Smtp_Server.Credentials = New Net.NetworkCredential("myemail@outlook.com, "mypassword")
Smtp_Server.EnableSsl = True
Smtp_Server.UseDefaultCredentials = False
Smtp_Server.DeliveryMethod = SmtpDeliveryMethod.Network
e_mail = New MailMessage()
e_mail.From = New MailAddress(txtFrom.Text)
e_mail.To.Add("myemail@outlook.com")
e_mail.Subject = "Email Sending"
e_mail.IsBodyHtml = False
e_mail.Body = txtMessage.Text
Smtp_Server.Send(e_mail)
MsgBox("Mail Sent")
Catch error_t As Exception
MsgBox(error_t.ToString)
End Try
End Sub
End Class
无论使用Outlook还是gmail,我都会收到以下错误消息。我的代码看起来不错,我不明白为什么不这样做。
System. Net. Mail.SmtpException: Failure sending mail.
System.Net.WebException: Unable to connect to the remote server
System.Net.Sockets.SocketException: No connection could be made
because the target machine actively refused it 52.96.O.21D:587
at System. Net. Sockets. Socket. DoConnect(EndPoint endpointsnapshot,
SocketAddress socketAddress)
at System. Net. Servicepoint. Connectsocketlnternal(aoolean
connectFaiIure, Socket 54, Socket 56, Socket& socket, IPAddress&
address, ConnectSocketState state, IAsyncResuIt asyncResuIt,
Exception& exception)
- End of inner exception stack trace -
at System. Net. ServicePoint.GetConnection(PooIedStream
PooledStream, Object owner, Boolean async, IPAddress& address,
Socket& abortSocket, Socket& abortSocketE)
at System.Net.PooIedStream.Actlvate(Object owningObject, Boolean
async, GeneralAsyncDeIegate asyncCaIIbackl
at System.Net.PooIedStream.Actlvate(ObJect owningObject,
GeneralAsyncDeIegate asyncCaIIbackl
at System. Net. Connectionpool. GetConnection(Object owningObJect,
GeneralAsyncDeIegate asyncCaIIback, Int32 creationTimeout)
at System. Net. Mail.SmtpConnection. GetConnection(ServicePoint
servicepoint)
at System. Net. Mail.SmtpTranspoft.GetConnection(ServicePoint
servicepoint)
at System. Net. Mail.SmtpCIient. GetConnectionO
at System. Net. Mail. SmtpCIient. Send(MaiIMessage message)
- End of inner exception stack trace
at System. Net. Mail. SmtpCIient. Send(MaiIMessage message)
at PeruPetrovI .WebFormI.auttonI Click(Object sender, EventArgs e)