azharkhan
azharkhan

Reputation: 365

sending email. Asp.net. C#. Existing connection was forcibly closed by the remote host

Code Behind:

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;
using System.Net;

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

}
protected void Button1_Click(object sender, EventArgs e)
{
    MailMessage msg = new MailMessage();
    msg.From = new MailAddress(txtfrom.Text, "OLMS");
    msg.To.Add(new MailAddress(txtto.Text));
    msg.Subject = txtsub.Text;
    msg.Body = txtbody.Text;
    msg.IsBodyHtml = true;

    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.Credentials = new NetworkCredential(txtfrom.Text, txtto.Text);

    smtp.EnableSsl = true;

    smtp.Send(msg);

    lblresult.Text = "Message Sent Successful!";

}
}

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) +6416371
System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) +130

[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) +296
System.Net.DelegatedStream.Read(Byte[] buffer, Int32 offset, Int32 
count) +45
System.Net.BufferedReadStream.Read(Byte[] buffer, Int32 offset, 
Int32 count) +106
System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader 
caller, Boolean oneLine) +203
System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader 
caller) +16
System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String& 
response) +54
System.Net.Mail.StartTlsCommand.Send(SmtpConnection conn) +28
System.Net.Mail.SmtpConnection.GetConnection(ServicePoint 
servicePoint) +843
System.Net.Mail.SmtpTransport.GetConnection(ServicePoint 
servicePoint) +170
System.Net.Mail.SmtpClient.GetConnection() +44
System.Net.Mail.SmtpClient.Send(MailMessage message) +1554
[SmtpException: Failure sending mail.]
System.Net.Mail.SmtpClient.Send(MailMessage message) +1906
_Default.Button1_Click(Object sender, EventArgs e) in c:\Users
\Azhar Khan\Documents\Visual Studio 2010\WebSites
\WebSite2\Default.aspx.cs:31
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9553594
System.Web.UI.WebControls.Button.RaisePostBackEvent(String 
eventArgument) +103
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.R
aisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler 
sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean 
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724

I have not configured IIS for SMTP ( I don't know whether to do this). I also have not added any smtp configuration into Web.config file in this wesite. Any help regarding the exception I am getting that prevents me from sending email. Thanks

Upvotes: 0

Views: 3566

Answers (2)

PeterK
PeterK

Reputation: 3817

Make sure to enable the Allow less secure apps option in Google account setting at https://google.com/settings/security/lesssecureapps -- from the second half of 2014, this is required to authenticate with Gmail SMTP using basic authentication.

Alternatively, you can keep the above option disabled and implement XOAUTH2 for SMTP authentication (see https://developers.google.com/gmail/xoauth2_protocol).

Upvotes: 1

Priyank Sheth
Priyank Sheth

Reputation: 2362

It seems from your code that you'r enabling SSL by smtp.EnableSsl = true;. If you want to do it by same way, use 465 port. Below is the resolved answer link for relative problem:

How can I send emails through SSL SMTP with the .NET Framework?

If you find any difficulties, please do let me know.

Upvotes: 0

Related Questions