Search code examples
c#imapmailkitmime-messageunique-id

MailKit Set MessageFlag Seen after Append


I send a MailKit.Message Async with the MailKit.Net.Smtp.SmtpClient.

Then i put the Mail in the send Folder, but the Message Flag is Unseen.

I can't set the messageflag in Message build, only after Append, but i found no way to convert the MailKit.UniqueId? to MailKit.UniqueId.

 var folderSend = IC.GetFolder(MailKit.SpecialFolder.Sent);
 MailKit.UniqueId? te = folderSend.Append(nochmalMessage);
 folderSend.AddFlagsAsync(te, MailKit.MessageFlags.Seen, true);

te must be MailKit.UniqueId


Solution

  • The Append() and AppendAsync() methods each have an overload that takes a MessageFlags argument. This allows you to simplify your logic down to:

    folder.Append (message, MessageFlags.Seen);
    

    or

    await folder.AppendAsync (message, MessageFlags.Seen);
    

    This eliminates the need to even bother calling AddFlags() or AddFlagsAsync() with the flags you want to set on the appended message because it'll set those flags as it appends the message.