0

I am trying to send email using gmail from my mvc application. I am getting the following exception

Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.

here is the code for sending email

string tn = "EmailVerification.htm";
string fileName = Path.GetFileName(tn);
string templatePath = Path.Combine(Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/EmailTemplates")), fileName);
  System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();

            mailMessage.From = new MailAddress(EmailFrom, "Email");
            mailMessage.To.Add(new MailAddress(To));
            mailMessage.Subject = "subject";
            mailMessage.IsBodyHtml = true;
            MailDefinition mailDef = new MailDefinition();
            mailDef.BodyFileName = templatePath;
            mailDef.IsBodyHtml = true;
            mailDef.Subject = "mailDefsubject";
            mailDef.From = mailMessage.From.ToString();

            ListDictionary replacements = new ListDictionary();

            replacements.Add("<%FIRSTNAME%>", FirstName);
            replacements.Add("<%USERNAME%>", "asd");
            replacements.Add("<%VERIFICATIONLINK%>", VerificationLink);     
            replacements.Add("<%WEBSITE%>", "http://www.mysite.com");

            MailMessage msgHtml = mailDef.CreateMailMessage(To, replacements, new System.Web.UI.Control());
            AlternateView htmlView = AlternateView.CreateAlternateViewFromString(msgHtml.Body, null, "text/html");

            AddLinkedResources(templatePath, ref htmlView);

            mailMessage.AlternateViews.Add(htmlView);

            SmtpClient smtpClient = new SmtpClient();
            Object userState = mailMessage;
            smtpClient.Credentials = new System.Net.NetworkCredential("myaddress@gmail.com", "1234567");
            smtpClient.EnableSsl = true;
            smtpClient.Host = "smtp.gmail.com";
            smtpClient.Port = 587;

            //Attach event handler for async callback
            smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
            try
            {               

                smtpClient.Send(mailMessage);
                EmailStatus = true;
            }
            catch (SmtpException smtpEx)
            {
                EmailStatus = false;

            }
            catch (Exception ex)
            {
                //HttpContext.Current.Response.Write(ex.InnerException);
                EmailStatus = false;

            }

in the web.config i have

<system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="noreply@asd.com">
        <network host="smtp.gmail.com" port="587"
                                       userName="asd@gmail.com"
                                       password="asd" />
      </smtp>
    </mailSettings>
  </system.net>
John x
  • 4,031
  • 8
  • 42
  • 67

1 Answers1

2

Try setting:

smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential("myaddress@gmail.com", "1234567");

Also you seem to be subscribing to the SendCompleted event but this event is intended to be used when sending an email asynchronously (SendAsync method), so you could get rid of it. The Send method that you are using is blocking.

You may also checkout the following answer.

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • tnx for providing the additional info i was indeed sending the emails asynchronously that line of code got in the way of copy pasting...however i am still getting the exception i have tried turning off the firewall. i have some setting in the config file plz have a look at the updated question – John x Jun 16 '12 at 20:55
  • 1
    Try simplifying: http://stackoverflow.com/questions/6974112/send-mail-by-smtp-gmail-sever/6974160#6974160 Does this work? Also please note that there are issues when using the async version in ASP.NET: http://stackoverflow.com/q/6935427/29407 – Darin Dimitrov Jun 16 '12 at 21:06
  • i have used the simplified version but still the same (inner)exception... i have used this code in my other applications successfully dont know why it's why its troubling... i am using visual studio 2010, windows 7, if that helps – John x Jun 16 '12 at 21:06
  • The code (simplified version) works perfectly fine for me. ASP.NET MVC 3 -> Windows 7. – Darin Dimitrov Jun 16 '12 at 21:07
  • i will try re-starting my machine then hopefully it will go away `:)` tnx for your time anyway – John x Jun 16 '12 at 21:09