user1776480
user1776480

Reputation: 369

MailSystem.NET IMAP4, marking mail as unread

Anyone familiar with MailSystem.NET?

I have an app which periodically checks a gmail account for new mail. If the subject title contains a specific phrase then an action is taken. However I need to modify the app a little to mark certain messages as unread.

Here is the existing code. A button click calls the logInLogOut() sub and starts a timer which takes care of the app periodically checking for new mail by calling the checkNewMail() sub in another thread. The app works as intended though the below may not be the best way of doing it.

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
            }
        }


    }


}

Upvotes: 3

Views: 8257

Answers (2)

Lars
Lars

Reputation: 657

The trick is to unset the "Seen" flag. There is a method to remove flags: RemoveFlags(). You just need the id of the message you want to remove flags from.

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);
}

Upvotes: 0

AbdElRaheim
AbdElRaheim

Reputation: 1394

Not familiar with it but they have an example in their source code.

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

Upvotes: 2

Related Questions