Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I want to programmatic-ally connect(using c#) to Gmail for retrieving e-mails details with their attachment.
After successfully connected, how to open one by one email from Gmail and checking if mail contain attachment then download this attachment programmatic-ally and save in local machine.

1)How I will do this?
2)If any reference code available please send to me?


here is my sample code ,
C#
using Microsoft.Office.Interop.Outlook;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MailApplication
{
    class Program
    {
        static void Main(string[] args)
        {
       //     Microsoft.Office.Interop.Outlook.MAPIFolder inbox =
       //    Application.ActiveExplorer().Session.GetDefaultFolder
       //(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);

       //     Microsoft.Office.Interop.Outlook.Items unreadItems = inbox.
       //         Items.Restrict("[Unread]=true");

       //     MessageBox.Show(
       //         string.Format("Unread items in Inbox = {0}", unreadItems.Count));


            SmtPop.POP3Client pop = new SmtPop.POP3Client();

            pop.Open("pop.gmail.com",995, "abc@gmail.com", "abdre@345");
            
           
            // Get message list from POP server
            
            SmtPop.POPMessageId[] messages = pop.GetMailList();
            
            if (messages != null)
            {

                // Walk attachment list
                foreach (SmtPop.POPMessageId id in messages)
                {
                    SmtPop.POPReader reader = pop.GetMailReader(id);
                    SmtPop.MimeMessage msg = new SmtPop.MimeMessage();

                    // Read message
                    msg.Read(reader);
                    if (msg.AddressFrom != null)
                    {
                        String from = msg.AddressFrom[0].Name;
                        Console.WriteLine("from: " + from);
                    }
                    if (msg.Subject != null)
                    {
                        String subject = msg.Subject;
                        Console.WriteLine("subject: " + subject);
                    }
                    if (msg.Body != null)
                    {
                        String body = msg.Body;
                        Console.WriteLine("body: " + body);
                    }
                    //string attach1 = Convert.ToString(msg.Attachments);
                    //Console.WriteLine(attach1);
                    if (msg.Attachments != null )
                    {
                        // Do something with first attachment
                        SmtPop.MimeAttachment attach = msg.Attachments[0];
                       
                        if (attach.Filename == "")
                        {
                            // Read data from attachment
                            Byte[] b = Convert.FromBase64String(attach.Body);
                            System.IO.MemoryStream mem = new System.IO.MemoryStream(b, false);

                            //BinaryFormatter f = new BinaryFormatter();
                            // DataClass data= (DataClass)f.Deserialize(mem); 
                            mem.Close();
                        }

                        // Delete message
                        // pop.Dele(id.Id);
                    }
                }
            }
            pop.Quit();
        }
    }
}

Thank You,
Nitin
Posted
Updated 18-Nov-13 4:03am
v4
Comments
Richard MacCutchan 18-Nov-13 11:40am    
Please do not edit your question to bump it up the queue. I have given you a link to a solution that answers the question; go and read it.

1 solution

 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900