尝试从Web表单发送电子邮件

时间:2017-06-09 23:10:48

标签: c# asp.net email ado.net smtp

我已经完成了一项任务,即在用户提交表单时发送电子邮件的Web表单原型。从谷歌搜索到目前为止,我已经提出了以下代码隐藏(我使用的是C#ASP.NET,我被告知要使用ado.net,虽然我不确定这部分的来源):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

public partial class WebPageSeparated : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        SmtpClient smtpClient = new SmtpClient("mail.udel.edu", 25);

        smtpClient.Credentials = new        System.Net.NetworkCredential("******@udel.edu", "**********");
        smtpClient.UseDefaultCredentials = true;
        smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
        smtpClient.EnableSsl = true;
        MailMessage mail = new MailMessage();

        //Setting From , To and CC
        mail.From = new MailAddress("******@udel.edu", "The Ether");
        mail.To.Add(new MailAddress("******@udel.edu"));
        mail.Subject = "Form Submitted";
        mail.Body = "User ID " + TextBox3.Text + " submitted a form.";
        //mail.CC.Add(new MailAddress("MyEmailID@gmail.com"));

        smtpClient.Send(mail);
    }

    protected void Button2_Click(object sender, EventArgs e)
    {

    }

    protected void TextBox3_TextChanged(object sender, EventArgs e)
    {

    }
}

我收到以下错误:

Server Error in '/' Application.

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 128.175.68.17:25

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 128.175.68.17:25

Source Error: 


Line 31:         //mail.CC.Add(new MailAddress("MyEmailID@gmail.com"));
Line 32: 
Line 33:         smtpClient.Send(mail);
Line 34:     }
Line 35: 

Source File: C:\Users\jfalesi\Documents\Jamie_Local\Projects\Data Management\FirstWebSite\WebPageSeparated.aspx.cs    Line: 33 

Stack Trace: 


[SocketException (0x274c): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 128.175.68.17:25]
   System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) +185
   System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception) +506

[WebException: Unable to connect to the remote server]
   System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6) +6642460
   System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback) +302
   System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) +23
   System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) +328
   System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) +141
   System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) +222
   System.Net.Mail.SmtpClient.GetConnection() +45
   System.Net.Mail.SmtpClient.Send(MailMessage message) +1557

[SmtpException: Failure sending mail.]
   System.Net.Mail.SmtpClient.Send(MailMessage message) +1905
   WebPageSeparated.Button1_Click(Object sender, EventArgs e) in C:\Users\jfalesi\Documents\Jamie_Local\Projects\Data Management\FirstWebSite\WebPageSeparated.aspx.cs:33
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9712662
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +204
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1639

我可以ping IP地址和邮件服务器。我也尝试使用“smtp.udel.edu”作为smtp客户端名称和端口587,结果相同。

2 个答案:

答案 0 :(得分:1)

明确传递您发送电子邮件的帐户的凭据。把这个:

<强> smtpClient.Credentials = new NetworkCredential("user@domain.co.za", "password");

smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;

答案 1 :(得分:0)

事实证明问题是smtp服务器上的双因素识别。创建一个没有2FA的虚拟Gmail帐户可以解决问题。