如何将gmail消息标记为未读(看不见)MailSystem.NET

时间:2013-06-19 00:33:26

标签: c# .net imap mailsystem.net

我搜索了很多如何从gmail阅读电子邮件然后将其标记为未读(看不见),现在我发现我想与所有人分享。

2 个答案:

答案 0 :(得分:5)

使用库http://mailsystem.codeplex.com/

来源:http://mailsystem.codeplex.com/discussions/269058

添加引用:activeup.net.common,activeup.net.imap4,activeup.net.mail

代码:

        Imap4Client imap = new Imap4Client();
        imap.ConnectSsl("imap.gmail.com", 993);
        imap.Login("aaaaa@gmail.com", "xxxxxxx");

        imap.Command("capability");

        Mailbox inbox = imap.SelectMailbox("inbox");
        int[] ids = inbox.Search("UNSEEN");
        if (ids.Length > 0)
        {
            ActiveUp.Net.Mail.Message msg_first = inbox.Fetch.MessageObject(ids[0]);

            //ignore this gmail_data stuff // undefined in this scope // checking to make sure it's a "new" unread msg
            //if (gmail_data != msg_first.Date.ToString())
            //{
               // gmail_data = msg_first.Date.ToString();

                XElement xmail = new XElement("gmail",
                    new XAttribute("count", ids.Length.ToString()),
                    new XAttribute("modified", msg_first.Date.ToString())
                );

                string name = "", address = "", from = "";
                Regex reg_name = new Regex("\"[^\"]+");
                Regex reg_address = new Regex("<[^>]+");

                ActiveUp.Net.Mail.Message msg = null;

                for (var i = 0; i < ids.Length; i++)
                {
                    msg = inbox.Fetch.MessageObject(ids[i]);

                    from = msg.HeaderFields["from"];
                    name = reg_name.Match(from).Value.Replace("\"", "");
                    address = reg_address.Match(from).Value.Replace("<", "");

                    xmail.Add(new XElement("entry",
                        new XAttribute("id", msg.MessageId),
                        new XAttribute("modified", msg.Date.ToString()),
                        new XAttribute("name", name),
                        new XAttribute("address", address),
                        new XElement("subject", msg.Subject),
                        new XElement("body-text", msg.BodyText.TextStripped),
                        new XElement("body-html", msg.BodyHtml.Text)
                    ));
                    //mark as unread
                    var flags = new FlagCollection();
                    flags.Add("Seen");
                    inbox.RemoveFlags(ids[i], flags);
                }


                File.WriteAllText("gmail.xml", xmail.ToString());


            }
        }

答案 1 :(得分:0)

  • 当您从服务器获取所有电子邮件时,API会自动将所有电子邮件标记为“已读/已读”,我认为无法使用messageId过滤的“搜索方法” ,因此您必须获取所有电子邮件,以将一封(已看到)电子邮件标记为未读。
  • 您应该始终获得“未读消息”,并在列表中添加要标记为“未读”的电子邮件的messageId,然后必须将该列表传递给以下方法将列表中的所有消息标记为未读。

可能的代码:

    public void MarkAsUnread(List<string> messageIdList)
    {
        Mailbox inbox = Client.SelectMailbox("inbox");
        int[] ids = inbox.Search("ALL");
        int ListCount = messageIdList.Count;
        int MarkedAsUnread = 0;

        if (ids.Length > 0)
        {
            ActiveUp.Net.Mail.Message msg = null;
            for (var i = 0; i < ids.Length; i++)
            {
                msg = inbox.Fetch.MessageObject(ids[i]);
                // if messageId is on the list, mark as unread.
                if (String.Join(",", messageIdList).Contains(msg.MessageId))
                {                        
                    var flags = new FlagCollection { "Seen" };
                    inbox.RemoveFlagsSilent(i+1, flags);
                    MarkedAsUnread = MarkedAsUnread + 1;
                }

                // optimization
                if (MarkedAsUnread == ListCount)
                {
                    break;
                }
            }
        }            
    }

*如果您真的不想全部获取,也许可以使用如下代码按日期进行过滤:

var box = imap.SelectMailbox("inbox");
var ids = box.Search("OR (CC @cc.lieser-online.de) (HEADER Envelope-To @cc.lieser-online.de)");