I keep getting this error when the send message is called. Any suggestion!
Server Error in '/Site' Application.
--------------------------------------------------------------------------------
An existing connection was forcibly closed by the remote host
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: An existing connection was forcibly closed by the remote host
Source Error:
Line 55:
Line 56: SmtpClient mySmtpClient = new SmtpClient();
Line 57: mySmtpClient.Send(myMessage);
Line 58:
Line 59: Message.Visible = true;
Source File: c:\Maintenance Request System\Site\Controls\ContactForm.ascx.cs Line: 57
Stack Trace:
[SocketException (0x2746): An existing connection was forcibly closed by the remote host]
System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) +6132200
System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) +134
[IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.]
System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) +300
System.Net.DelegatedStream.Read(Byte[] buffer, Int32 offset, Int32 count) +45
System.Net.BufferedReadStream.Read(Byte[] buffer, Int32 offset, Int32 count) +105
System.Net.Mail.SmtpReplyReaderFactory.ReadLines(S mtpReplyReader caller, Boolean oneLine) +248
System.Net.Mail.SmtpReplyReaderFactory.ReadLine(Sm tpReplyReader caller) +16
System.Net.Mail.SmtpConnection.GetConnection(Servi cePoint servicePoint) +665
System.Net.Mail.SmtpTransport.GetConnection(Servic ePoint servicePoint) +170
System.Net.Mail.SmtpClient.GetConnection() +50
System.Net.Mail.SmtpClient.Send(MailMessage message) +1484
[SmtpException: Failure sending mail.]
System.Net.Mail.SmtpClient.Send(MailMessage message) +1811
Controls_ContactForm.SendButton_Click(Object sender, EventArgs e) in c:\Maintenance Request System\Site\Controls\ContactForm.ascx.cs:57
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
System.Web.UI.WebControls.Button.RaisePostBackEven t(String eventArgument) +112
System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563
web.config
Code:
configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="my name<[email protected]>">
<network host="1123smtp.123.com" />
</smtp>
</mailSettings>
</system.net>
</configuration>
contactform.ascx
Code:
protected void SendButton_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
string fileName = Server.MapPath("~/App_Data/ContactForm.txt");
string mailBody = File.ReadAllText(fileName);
mailBody = mailBody.Replace("##UserID##", UserID.Text);
mailBody = mailBody.Replace("##FirstName##", FirstName.Text);
mailBody = mailBody.Replace("##LastName##", LastName.Text);
mailBody = mailBody.Replace("##RoomNum##", RoomNum.Text);
mailBody = mailBody.Replace("##HomePhone##", PhoneNum.Text);
mailBody = mailBody.Replace("##Location##", Location.Text);
mailBody = mailBody.Replace("##Notify##", Notify.Text);
mailBody = mailBody.Replace("##Comments##", Comments.Text);
MailMessage myMessage = new MailMessage();
myMessage.Subject = "Response from web site";
myMessage.Body = mailBody;
myMessage.From = new MailAddress("[email protected]", "Sender Name");
myMessage.To.Add(new MailAddress("[email protected]", "Receiver Name"));
SmtpClient mySmtpClient = new SmtpClient();
mySmtpClient.Send(myMessage);
Message.Visible = true;
FormTable.Visible = false;
}
}
}