Become a Columnist Microsoft Exchange Site Microsoft Support SiteMSDN Exchange Site

       How did you like this article? Please vote and let us know.          

Subscribe to OutlookExchange
Anderson Patricio
Ann Mc Donough
Bob Spurzem
Brian Veal
Catherine Creary
Cherry Beado
Colin Janssen
Collins Timothy Mutesaria
Drew Nicholson
Fred Volking
Glen Scales
Goran Husman
Guy Thomas
Henrik Walther
Jason Sherry
Jayme Bowers
John Young
Joyce Tang
Justin Braun
Konstantin Zheludev
Kristina Waters
Kuang Zhang
Mahmoud Magdy
Martin Tuip
Michael Dong
Michele Deo
Mitch Tulloch
Nicolas Blank
Pavel Nagaev
Ragnar Harper
Ricardo Silva
Richard Wakeman
Russ Iuliano
Santhosh Hanumanthappa
Shannal L. Thomas
Steve Bryant
Steve Craig
Todd Walker
Tracey J. Rosenblath

 

 
 

Forward Mail on Demand to any Mailbox    Download scripts

The script allows you to forward all the unread mail in an inbox on demand to the mailbox that initiates the request to forward the mail. For example the user logs onto their hotmail address sends a message to their office email address with a subject of Mailbox Forward and for extra security they put a password in the body of the email message (my example uses the password eskimo). This mail fires a Web Storage System event sink on the Exchange sever that goes through the user's inbox marks each unread message as read and then forwards the message to the email address in the initiating email altering the subject line to MB forward: (original subject). This basically allows you to be at anyone's email box and have you email temporarily forward there, you can call this script successively and only have your new email sent because mail is marked as read once its forwarded.

How it works

I've used a two step approach to firing this event that adds a layer of abstraction for the Exchange event sinks. My event sink code look as follows

<SCRIPT LANGUAGE="VBScript">

Sub ExStoreEvents_OnSave(pEventInfo, bstrURLItem, lFlags)

Stm = bstrURLItem
set WshShell = CreateObject("WScript.Shell")
strrun = WshShell.run ("c:\evtsink\formail.vbs " & stm)
set WshShell = nothing
End Sub

</SCRIPT>

This piece of code spawns a process that starts the main script and passes the URL of the email  that caused the event. The main script uses both ADO and CDO to perform the required functions, it starts by creating an ADO query that creates a record set with all the unread messages in the users inbox. The code then loops through this recordset one at a time creating a CDO instance for each email modifying the read properties of the email then forwarding it to the initiating email address and modifying the subject .

Main Script

Dim Rec,Rs,strURLInbox,msgobj,msgobj1,flds,objArgs,strView
Set objArgs = WScript.Arguments
For I = 0 to objArgs.Count - 1
   if I = 0 then
	inbstr = objArgs(I)
   else
	inbstr = inbstr & " " & objArgs(I)
   end if
Next
Set msgobj = CreateObject("CDO.Message")
Set Rec = CreateObject("ADODB.Record")
Set Rs = CreateObject("ADODB.Recordset")
msgobj.DataSource.Open inbstr 
if msgobj.subject = "Mailbox Forward" then
	if instr(msgobj.textbody,"eskimo") then
		slen1 = instr(inbstr,"Inbox")
		elen = slen1 + 4
		strURLMailbox = Mid(inbstr,1,elen)
		Rec.Open strURLMailbox
		strView = "select " _
		& "  ""DAV:href""" _
		& ", ""DAV:content-class""" _
		& ", ""urn:schemas:httpmail:datereceived""" _
		& ", ""DAV:isfolder""" _
		& ", ""DAV:getcontentlength""" _
		& ", ""urn:schemas:httpmail:from""" _
		& ", ""urn:schemas:httpmail:subject""" _
		& ", ""urn:schemas:mailheader:importance""" _
		& ", ""urn:schemas:httpmail:hasattachment""" _
		& ", ""urn:schemas:httpmail:read""" _
		& ", ""DAV:lastaccessed""" _
		& " from scope ('shallow traversal of """ _
		& strURLInbox & """') " _
		& " WHERE ""DAV:isfolder"" = false AND ""DAV:ishidden"" = false AND ""urn:schemas:httpmail:read"" = false" _
		& " ORDER BY ""urn:schemas:httpmail:datereceived"" DESC"
		Rs.Open strView, Rec.ActiveConnection, 3
		If Rs.RecordCount <> 0 Then
  		Rs.MoveFirst
		  While Not Rs.EOF
			set msgobj1 = CreateObject("CDO.Message")
			msgobj1.DataSource.Open Rs.Fields("DAV:href").Value, ,3
			msgobj1.Fields("urn:schemas:httpmail:read").value = true
			msgobj1.fields.update
			msgobj1.datasource.save
			msgobj1.subject = "MB Forward:" & msgobj1.subject
			msgobj1.To = msgobj.From
			msgobj1.send
			set msgobj1 = nothing
			Rs.MoveNext
  		   Wend
		End If
	end if
end if
set msgobj = nothing
set rec = nothing
set rs = nothing

Registering this script

For details on installing and registering this script see my previous article Using VBS Event Sink scripts with the Web Storage System. Or have a look in the ESDK search for regevent.vbs.   Download scripts


Disclaimer: Your use of the information contained in these pages is at your sole risk. All information on these pages is provided "as is", without any warranty, whether express or implied, of its accuracy, completeness, fitness for a particular purpose, title or non-infringement, and none of the third-party products or information mentioned in the work are authored, recommended, supported or guaranteed by Pro Exchange. OutlookExchange.Com and Pro Exchange shall not be liable for any damages you may sustain by using this information, whether direct, indirect, special, incidental or consequential, even if it has been advised of the possibility of such damages.

© Copyright Pro Exchange, Inc., 2006