【问题标题】:Trying to send email from a web form尝试从 Web 表单发送电子邮件
【发布时间】:2017-06-09 23:10:47
【问题描述】:

我的任务是制作一个 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,结果相同。

【问题讨论】:

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


    【解决方案1】:

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

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

    smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;

    【讨论】:

    • "UseDefaultCredentials = true" 将使凭据分配无用。 “要使用您的默认网络凭据,您可以将 UseDefaultCredentials 设置为 true 而不是设置此属性。如果 UseDefaultCredentials 属性设置为 false,则在连接到服务器时,将使用 Credentials 属性中设置的值作为凭据。 " -- 来源msdn.microsoft.com/en-us/library/…
    • @JanErikGunnar 我已根据您的评论更新了答案。
    • 我注释掉了这一行:“//smtpClient.UseDefaultCredentials = true;”并得到同样的错误。我明确设置了用户名和密码 - 我只是使用 ***** 因为我不想在 Stackoverflow 上发布我的用户名和密码。
    • @JamesAlesi 看看这个:stackoverflow.com/questions/44302243/…
    【解决方案2】:

    原来问题是 smtp 服务器上的 2 因素识别。创建一个没有 2FA 的虚拟 gmail 帐户可以解决问题。

    【讨论】:

      猜你喜欢
      • 2011-11-17
      • 1970-01-01
      • 2012-03-15
      • 1970-01-01
      • 1970-01-01
      • 2017-03-31
      • 2016-03-11
      • 1970-01-01
      相关资源
      最近更新 更多