3

任何熟悉 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
            }
        }


    }


}
4

2 回答 2

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();
于 2012-10-26T08:42:38.570 回答
0

诀窍是取消设置“Seen”标志。有一种删除标志的方法: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);
}
于 2014-06-06T14:59:00.470 回答