MailSystem.NET IMAP4,将邮件标记为未读

时间:2012-10-26 08:24:38

标签: c# email

有谁熟悉MailSystem.NET?

我有一个应用程序会定期检查gmail帐户是否有新邮件。如果主题标题包含特定短语,则采取动作。但是,我需要稍微修改应用程序以将某些邮件标记为未读。

这是现有的代码。单击按钮会调用logInLogOut()子并启动一个计时器,该计时器通过调用另一个线程中的checkNewMail()子来定期检查新邮件。该应用程序按预期工作,但下面可能不是最佳方式。

private void logInLogOut()
{
    try
    {
        Client.ConnectSsl(txtIMAPServer.Text, int.Parse(txtIMAPPort.Text));
        Client.Login(@txtUserName.Text, txtPassword.Text); 
        globalClientConnected = true;

    }
    catch (Exception ex)
    {
        globalClientConnected = false;

    }
}    


private void checkNewMail()
{
    if (globalClientConnected)
    {
        foreach (ActiveUp.Net.Mail.Message email in GetUnreadMails("Inbox"))
        {
            string from = parseEmailAddress(email.From.ToString());
            string subject = email.Subject;
            string receivedDateTime = email.ReceivedDate.Date.ToString()

            string updateString = receivedDateTime + ", " + from + ", " + subject + "\r\n";

            if (subject.Contains("ABC"))
            {
                string to = from;

                try
                {              
                    //do something
                }
                catch (Exception ex)
                {
                    //bla bla
                }
            }
            else
            {
                //If mail subject not like "ABC"
                //Do something else

                //Mark the mail as unread
            }
        }


    }


}

2 个答案:

答案 0 :(得分:2)

不熟悉它,但他们的源代码中有一个例子。

        Imap4Client imap = new Imap4Client();
        imap.Connect("mail.myhost.com");
        imap.Login("jdoe1234","tanstaaf");
        Mailbox inbox = imap.SelectInbox("inbox");
        FlagCollection flags = new FlagCollection();
        flags.Add("Read");
        flags.Add("Answered");
        inbox.AddFlags(1,flags);

//Message is marked as read and answered. All prior flags are unset.
        imap.Disconnect();

答案 1 :(得分:0)

诀窍是取消设置“看见”标志。有一种删除标志的方法:RemoveFlags()。您只需要要从中删除标记的消息的ID。

var imap = new Imap4Client();
imap.ConnectSsl(hostname, port);
imap.Login(username, password);

var inbox = imap.SelectMailbox("inbox");
var ids = inbox.Search("UNSEEN");
foreach (var messageId in ids)
{
        var message = inbox.Fetch.MessageObject(messageId);
        // process message

        var flags = new FlagCollection { "Seen" };
        inbox.RemoveFlagsSilent(messageId, flags);
}