开发者

Whats the best way to Programmatically Process New Email Messages and Store Attachments

I have several clients/vendors that distribute reports to me via email. Some of these files are enormous, and need to be removed from email and saved on a file share for processing, as well as to control mailbox size.

Can anyone provide guidance on their recommended method of automatically downloading and saving attachments.

I am in a MS Windows Environment (Client & Server Computers). Emails are on an Microsoft Exchange 2003 Email Server.

Preferred use of Microsoft Technology for consistency across solutions (C#), however I am open to any suggestions, be it C#, VBScript, Perl, Java, Components I should purchase, etc..

Scenario Each Day bob@whysendmereportsbyemail.com sends an email with the subject "Activity Report for YYYY-MM-DD" to me at john@myemailaddress.com

Each Email has an attachment named "ActivityReport-YYYY-MM-DD-HH-MI-SS.xls" which I need to save on my filesystem at "C:\FilesFromBob\ActivityReport-YYYY-MM-DD-HH-MI-SS.xls"

Thanks in adv开发者_如何学编程ance for any assistance.


Exchange 2003 provides a WebDav API which you can use to access emails, contacts etc.. from a user's account.

There's a few answers about accessing a user's Exchange inbox on SO already. I've previously used this approach for almost exactly the situation you outline, and once you work out the WebDav API model and the structure of the requests and responses, it's not too difficult to extract emails and their attachments.

There are other ways to interact with Exchange 2003 (outlined on SO here), but I've only tried the WebDav approach because it seemed the most reliable.


I finally wrote the code to store messages from Outlook Unfortunately this code runs from within Outlook, so Outlook has to be open.

I did not yet investigate how to schedule the run, but now its easy to do

Sub SaveOutlookFileAttachments()


Dim oStores As Outlook.Stores
Dim oStore As Outlook.Store
Dim oFolders As Outlook.Folders
Dim oFolder As Outlook.Folder
Dim destFolder As String
Dim oItems As Outlook.Items
Dim oMsg As Outlook.MailItem
Dim oAttachments As Outlook.Attachments
Dim oAttachment As Outlook.Attachment
Dim oExplorer As Outlook.Explorer


destFolder = "\\NetworkShare\OrderDetailReport\"

On Error Resume Next
Set oStores = Application.Session.Stores

For Each oStore In oStores
    If oStore.DisplayName = "Inbox" Then
        oFolders = oStore.GetSearchFolders
        For Each oFolder In oFolders
        oItems = oFolder.Items
            For Each oMsg In oItems
                oAttachments = oMsg.Attachments
                For Each oAttachment In oAttachments
                    If InStr(1, oAttachment.FileName, "orderdetail_", vbTextCompare) Then
                        'MsgBox ("This File Needs to be Saved: " & oAttachment.FileName)
                        oAttachment.SaveAsFile (destFolder & oAtch.DisplayName)
                    End If
                Next
            Next

        Next

    End If

Next


End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜