title: Remove and delete all cookies of my ASP NET c-sharp application
body: <p>I need remove and delete all cookies of my <strong>ASP NET c-sharp</strong> application after sent on message email.</p>
  
  <p>I have tried this solution without success because I have this error.</p>
  
  <pre><code>Server cannot modify cookies after HTTP headers have been sent.
  </code></pre>
  
  <p>In this line:</p>
  
  <pre><code>HttpContext.Current.Response.Cookies.Add(expiredCookie);
  </code></pre>
  
  <p>The message email started regularly.</p>
  
  <p>The google search does not help me.</p>
  
  <p>Anybody know how can I resolve do this?</p>
  
  <p>Can you suggest?</p>
  
  <p>Can you help me?</p>
  
  <p>My code below.</p>
  
  <p>Thank you in advance.</p>
  
  <pre><code>private void ExpireAllCookies()
  {
      if (HttpContext.Current != null)
      {
          int cookieCount = HttpContext.Current.Request.Cookies.Count;
          for (var i = 0; i &lt; cookieCount; i++)
          {
              var cookie = HttpContext.Current.Request.Cookies[i];
              if (cookie != null)
              {
                  var cookieName = cookie.Name;
                  var expiredCookie = new HttpCookie(cookieName) { Expires = DateTime.Now.AddDays(-1) };
                  HttpContext.Current.Response.Cookies.Add(expiredCookie);
              }
          }
  
          HttpContext.Current.Request.Cookies.Clear();
      }
  }
  
  
  
  ............
  
      {
          smtpClient.Send(mailMessagePlainText);
          ExpireAllCookies();
          Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "alert('Ok.');window.location='http://...';", true);
      }
      catch (Exception ex)
      {
          throw (ex);
      }
  </code></pre>