0

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

hilbiazhar
  • 357
  • 3
  • 10
  • Does setting `Smtp.Port` to `587` help? I can't test it from here due to firewall rules. – PeterK Oct 08 '14 at 06:45
  • Thanks PeterK for a reply. No, setting the Port to 587 did not work. It throws an exception "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required". I have set a strong password by the way for the email I am using to send emails. – hilbiazhar Oct 08 '14 at 08:04
  • That's slightly better :) I don't see any errors in your code, however. How about Gmail settings? See http://stackoverflow.com/questions/17227532/gmail-530-5-5-1-authentication-required-learn-more-at – PeterK Oct 08 '14 at 08:23
  • PeterK my email is not pointing out any suspecious login attempt. Does this mean that the credentials with which i am logging in are incorrect keeping in view the above exception ...? – hilbiazhar Oct 08 '14 at 17:48
  • 1
    Every resource I found on this issue points toward enabling Less Secure Apps https://www.google.com/settings/security/lesssecureapps -- did you try this? – PeterK Oct 09 '14 at 07:18
  • PeterK you rock. Thanks this solved my problem. :) – hilbiazhar Oct 10 '14 at 19:11
  • Glad to be of help. I'll also post this as an answer for future reference. – PeterK Oct 12 '14 at 06:10

2 Answers2

1

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).

PeterK
  • 3,207
  • 1
  • 14
  • 24
0

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.

Community
  • 1
  • 1
Priyank Sheth
  • 2,167
  • 16
  • 30
  • The link you have mentioned I tried it but it did not work for me. By the above method in the given link it is throwing the exception "The message could not be sent to the SMTP server. The transport error code was 0x80040217. The server response was not available" – hilbiazhar Oct 08 '14 at 17:44