繁体   English   中英

尝试从网络表单发送电子邮件

[英]Trying to send email from a web form

我已经完成了一个网络表单原型的任务,该表单将在用户提交表单时发送电子邮件。 到目前为止,从Googling,我已经提出了以下代码(我正在使用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,结果相同。

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

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

smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM