Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8091213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:59:37+00:00 2026-06-05T19:59:37+00:00

I am trying to send email using gmail from my mvc application. I am

  • 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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T19:59:38+00:00Added an answer on June 5, 2026 at 7:59 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to send email through gmail using PHPMailer_V5.1. Getting the following error,
Well I'm trying to send a HTML email using gmail smtp from CI, and
I am trying to send an email via GMail from ASP.Net using the code
I am trying to send email using Exchange 2007 from a console app using
I am trying to send email using c# following is my code. try {
Hey guys, I am trying to send email using my gmail account, and I'm
I'm trying to send e-mails from my localhost, using my gmail account, I've looked
I am trying to send an smtp email through gmail's smtp server using the
I am trying to use an intent to send an email from my application
I'm trying to send an email from my application with a PHP contact form,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.