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(); } }
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(); } };
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)