Click here to Skip to main content
15,886,422 members
Articles / Desktop Programming / Win32
Tip/Trick

Download Mail Details With attachment using IMAP

Rate me:
Please Sign up or sign in to vote.
4.80/5 (7 votes)
23 Feb 2015CPOL2 min read 33.5K   8   1
This article Describes Connectivity to Gmail using IMAP, and also guide you to download and save mail content along with attachment.

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") ' For 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)' read 12th mail
     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()
' To take bcc and CC as comma seperated values
     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
' To save the attachment to particular folder
    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

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Vanilla Networks Pvt.Ltd
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionDrawback of IMAP Pin
Member 1145844024-Feb-15 9:44
Member 1145844024-Feb-15 9:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.