Author |
|
karen32 Newbie
Joined: 30 October 2006
Online Status: Offline Posts: 8
|
Posted: 18 December 2006 at 10:14am | IP Logged
|
|
|
Hi All,
I have a question about retrieving new messages. I am looking to retrieve all new messages (to,from,date,subject,message body, and all attachments). I've found samples of how to do this with POP3, but with IMAP4, I've only found a way to download either just a summary of all messages or a single message. Is the only way to do this to just create a loop and retrieve a single message each pass until there are no new messages left?
Thank you!
|
Back to Top |
|
|
Andrew AfterLogic Support
Joined: 28 April 2006 Location: United States
Online Status: Offline Posts: 1189
|
Posted: 19 December 2006 at 3:28am | IP Logged
|
|
|
You can retrieve all messages from an IMAP folder via IMAP4.RetrieveEnvelopesEx method. To retrieve not only messages' headers, but the messages' bodies too, you should specify AlsoGetMessagePreview parameter (the 5th) as true and BodyPreviewSize equal to 9999999 (about 10MB) or more. The following sample shows how to do this (in VB6 syntax):
Code:
Dim Mailer, Envelopes, Envelope
Set Mailer = CreateObject("MailBee.IMAP4")
' Enable logging
Mailer.EnableLogging = True
Mailer.LogFilePath = "C:\imap4_log.txt"
Mailer.ClearLog
Mailer.LicenseKey = "Your license key here"
' Connect to IMAP server
If Mailer.Connect("mailserver.com", 143, "MyName", "MyPassword") Then
' Select Inbox folder
If Mailer.SelectMailbox("Inbox") Then
' Retrieve all envelopes
Set Envelopes = Mailer.RetrieveEnvelopesEx(1, Mailer.MessageCount, False, True, True, 9999999)
If Not Envelopes Is Nothing Then
For Each Envelope In Envelopes
' Display body text of the message
MsgBox Envelope.MessagePreview.BodyText
Next
End If
End If
Mailer.Disconnect
End If |
|
|
Envelope.MessagePreview is of MailBee.Message type and contains entire message which you can use in the same way as you use the message retrieved via IMAP4.RetrieveSingleMessage method.
Best regards,
Andrew
|
Back to Top |
|
|
karen32 Newbie
Joined: 30 October 2006
Online Status: Offline Posts: 8
|
Posted: 19 December 2006 at 8:48am | IP Logged
|
|
|
Hi Andrew,
Thank you for your response. I had tried using the Envelope approach you mentioned above. I could be missing something, but I couldn't find a way to download the message attachments this way w/o still using the RetrieveSingleMessage method. That is what lead me to believe that looping through the messages one at a time was the only way to gain access to all of the data I need - to, from, subject, date, message body & attachements.
|
Back to Top |
|
|
Andrew AfterLogic Support
Joined: 28 April 2006 Location: United States
Online Status: Offline Posts: 1189
|
Posted: 19 December 2006 at 10:54am | IP Logged
|
|
|
There shouldn't be any problems with downloading attachments. We modified the code above:
Code:
Dim Mailer, Envelopes, Envelope, Attach
Set Mailer = CreateObject("MailBee.IMAP4")
' Enable logging
Mailer.EnableLogging = True
Mailer.LogFilePath = "C:\imap4_log.txt"
Mailer.ClearLog
Mailer.LicenseKey = "Your license key here"
' Connect to IMAP server
If Mailer.Connect("mailserver.com", 143, "MyName", "MyPassword") Then
' Select Inbox folder
If Mailer.SelectMailbox("Inbox") Then
' Retrieve all envelopes
Set Envelopes = Mailer.RetrieveEnvelopesEx(1, Mailer.MessageCount, False, True, True, 9999999)
If Not Envelopes Is Nothing Then
For Each Envelope In Envelopes
' Display attachments count
MsgBox Envelope.MessagePreview.Attachments.Count
' Save each attachment to disk
For Each Attach in Envelope.MessagePreview.Attachments
Attach.SaveFile "C:\temp\attachments\"
Next
Next
End If
End If
Mailer.Disconnect
End If |
|
|
and tested it. It successfully retrieves entire messages, displays attachment numbers, and saves all the attachments to disk.
In the case if you have messages which sizes more than 10MB, you should use 99999999 (about 100MB) instead of 9999999 (about 10MB). Please try to increase this value and let us know the outcome.
This approach allows retrieving all messages from the mailbox via single call of IMAP4.RetrieveEnvelopesEx method and you get the same result as in the case of RetrieveMessages method for POP3 accounts.
Best regards,
Andrew
|
Back to Top |
|
|
karen32 Newbie
Joined: 30 October 2006
Online Status: Offline Posts: 8
|
Posted: 19 December 2006 at 1:32pm | IP Logged
|
|
|
Thank you so much! It hadn't been clear to me that the MessagePreview had the same collections and properties as the Message Object.
|
Back to Top |
|
|