2012-10-26 66 views
3

任何熟悉MailSystem.NET的人?MailSystem.NET IMAP4,將郵件標記爲未讀

我有一個應用程序定期檢查一個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

不熟悉它,但他們在源代碼中有一個例子。

 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(); 
+0

那麼謝謝你的!當你第二次看時,你會發現很驚人的東西:) – user1776480

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); 
} 
相關問題