Click here to Skip to main content
15,890,670 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi
i have the following code the which retreives mails from inbox and mark as read, but my requirement is to move the read mails to the specified folder in gmail.

can any one provide me solution.

C#
static void Main()
        {
            using (Imap imap = new Imap())
            {
          
                imap.ConnectSSL(_server);                              
                imap.Login(_user, _password);                      
                                                                   
             
                imap.SelectInbox();                               

                List<long> uids = imap.Search(Flag.Unseen);   
                
                Console.WriteLine("Number of unseen messages is: " + uids.Count);

                foreach (long uid in uids)
                {
                    IMail email = new MailBuilder().CreateFromEml(imap.GetMessageByUID(uid));

                    ProcessMessage(email);                        
                }
                imap.Close();
            }
        }


C#
private static void ProcessMessage(IMail email)
       {
           Console.WriteLine("Subject: " + email.Subject);
           Console.WriteLine("From: " + JoinAddresses(email.From));
           Console.WriteLine("To: " + JoinAddresses(email.To));
           Console.WriteLine("Cc: " + JoinAddresses(email.Cc));
           Console.WriteLine("Bcc: " + JoinAddresses(email.Bcc));

           Console.WriteLine("Text: " + email.Text);
           Console.WriteLine("HTML: " + email.Html);

           Console.WriteLine("Attachments: ");
           foreach (MimeData attachment in email.Attachments)
           {
               Console.WriteLine(attachment.FileName);
               attachment.Save(@"c:\" + attachment.SafeFileName);
           }
       }

       private static string JoinAddresses(IList<MailBox> mailboxes)
       {
           return string.Join(",",
               new List<MailBox>(mailboxes).ConvertAll(m => string.Format("{0} <{1}>", m.Name, m.Address))
               .ToArray());
       }

       private static string JoinAddresses(IList<MailAddress> addresses)
       {
           StringBuilder builder = new StringBuilder();

           foreach (MailAddress address in addresses)
           {
               if (address is MailGroup)
               {
                   MailGroup group = (MailGroup) address;
                   builder.AppendFormat("{0}: {1};, ", group.Name, JoinAddresses(group.Addresses));
               }
               if (address is MailBox)
               {
                   MailBox mailbox = (MailBox) address;
                   builder.AppendFormat("{0} <{1}>, ", mailbox.Name, mailbox.Address);
               }
           }
           return builder.ToString();
       }
   };
Posted

1 solution

Go through below article may it will help you

Read_Emails[]
 
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