I made a script that pulls email messages and posts them in an access database. From time to time, I am getting something that gives me a script out of range error. More then likely it is extra header information (best guess).

Can anyone see some place in my code that I can correct this or does anyone have any other scripts that checks emails and puts them in an MDB?

Error:

Microsoft VBScript runtime error '800a0009'
Subscript out of range: 'msg'
/webdesk/pop3open_update.asp, line 37

Code:

tempstr=replace(request("emailbody"),"<BR>",vbcrlf)
tempstr = split(tempstr, vbcrlf)
for msg = 0 to ubound(tempstr)
if lcase(left(tempstr(msg),5))="from:" then
emailstart = instr(tempstr(msg), "<")
emailend = instr(tempstr(msg), ">")
user_email = mid(tempstr(msg), emailstart + 1)
user_email = left(user_email, len(user_email) - 1)
end if
if instr(tempstr(msg), "Subject:") > 0 then subject = mid(tempstr(msg), 10)
if instr(tempstr(msg), "boundary=") > 0 then
msgendtemp = instr(tempstr(msg), "boundary")
msgend = mid(tempstr(msg), msgendtemp + 11, 10)
end if
if tempstr(msg) = "" then
if msgendtemp > 0 then
do until msg=>ubound(tempstr) or tempstr(msg) = ""
msg = msg + 1
loop
do until msg>=ubound(tempstr) or instr(tempstr(msg), msgend) > 0
msg = msg + 1
loop
do until msg=>ubound(tempstr) or tempstr(msg) = ""
msg = msg + 1
loop
msg = msg + 1
else
msg = msg + 1
end if
Error area ===>> Do until msg=>ubound(tempstr) or tempstr(msg) = "." or instr(tempstr(msg), msgend) > 0
strBody = strBody & tempstr(msg) & "<BR>"
msg = msg + 1
if msg>ubound(tempstr) then exit do
loop
exit for
end if
next

if len(strBody)<1 then strBody=""
tempsubject=subject
if (instr(subject, "Ticket Updated") > 0 or instr(subject, "Ticket ID") > 0) and instr(subject, "/") > 0 then
ticketupdate = true
tickettemp = split(subject, "Ticket ID - ")
ticket_user = split(tickettemp(1), "/")
ticketid = ticket_user(0)
rs.open "select user_id,user_email,user_name from tbluser where user_account_id=" & session("account_id") & " AND user_id=" & ticket_user(1),session("conn")
else
rs.open "select user_id,user_email,user_name from tbluser where user_account_id=" & session("account_id") & " AND user_email='" & user_email & "'",session("conn")
end if

Thanks in advance,
Gary