Author |
|
webchetan Newbie
Joined: 23 October 2006
Online Status: Offline Posts: 20
|
Posted: 24 October 2006 at 10:40am | IP Logged
|
|
|
Hi - I am new to MailBee.NET objects. Could someone help me, is to how to iterate throught only new emals in IMAP or POP3 and save attachments to a folder?
Thanks a bunch!
|
Back to Top |
|
|
Andrew AfterLogic Support
Joined: 28 April 2006 Location: United States
Online Status: Offline Posts: 1189
|
Posted: 25 October 2006 at 4:52am | IP Logged
|
|
|
The POP3 protocol has no mechanism to determine whether a message has been already downloaded. Therefore, it's required to keep a list of identifiers of already downloaded messages in a local database maintained by your application.
Any time your application searches the mailbox for new messages, it queries the database for each message in the mailbox to determine if a message has already been downloaded. If the identifier of the particular message has not been found in the database, this message is new.
As the message identifier, Unique-ID can be used. Unique-ID (UID for short) is a unique string assigned to each message in the mailbox. No two messages in the mailbox can have the same Unique-ID value. Sometimes Unique-ID is called GUID (globally unique identifier).
Unique-ID is not associated with "Message-ID:" header of the message.
The following sample shows how to retrieve new messages from POP3 server and save all attachments:
Code:
// You should implement this function
bool IsNewMessage(string uid)
{
return true;
}
Smtp.LicenseKey = "Trial or permanent key";
Pop3 pop = new Pop3();
// Connecting to POP3 server
pop.Connect("mail.domain.com");
// Logging into the account
pop.Login("jdoe", "secret");
// Retrieving the list of UIDs
string[] uids = pop.GetMessageUids();
// Iterating through UIDs collection
foreach(string uid in uids)
{
// Checking if the message is new
if(IsNewMessage(uid))
{
// Downloading entire message
MailMessage msg = pop.DownloadEntireMessage(pop.GetMessageIndexFromUid(uid));
// Saving all message attachments into the folder
msg.Attachments.SaveAll(@"C:\temp\attachments");
}
}
// Disconnecting from the POP3 server
pop.Disconnect(); |
|
|
In real code, the sample function IsNewMessage() should query the database for specified Unique-ID value and return true only if this Unique-ID value is not found in the database. For demo purposes, this function always returns true.
The same approach can be applied for IMAP account too:
Code:
Smtp.LicenseKey = "Trial or permanent key";
Imap imp = new Imap();
imp.Connect("mail.domain.com");
imp.Login("jdoe", "secret");
imp.SelectFolder("Inbox");
EnvelopeCollection envelopes = imp.DownloadEnvelopes("1:*", true);
foreach(Envelope envelope in envelopes)
{
if(IsNewMessage(envelope.Uid))
{
MailMessage msg = imp.DownloadEntireMessage(envelope.Uid, true);
msg.Attachments.SaveAll(@"C:\temp\attachments");
}
}
imp.Disconnect(); |
|
|
Also, IMAP protocol supports messages' flags stored on mail server. You can check these flags instead of keeping UIDs in database. You can use Seen or Recent flags for this purpose:
Code:
if((envelope.Flags.SystemFlags & SystemMessageFlags.Seen) != SystemMessageFlags.Seen) |
|
|
instead of:
Code:
if(IsNewMessage(envelope.Uid)) |
|
|
|
Back to Top |
|
|
webchetan Newbie
Joined: 23 October 2006
Online Status: Offline Posts: 20
|
Posted: 26 October 2006 at 9:38am | IP Logged
|
|
|
Hi Andrew - Thank you for the reply. I appreciate it.
I had one question I have 2 IMAP email accounts (Say A and another B) both of which has attchments in the emails. When I view the source of message in A, it shows me the content-type as multipart/message as below...
Message-ID: <000001c6f30d$b9e14330$4e02a8c0@IBM>
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_NextPart_000_0001_01C6F2EC.32CFA330"
Now the source in another email account "B" does not show this above information as it does with Account "A", considering both of them has an attachment.
The difficult part here is that when I use the below code to detect attachments, it some how doesnot detect attachments for email account "B" but for account "A" it does and successfully downloads all the attachments. I debugged and found that when I pass in credentials for account "B" it doesnot find
part.Disposition, part.ContentType and part.Filename
Any ideas why it does that even though both the accounts with the emails has attchments when you manually open them up?
Thanks!
Code:
envelopes = imp.DownloadEnvelopes(uids.ToString, True, _
EnvelopeParts.MailBeeEnvelope Or EnvelopeParts.BodyStructure, 0)
envs = imp.DownloadEnvelopes(uids.ToString(), True, EnvelopeParts.MessagePreview, _
-1, Nothing, Nothing)
For Each env In envelopes
If env.IsValid Then
Dim parts As ImapBodyStructureCollection = env.BodyStructure.GetAllParts()
For Each part As ImapBodyStructure In parts
' Detect if this part is attachment.
If (Not part.Disposition Is Nothing AndAlso _
part.Disposition.ToLower() = "attachment") OrElse _
(Not part.Filename Is Nothing AndAlso _
part.Filename <> String.Empty) OrElse _
(Not part.ContentType Is Nothing AndAlso _
part.ContentType.ToLower() = "message/rfc822") Then
uids.Add(env.Uid)
Exit For
End If
Next
End If
Next
|
|
|
|
Back to Top |
|
|
webchetan Newbie
Joined: 23 October 2006
Online Status: Offline Posts: 20
|
Posted: 26 October 2006 at 9:52am | IP Logged
|
|
|
Andrew - Btw, When I double click the message using the IMAP Demo 2 supplied. I see the attachment in the Body Part (for account "B" in above scenario)as below
This come as Uuencode a form of binary to text encoding
Code:
begin 666 data.zip
M"^KH)'ODBDS8?^S_9/U+UC1H),$J1@-&=QTFY;DPK)S;09@=,Z[=]II4D"*>
M`7C'>:T'\R\G3T[Q1.Y#CZ6Q.]/`U<M<*U+B[-I)KJBJB)T.^A7BUNMJ2[V?
MMWD601+L[?E-,9HHHP,*1<.6,/EO1S7[S&JDNRC0=%:C[\!?>5\#Q 7^D9VI'
M-OQF840DX185M%[Y%#M03!/H1G&J"D,]P^ABKI2ZS$>PFH+YYHHO5TV/\!US
|
|
|
Is there a way we could read the body part and encode it back to the ZIP file which is actually the original file?
Any help would be great!
Thank you!
|
Back to Top |
|
|
Andrew AfterLogic Support
Joined: 28 April 2006 Location: United States
Online Status: Offline Posts: 1189
|
Posted: 26 October 2006 at 10:40am | IP Logged
|
|
|
This message is a non-MIME message. The attachments in this message are contained in the message body text directly and it's impossible to detect them through headers (part.Disposition, part.Filename, etc.). To detect the attachment, you have to fully download the message.
Please try to fully download this message and check Attachments collection. If there is no this attachment in the collection, please provide us with this message as .eml file. Thus, we would be able to investigate it. You can attach the .eml file to a message and sent it to support@afterlogic.com.
|
Back to Top |
|
|
webchetan Newbie
Joined: 23 October 2006
Online Status: Offline Posts: 20
|
Posted: 26 October 2006 at 11:24am | IP Logged
|
|
|
Thanks for the reply. Really fast! Okay Now could you help me is to how to download the entire message and look for any attachments? from the code I sent above...what I mean is how can I modify the code?
|
Back to Top |
|
|
Alex AfterLogic Support
Joined: 19 November 2003
Online Status: Offline Posts: 2206
|
Posted: 26 October 2006 at 11:42am | IP Logged
|
|
|
The code above (our first posting) already downloads entire messages and saves all attachments. To learn the number of attachments instead of saving them, you can get msg.Attachments.Count instead of calling msg.Attachments.SaveAll(@"C:\temp\attachments");
Regards,
Alex
|
Back to Top |
|
|
webchetan Newbie
Joined: 23 October 2006
Online Status: Offline Posts: 20
|
Posted: 26 October 2006 at 2:25pm | IP Logged
|
|
|
Alex - Bunch of thanks! Btw could please just tell me what code I need to enter for the example you mentioned above to only download messages (envelopes) since 1-OCT-2006. Basically having a date range. If you could show me an example I would be obliged!
Thanks again!
MailBee rocks!
Sincerely,
Chetan
|
Back to Top |
|
|
Alex AfterLogic Support
Joined: 19 November 2003
Online Status: Offline Posts: 2206
|
Posted: 26 October 2006 at 2:57pm | IP Logged
|
|
|
Assuming a folder is already selected and you wish to download envelopes in 1-Oct to 1-Nov range:
Code:
UidCollection uids = (UidCollection)imp.Search(true, "SINCE 1-OCT-2006 BEFORE 1-NOV-2006", null);
EnvelopeCollection envelopes = imp.DownloadEnvelopes(uids.ToString(), true);
|
|
|
The same approach will work fine for downloading entire messages (DownloadEntireMessages).
Regards,
Alex
|
Back to Top |
|
|
webchetan Newbie
Joined: 23 October 2006
Online Status: Offline Posts: 20
|
Posted: 01 November 2006 at 12:24pm | IP Logged
|
|
|
Hi Alex - Is it possible for me to read the Uuencoded message which has an attachment in the Body Part of the email?
The point here is that email server is actuall the Apple Mac server and the attachmnets in that are Uuencode to Body Part. We would eventually move to Microsoft Exchange Server which has a standar MIME type.
Now could I check the Body part of the email message to see if it has attachments using MailBee.NET Objects.
I am very fond of MailBee.NET Objects now, and I would be glad if its has that functionality?
I would be glad if you could explain it to me with an example in VB.NET
Thanks a million!
MailBee.NET ROCKS!!!!!!!!!
Sincerely,
Chetan A. Sharma
|
Back to Top |
|
|
Alex AfterLogic Support
Joined: 19 November 2003
Online Status: Offline Posts: 2206
|
Posted: 01 November 2006 at 12:45pm | IP Logged
|
|
|
Yes, MailBee.NET (and MailBee ActiveX version) both support UUE attachments in the body. You do not need to do anything special to extract them. This is done automatically, and you can access this attachments via Attachment property of MailMessage object just as you do with any other types of attachments. If, however, you encountered a message which does not expose its attachments via MailMessage.Attachments property, please send it to support@afterlogic.com for examination. Thanks!
Regards
Alex
|
Back to Top |
|
|
webchetan Newbie
Joined: 23 October 2006
Online Status: Offline Posts: 20
|
Posted: 02 November 2006 at 9:55am | IP Logged
|
|
|
Hi Alex - Thank you for your reply. Could you tell me if there is an example where I can look at? or I mean what method or property needs to be used to look for attachments in the Body Part of the message?
Would appreciate if you could tell me how would I modify the below code to check for attachments in the "Body Part" of the message if not found as a "parts" in ImapBodyStructure envelope collection.
Code:
' List ALL messages or only NEW ones?
If checkBoxNewOnly.Checked Then
' Get Unique-IDs of new messages.
'uids = CType(imp.Search(True, "NEW", Nothing), UidCollection)
uids = CType(imp.Search(True, "SINCE 24-OCT-2006", Nothing), UidCollection)
Else
' Get Unique-IDs of all messages.
uids = CType(imp.Search(True, "ALL", Nothing), UidCollection)
End If
' Any messages found?
If uids.Count > 0 Then
msgCountTotal = uids.Count
msgCountDownloaded = 0
' Subscribe to EnvelopeDownloaded event.
AddHandler imp.EnvelopeDownloaded, AddressOf OnEnvelopeDownloaded
Try
' Download envelopes (to detect attachments,
' we also download body structure). Body structure also helps
' to determine message charset if it's not available in the
' message header (usually, in multipart messages).
envelopes = imp.DownloadEnvelopes(uids.ToString, True, _
EnvelopeParts.MailBeeEnvelope Or EnvelopeParts.BodyStructure, 0)
envs = imp.DownloadEnvelopes(uids.ToString(), True, EnvelopeParts.MessagePreview, _
-1, Nothing, Nothing)
For Each env In envelopes
If env.IsValid Then
Dim parts As ImapBodyStructureCollection = env.BodyStructure.GetAllParts()
For Each part As ImapBodyStructure In parts
' Detect if this part is attachment.
If (Not part.Disposition Is Nothing AndAlso _
part.Disposition.ToLower() = "attachment") OrElse _
(Not part.Filename Is Nothing AndAlso _
part.Filename <> String.Empty) OrElse _
(Not part.ContentType Is Nothing AndAlso _
part.ContentType.ToLower() = "message/rfc822") Then
uids.Add(env.Uid)
Exit For
End If
Next
End If
Next
For Each env In envs
env.MessagePreview.Attachments.SaveAll("C:\_Temp")
Next
Catch ex As Exception
btnDecryptFiles.Enabled = False
'For Each msgAttachment In msg.Attachments
' msgAttachment.Save("C:\_Temp\" + msg.Subject + msgAttachment.Filename, False)
'Next
Finally
' Unsubscribe from EnvelopeDownloaded event.
RemoveHandler imp.EnvelopeDownloaded, AddressOf OnEnvelopeDownloaded
End Try
btnDecryptFiles.Enabled = True
If checkBoxNewerFirst.Checked Then
envelopes.Reverse()
End If
ElseIf checkBoxNewOnly.Checked Then
MessageBox.Show("Inbox contains no new messages")
Else
MessageBox.Show("Inbox contains no messages")
End If
|
|
|
Would appreciate a little help.
Thanks again!
Alex wrote:
Yes, MailBee.NET (and MailBee ActiveX version) both support UUE attachments in the body. You do not need to do anything special to extract them. This is done automatically, and you can access this attachments via Attachment property of MailMessage object just as you do with any other types of attachments. If, however, you encountered a message which does not expose its attachments via MailMessage.Attachments property, please send it to support@afterlogic.com for examination. Thanks!
Regards
Alex |
|
|
|
Back to Top |
|
|
Andrew AfterLogic Support
Joined: 28 April 2006 Location: United States
Online Status: Offline Posts: 1189
|
Posted: 02 November 2006 at 10:16am | IP Logged
|
|
|
The following sample receives all messages from Inbox folder of the IMAP account and saves all attachments contained in them to the file system and doesn't matter if they UUE attachments contained in message body directly or they're standard MIME attachments.
Code:
Imap.LicenseKey = "Trial or permanent key";
Imap imp = new Imap();
imp.Connect("mail.domain.com");
imp.Login("jdoe", "secret");
imp.SelectFolder("Inbox");
EnvelopeCollection envelopes = imp.DownloadEnvelopes("1:*", true);
foreach(Envelope envelope in envelopes)
{
MailMessage msg = imp.DownloadEntireMessage(envelope.Uid, true);
msg.Attachments.SaveAll(@"C:\temp\at tachments");
}
imp.Disconnect();
|
|
|
Best regards,
Andrew
|
Back to Top |
|
|
webchetan Newbie
Joined: 23 October 2006
Online Status: Offline Posts: 20
|
Posted: 07 November 2006 at 4:07pm | IP Logged
|
|
|
Hi Andrew - I am loving this application day by day.
Btw, could I just add the above codes which you sent...together so that I can download only messages starting 1st Oct 2006 and Before 1st Nov 2006 and save all the attachmnets contained within those emails?
Please advice.
Thanks and much appreciated!
Code:
UidCollection uids = (UidCollection)imp.Search(true, "SINCE 1-OCT-2006 BEFORE 1-NOV-2006", null);
EnvelopeCollection envelopes = imp.DownloadEnvelopes(uids.ToString(), true);
Imap.LicenseKey = "Trial or permanent key";
Imap imp = new Imap();
imp.Connect("mail.domain.com");
imp.Login("jdoe", "secret");
imp.SelectFolder("Inbox");
EnvelopeCollection envelopes = imp.DownloadEnvelopes("1:*", true);
foreach(Envelope envelope in envelopes)
{
MailMessage msg = imp.DownloadEntireMessage(envelope.Uid, true);
msg.Attachments.SaveAll(@"C:\temp\at tachments");
}
imp.Disconnect();
|
|
|
|
Back to Top |
|
|
Andrew AfterLogic Support
Joined: 28 April 2006 Location: United States
Online Status: Offline Posts: 1189
|
Posted: 08 November 2006 at 6:46am | IP Logged
|
|
|
The following sample downloads only messages starting 1st Oct 2006 and before 1st Nov 2006 and saves all the attachments contained within these emails.
Code:
Imap.LicenseKey = "Trial or permanent key";
Imap imp = new Imap();
imp.Connect("mail.domain.com");
imp.Login("jdoe", "secret");
imp.SelectFolder("Inbox");
UidCollection uids = (UidCollection)imp.Search(true, "SINCE 1-OCT-2006 BEFORE 1-NOV-2006", null);
EnvelopeCollection envelopes = imp.DownloadEnvelopes(uids.ToString(), true);
foreach(Envelope envelope in envelopes)
{
MailMessage msg = imp.DownloadEntireMessage(envelope.Uid, true);
msg.Attachments.SaveAll(@"C:\temp\attachments");
}
imp.Disconnect(); |
|
|
Best regards,
Andrew
|
Back to Top |
|
|
webchetan Newbie
Joined: 23 October 2006
Online Status: Offline Posts: 20
|
Posted: 16 November 2006 at 12:31pm | IP Logged
|
|
|
Hi Andrew - I am really desperate to get this working...heres the delimma...this is what I am getting in the Body Part of the email which I receive.
Code:
begin 644 Test.zip
M4$L#!!0`"``(`%21;S4````````````````+````;F5W9&%T82YX;6R\O7V 3
MVSB2-_C_1=QW8'3$;I1CZX7ODMK/7$2UW6Y[QG;[VF[/QOUSP9)8*HXI4D- 2
M9:N?N.]^"5`B``HI46+F[FS,=!?$'X!$9B*1F4C\K\]9DSKBO]Z]_MM/__O>
M]2:_OHY_O9E%;Z8WX6MO=G,_^V5V\^;>?3/SIJ_\5
|
|
|
Now since this above message (example code) is UUEncoded...I am not able to download the actual Test.ZIP file which actually is an attachment.
Could you please let me know is to how MailBee.NET objects can do that.
Would be a big help and I would be highly obliged.
Thank you!
Sincerely,
Chetan A. Sharma
|
Back to Top |
|
|
Alex AfterLogic Support
Joined: 19 November 2003
Online Status: Offline Posts: 2206
|
Posted: 16 November 2006 at 12:36pm | IP Logged
|
|
|
Are you sure you're using the latest version of MailBee.NET.dll? You can get it from http://www.afterlogic.com/updates/mailbee_net.zip
After upgrading, please make sure the new version has taken effect. You can enable logging of MailBee session into a file (via .Log property). The MailBee version must be 3.0.0.x, not 2.0.0.x.
If it still does not work, please send us the problem message as .eml file to support at afterlogic.com for examination. Thanks!
Regards,
Alex
|
Back to Top |
|
|
webchetan Newbie
Joined: 23 October 2006
Online Status: Offline Posts: 20
|
Posted: 16 November 2006 at 1:06pm | IP Logged
|
|
|
Hi Alex - Thanks for the quick reply!
I have downloaded the ver 3.0 and this is what I am doing...on Button Click Event...please correct me if I am wrong. I am downloading attchments which are UUencoded.
Code:
Imap.LicenseKey = "MN200-70B8B8C0B852B813B8290FA3B202-8903"
Dim imp As Imap = New Imap
imp.Connect("somemail.abcxyz.com")
imp.Login("adminuser", "testpassword")
imp.SelectFolder("Inbox")
Dim uids As UidCollection = CType(imp.Search(True, "SINCE 6-Nov-2006", Nothing), UidCollection)
Dim envelopes As EnvelopeCollection = imp.DownloadEnvelopes(uids.ToString, True)
For Each envelope As Envelope In envelopes
Dim msg As MailMessage = imp.DownloadEntireMessage(envelope.Uid, True)
msg.Attachments.SaveAll("C:\_Temp") & nbsp;
Next
imp.Disconnect()
|
|
|
I know there are 3 emails since 6th Nov 2006 and all the 3 emails have .ZIP attachments which are in the BodyPart of the message and are Uuencoded.
Thank you thak you for the great help!
|
Back to Top |
|
|
Alex AfterLogic Support
Joined: 19 November 2003
Online Status: Offline Posts: 2206
|
Posted: 16 November 2006 at 1:23pm | IP Logged
|
|
|
Yes, you're doing right (although you haven't enabled log so I don't see which version of MailBee.NET is in use now).
Also, we didn't receive any mail from you with .eml attachment containing the problem mail. Please send it in the case if the problem persists in MailBee.NET 3.0.0.x.
Regards,
Alex
|
Back to Top |
|
|
webchetan Newbie
Joined: 23 October 2006
Online Status: Offline Posts: 20
|
Posted: 16 November 2006 at 1:23pm | IP Logged
|
|
|
Could someone please tell me what ma I doing wrong in the above code?
Please ....I would be really glad...
I really need to buy this product license...
Thanks
Chetan A. Sharma
|
Back to Top |
|
|
webchetan Newbie
Joined: 23 October 2006
Online Status: Offline Posts: 20
|
Posted: 16 November 2006 at 1:30pm | IP Logged
|
|
|
Here a snapshot of version I am using...
Please advice.
Thank you!
|
Back to Top |
|
|
webchetan Newbie
Joined: 23 October 2006
Online Status: Offline Posts: 20
|
Posted: 16 November 2006 at 1:39pm | IP Logged
|
|
|
Alex/Andrew - This is what the message preview exactly looks like with .ZIP attachment. Please see the Body Part carefully where it says
<b>644 newdata.zip</b>
Any help would be appreciated...
Thank you...
Thank you......
Chetan A. Sharma
|
Back to Top |
|
|
Alex AfterLogic Support
Joined: 19 November 2003
Online Status: Offline Posts: 2206
|
Posted: 16 November 2006 at 1:46pm | IP Logged
|
|
|
Please send the problem as .eml file. It's very important. We cannot reproduce the problem just from the contents of the body.
For instance, you can get this message with Outlook Express and then save it into a .EML file and attach it to your reply to support AT afterlogic.com.
Regards,
Alex
|
Back to Top |
|
|
webchetan Newbie
Joined: 23 October 2006
Online Status: Offline Posts: 20
|
Posted: 16 November 2006 at 2:05pm | IP Logged
|
|
|
Alex - I emailed you the .eml file. you should receive it from webchetan@gmail.com
Please let me know how can we download the attachment in the .EML file I sent you..
Thank you
Chetan A. Sharma
webchetan@gmail.com
|
Back to Top |
|
|
Alex AfterLogic Support
Joined: 19 November 2003
Online Status: Offline Posts: 2206
|
Posted: 16 November 2006 at 2:52pm | IP Logged
|
|
|
Yes, we received the file, thanks. Unfortunately, there are no any attachment in that file. There is "sample.zip" in Subject line but nothing else. The message body is empty (i.e. the message does not contain anything but headers section). And it's only 1KB in size.
You also attached newdata.zip but it brings "Invalid format" when I attempt to unpack it (I used a couple of zip unpackers with no luck).
Regards,
Alex
|
Back to Top |
|
|