Introduction
This article Describes Connectivity to Gmail using IMAP, and also guide you to download and save mail content along with attachment. This includes the following contents:
1. Description about IMAP
2. Drawbacks of POP
3. How to connect to Gmail using IMAP
4. Extract mail details
5. Save attachments to lacal
Background
Description about IMAP
Internet Message Access Protocol(IMAP) allows you to download messages from Gmail's servers onto your computer so you can access your mail from your application. This protocol is developed by Mark Crispin in 1986 at Stanford University. IMAP can be an alternative to POP. IMAP allows simultaneous access to same mailbox by multiple clients. these are achieved through flags stored on the server.
Drawbacks of POP
- You have to delete or file the same email on every device
- Logging into each device, you will see lots of unread emails with no indication of which you deleted, read, flagged or filed
- Any folders you created and organize on one device won't be replicated on the other devices.
How to connect to Gmail using IMAP
To connect with a gmail account using IMAP. You need a valid email account and password. Now you can download mail.dll from here. To work with IMAP you need to add these reference.
step 1 : in your solution imports the .dll using Imports ActiveUp.Net.Mail
step 2 : Declare and initialize an IMAP client
Dim imapClient As New Imap4Client()
imapClient.ConnectSsl("Imap.gmail.com", 993)
' where Imap.gmail.com is host and 993 is port number
step 3 : Now you can login to your mail account by giving a valied mail id and its password. uing this code
imap.Login("yourMailid", "mailpassword")
Or in a better way you can use this function for login as follows:
Private Function TryToLoginToImap(ByRef imap As Imap4Client, ByVal lsemailid As String, ByVal lsemailpassword As String, Optional ByVal tryAgain As Boolean = True) As Boolean
Try
imap.Login(lsemailid, lsemailpassword)
Catch imapException As Imap4Exception
Return False
End Try
Return True
End Function
You have to pass : 1. Imap client you have created earlier 2. gmail id 3. passowrd to this function to login. this will return True
for a successfull login and False
for a failure.
step 4: After successfull login you have to load the mail boxes, and then select the mail box from which mails have to fetch.
imapClient.LoadMailboxes()
Dim myMailbox As Mailbox = imapClient.SelectMailbox("Inbox") Dim count As Integer = myMailbox.MessageCount
For fetching sent items you have to give
imapClient.SelectMailbox("[Gmail]/Sent Mail")
For fetching a particular mail details you can use the following code
Extract mail details and Save attachments to lacal:
Dim mailMsg As Message =myMailbox .Fetch.MessageObject(12) Dim unreadMsg As String = mailMsg .Flag
Dim attachCount As Integer= mailMsg .Attachments.Count
Dim mailDate As Date = CDate(mailMsg.Date)
Dim mailSubject = mailMsg .Subject.ToString
Dim mailAddressTo = mailMsg .To.Item(0).ToString
Dim mailBodyContent = mailMsg .BodyHtml.Text.ToString
Dim cc() As ActiveUp.Net.Mail.Address = mailMsg .Cc.ToArray()
Dim bcc() As ActiveUp.Net.Mail.Address = mailMsg .Bcc.ToArray()
Dim mail_cc As String = Nothing
Dim mail_bcc As String = Nothing
Dim msg_gid As String = Nothing
For Each Address As ActiveUp.Net.Mail.Address In cc
mail_cc &= Address.ToString
Next
For Each Address As ActiveUp.Net.Mail.Address In bcc
mail_bcc &= Address.ToString
Next
If msg.Attachments.Count <> 0 Then
If System.IO.Directory.Exists("../email_attachment/" ) = False Then
System.IO.Directory.CreateDirectory("../email_attachment/")
msg.Attachments.StoreToFolder("../email_attachment/")
End If
End If
Points of Interest
1. In such a similar way i can fetch the mails in the sent items/draft/spam folders.
2. Points to notice that you must enable IMAP in your account settings. else you cannot be able to login to the account