Reputation: 89
I've been given a task to prototype a web form that will send an email when the user submits the form. From Googling, so far I've come up with the following code-behind (I'm using C# ASP.NET and I have been told to use ado.net although I'm not sure where this part comes in):
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("[email protected]"));
smtpClient.Send(mail);
}
protected void Button2_Click(object sender, EventArgs e)
{
}
protected void TextBox3_TextChanged(object sender, EventArgs e)
{
}
}
I am getting the following error:
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("[email protected]"));
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
I can ping both the IP address and the mail server. I also tried using "smtp.udel.edu" as the smtp client name and port 587 with the same result.
Upvotes: 0
Views: 602
Reputation: 89
It turns out the problem was 2-factor identification on the smtp server. Creating a dummy gmail account without 2FA solves the problem.
Upvotes: 0
Reputation: 722
Explicitly pass in the credentials of the account from where you are you sending the email. Put this:
smtpClient.Credentials = new NetworkCredential("[email protected]", "password");
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
Upvotes: 1