Solved

Response.redirect - The HTTP headers are already written to the client browser

Posted on 2010-08-24
9
802 Views
Last Modified: 2012-05-10
Have a script which sends muiltiple emails to a number of email addresses in a loop depending on the number of clients.  

Sometimes this can be 1000 or more and would time out.  

Once complete at the end of the page I do a response.redirect to a page to show it is completed.

I was recommended to add the following to prevent the time out.

At the top of page:
Response.Buffer = True

At the last line of the loop
Response.Flush


But after doing this my response.redirect no longer works giving the error:

Response object error 'ASP 0156 : 80004005'

Header Error

/send/sendmessage/index.asp, line 125

The HTTP headers are already written to the client browser. Any HTTP header modifications must be made before writing page content.


How can I fix this?
Never really used the .buffer or .flush before.
0
Comment
Question by:harris9999
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 28

Expert Comment

by:sybe
ID: 33510297
You can not redirect after having send output to the browser.

Simplest solution is not to do Response.Redirect but to write a javascript redirect

<script>document.location = 'nextpage.htm';</script>

0
 
LVL 6

Expert Comment

by:richardburwood
ID: 33510495
Forget using the response.buffer & response.flush...

simply put

<%
Server.ScriptTimeout = 1200
%>

at the top of the script... the script will not timeout for 20 min...
0
 
LVL 28

Expert Comment

by:sybe
ID: 33510651
Well, my guess is that the code shows progress to the user. But if that is not the case, then why do a Response.Flush at all.

Sending 1000 mails can be (too) hard for the mailserver to handle. In similar functionality I made the script wait a few seconds after 50 mails had been sent.
0
 
LVL 3

Author Comment

by:harris9999
ID: 33510924
No progress is shown to the user, so I assume then that the response.flush is of no use?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:FEOX
ID: 33511167
Sybe is right.  You cannot redirect when there is already data sent to the Response Buffer (via Response.Flush) even though it may be empty.  You will have to do away with Response.Flush (and Response.Buffer) and just use Response.Redirect.

On the other hand, if it would take some time for the page to be processed in sending emails, displaying progress status (using Response output and Response.Flush) would be a great idea.  As sybe said, at the end of your page, Response.write a client-side script so the browser (not the server) will handle the redirect.
0
 
LVL 3

Author Comment

by:harris9999
ID: 33511279
Ok.  So if I was to display progress how is that best done?

just do a response.write whatever I want, then does the response.flush clear that and repeated over again through the loop?
0
 
LVL 2

Accepted Solution

by:
FEOX earned 250 total points
ID: 33511561
Try including this on your ASP page.  Just change the layout, loop and insert your email code:

<!-- Set-up Page -->

<CENTER>Sending Emails...</CENTER><BR>

<CENTER><DIV ID="msg1">Generating List...</DIV></CENTER><BR>

<BR>



<!-- SCRIPT TO GO THROUGH LIST -->

<SCRIPT>

<%

  // Assuming Rset is the recordset for your list of recipients

  var c_name;



  Rset.MoveFirst();

  while (!Rset.EOF) {

  

    c_name = Rset("name");

    

    // ... code to email to recipient



    // Update page to show current name on msg1

    %>document.all("msg1").innerText = unescape("<%= escape(c_name) %>");<%

    Response.Flush();

  

    Rset.MoveNext();

  };

%>

</SCRIPT>



<!-- Go to next page (as taken from sybe) -->

<SCRIPT>document.location = 'nextpage.html';</SCRIPT>

Open in new window

0
 
LVL 2

Expert Comment

by:FEOX
ID: 33511578
While the above code display's the names, something similar can be done using a progress bar instead.

Hope this helps.
0
 
LVL 5

Assisted Solution

by:Abiel de Groot
Abiel de Groot earned 250 total points
ID: 33512015
I too ran into a simular problem when sending large numbers of Emails. I use Persits Mail but the idea would work with most other mail scripts.

Send the email directly to the smtp servers "Queue folder" rather than sending one by one.

If you choose to send one by one the process is as follows.

1. Negotiate authorisation with the smtp server
2. If accepted, the smtp server attempts top send the mail
3. smtp server send results (Success or not)
4. connection closed
5. Do it all over again for each individual mail message.... 1000 times +

This is very time consuming and server heavy.

Solution.....................

Open the connection to the smtp server and loop all the 1000 + emails using "Mail.Queue = True"
This simply send all to the queue folder close and go home.

The smtp server by default sends queue content in batches of 50 at a time and there is no negotiation after the initial entery.

Doing it this way I can send many 1000's of emails to registered clients, within a few seconds.

My sample code below.

Hope this helps.





If IsArray(ArrEmailRecipients) Then
For Fu = 0 To Ubound(ArrEmailRecipients,2)
Set Mail = Server.CreateObject("Persits.MailSender")
Mail.Charset = "UTF-8"
Mail.ContentTransferEncoding = "quoted-printable"
Mail.Username = E_Email_UN
Mail.Password = E_Email_PW
Mail.Queue = True
Mail.Host = E_SMTP_Server
Mail.From = E_Email_SystemAddress
Mail.FromName = strCompanyName

Mail.AddAddress   ArrEmailRecipients(3,Fu)
Mail.Subject = Mail.EncodeHeader(strCompanyName & " - " & strEmailTitle, "UTF-8")
Mail.Body = strBody
Mail.IsHTML = True

On Error Resume Next
      Mail.Send
      If Err <> 0 Then
        Response.Write "An error occurred: " & Err.Description
      End If
On Error GoTo 0

Set Mail = Nothing
Next
End If




0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now