MailKit Set MessageFlag Seen after Append

2k Views Asked by At

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

2

There are 2 best solutions below

0
On BEST ANSWER

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.

2
On

Your variable te has type Nullable<UniqueId> but method AddFlagsAsync accept type UniqueId. You can use te.Value or before it check if te has value:

if (te.HasValue)
    folderSend.AddFlagsAsync(te.Value, MailKit.MessageFlags.Seen, true);