exchangefreaks.com Forum Index
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Downloading attachments, how to do it

 
Post new topic   Reply to topic    exchangefreaks.com Forum Index -> MS Exchange Development
Author Message
Maury Markowitz



Joined: 14 Feb 2008
Posts: 12

PostPosted: Tue Feb 19, 2008 3:11 pm    Post subject: Downloading attachments, how to do it Reply with quote

Ok, it's working. Here's what you do.

To start with, use ADO/ODBC/whatever to do a find and get the message that
you want. If you want "all attachments" then just skip this step. However,
you probably don't want "all attachments", because then you'll be
re-downloading them every time you run it. Anyway...

' use ODBC to open the mail folder and get the list of attachments
conn = ("Provider=Microsoft.Jet.OLEDB.4.0;Exchange 4.0;MAPILEVEL=Public
Folders|All Public Folders;DATABASE=C:\\temp\\;")
Set rstEmails = New ADODB.Recordset
rstEmails.Open "SELECT * FROM mytable WHERE [From] LIKE '%@adomain.com'
AND [Has Attachments]=True AND [Received]>=#" & date & "# AND [Received]<#" &
DateAdd("d", 1, date) & "#", conn, adOpenStatic, adLockReadOnly
if rstEmails.EOF THEN (do something here)

This should be fairly clear: it does a search to find messages that have an
attachment that posted today from a particular address. We do this so we can
find the message using the following code.

Ok, now that we have our message, use CDO to actually get at it...

Set Session = CreateObject("MAPI.session")
Session.logon "", "", False, False, 0
Set Folder = Session.infostores("Public
Folders").rootfolder.folders(2).folders("the folder")

That last line of code is rather nasty. Instead of providing a way to get at
these folders easily from the root, you have to traverse the entire store in
the same way that it is displayed in Outlook. Worse, there appears to be no
way to access "All Public Folders" by name, you have to do it by index, the
(2), which is fragile to say the least (1 is "Favorites").

Ok, now you have the folder you're interested in, so we loop over the
messages and see if we can find the one we wanted...

Set Messages = Folder.Messages
Set Message = Messages.GetFirst()
Do While Not Message Is Nothing
' skip over ones that don't match
If Message.Subject rstEmails!Subject Then GoTo TRYNEXT
If Message.Sender rstEmails!from Then GoTo TRYNEXT
If Message.TimeCreated rstEmails![Creation Time] Then GoTo TRYNEXT
' ADD ADDITIONAL TESTS HERE, BUT THIS SEEMS TO WORK 100% IN MY CASE

' found it, get the attachment
Debug.Print Message.Subject
Set Attachment = Message.Attachments(1)
Attachment.WriteToFile "O:\aPricing\ms_cds_" & Format(date,
"yyyymmdd") & ".txt.pgp"
GoTo ALLDONE

TRYNEXT:
Set Message = Messages.GetNext()
Loop

ALLDONE:

If you are trying to download every message, or every message from today,
you can simply use the second part, the CDO portion, and skip the ADO part at
the top.

I would also like to figure out a way to make the testing for "the right
message" more accurate. It would be nice if I could get the ID from ADO, but
it doesn't seem like this is possible. If anyone can think of a better way to
navigate to the public folder of your choice, I'd love to hear from you.

Enjoy!

Maury

Archived from group: microsoft>public>exchange>development
Back to top
View user's profile Send private message
Display posts from previous:   
Related Topics:
Downloading attachments, some progress... Quick refresher: I'm being sent a number of files in e-mail which is directed to a public folder. I need to extract the attachments once a day. I have made some progress. I have figured out how to open the folder using ODBC and made queries against the me

WebDav Attachments issue! Hi there, I've been struggling for a while to send messages with attachement, it worked once but now I just get an operation time out error. I can send a normal mail easily but the attachment part is a different story. I authenticate using FBA and its an

Use transport agent to remove large attachments Hi, I have to develop an Exchange 2007 transpost agent that handles incoming emails as follows: If an email contains a large attachment, the agent should remove it and put a warning in the email body. This should be done BEFORE the email is written to the

Accessing attachments through ODBC? Any ODBC/Exchange gurus? I have created a public folder to act as a "drop box" for incoming messages that need to be readable by anyone in the company. Some of these messages are automated reports that are generated by 3rd parties. I'd like to access these files, preferably via O

Attachments not downloading to Windows Mobile 5 devices Weird issue... We've just completed our migration to Exchange 2007 SP1 from 2003 SP2. We have Windows Mobile 5 users who can't download attachments; however, those who have Windows Mobile 6 aren't having an issue. The default ActiveSync policy in Exchan
Post new topic   Reply to topic    exchangefreaks.com Forum Index -> MS Exchange Development All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group