2

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

Wolfgang Schorge
  • 157
  • 1
  • 10

2 Answers2

6

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.

jstedfast
  • 35,744
  • 5
  • 97
  • 110
2

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);
  • 1
    Your solution is correct, but there is an even easier solution since the Append/AppendAsync methods take an optional MessageFlags argument as well, thereby eliminating the need for calling AddFlags or AddFlagsAsync :-) – jstedfast Nov 22 '18 at 14:39
  • te.value is coming as {462} for me. Is it correct format? and Do i have to set true or false in 3rd parameter? – Mit Jacob May 23 '23 at 17:59