Exchange scripting: auto save received attachment on mail with signature
Hi, we are using Echange Server 2003 with Outlook 2003 / 2007. For some mailboxes we automatically save the received attachments to file system like this.
Public Sub AttachmentTrim(BaseSavePath)
Dim oMsg 'New message
Dim oUser 'User Object.
Dim osFileName 'File name with UNC path
Dim oReply 'Reply Message
Dim index : index = 0 'Var for indexing through attachments
Dim ErrorMsgBody 'Body text of reply message
Dim oMsgBody 'Body test of message to be modified
Dim AttachName 'Name of attachment
Dim MsgBodyAdd 'Text being added to the body of the original massage
Dim CurrentFolder 'Folder that the evet took place in
Dim CurrentFolderParent 'Perent of folder that event took place in
Dim osSaveFolder 'Full path to the save location
Dim msg
On Error Resume Next
'Set the current user.
Set oUser = EventDetails.Session.CurrentUser
'Get the current folder and its parent.
Set CurrentFolder = EventDetails.Session.GetFolder(EventDetails.FolderID, Null)
Set CurrentFolderParent = CurrentFolder.Parent
'Get Incoming Message.
Set oMsg = EventDetails.Session.GetMessage(EventDetails.MessageID, Null)
'Save the current message body.
oMsgBody = oMsg.Text
'Veriyf the save folder exists.
osSaveFolder = BaseSavePath
'Clear any current error code
ErrorCode = ""
If oMsg.Attachments.Count > 0 then
MsgBodyAdd = MsgBodyAdd & Chr(10)
Do
index = index + 1
AttachName = oMsg.Attachments.Item(index).Name
osFileName = osSaveFolder & AttachName
If ErrorCode = "" then
oMsg.Attachments.Item(index).WriteToFile(osFileName)
If Err.Number = 0 then
MsgBodyAdd = MsgBodyAdd & osFileName & vbCrLf
oMsg.Attachments.Item(index).Delete
index = index - 1
oMsg.Update
Else
msg = "Error:" & Err.Number & vbCrLf & " description:" & Err.Description & vbCrLf & "There was an error writing a file."
Call SendMail("Script Error", msg)
MsgBodyAdd = MsgBodyAdd & "Error Occured Description: " & Err.Description & vbCrLf
Err.Clear
End if
Else
MsgBodyAdd = MsgBodyAdd & ErrorCode & " Attachment Not Writen to disk. Attachment Name: " & oMsg.Attachments.Item(index).Name & vbCrLf
ErrorCode = ""
Err.Clear
End if
Loop until (index >= oMsg.Attachments.Count) or (oMsg.attachments.count = 0)
oMsg.Text = "Attachments:" & MsgBodyAdd & vbCrLf & "****Body of Message****" & vbCrLf & oMsgBody
End if
oMsg.Update
Set oMsg = Nothing
Set oUser = Nothing
Set osFileName = Nothing
Set oReply = Nothing
Set index = Nothing
Set ErrorMsgBody = Nothing
Set oMsgBody = Nothing
Set AttachName = Nothing
Set MsgBodyAdd = Nothing
Set CurrentFolder = Nothing
Set CurrentFolderParent = Nothing
Set osSaveFolder = Nothing
End Sub
This worked fine until we got our first e-mail with signature.
If we receive a certified message with for example an XLS attachment, the oMsg.Attachments.Count has a value of "1" and the oMsg.Attachments.Item(1).Name is "SMIME.TXT" (not "xxx.XLS" as expected). It seams the XLS is overwritten by the signature file. Anyway
we are not able to see and handle the XLS attachment by code.
Note: In the outlook inbox everything is OK and we can manually save the XLS to file system. But we have to do it automatically.
Thanks for your suggestions
Doublespin
December 20th, 2010 6:05am
Hi Doublespin,
The Development forum is a better place to seek the solution:
http://social.technet.microsoft.com/Forums/en-US/exchangesvrdevelopment/threadsPlease remember to click Mark as Answer on the post that helps you, and to click Unmark as Answer if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Free Windows Admin Tool Kit Click here and download it now
December 24th, 2010 3:58am