-
Hey @jstedfast , I know this question was somehow answered in #338, but it's very old and closed and I stumbled over this issue this week and I might miss here something. Currently I implement a feature to move processed emails to an archive folder. I open the INBOX folder and get all messages in it. After that I am storing relevant information in a dictionary, so I later have the relation between the MessageId and the UID. After some other business logic I am trying to update and move the processed e-mails into an archive folder and additionally add a header information (as a "flag" to be sure this mail was processed). Now I am currently struggling to update the message and afterwards move it - my current situation is, that the // the folder is 'INBOX'
await folder.OpenAsync(FolderAccess.ReadWrite, _token);
// Get all uids in the inbox folder
var uidList = await folder.SearchAsync(SearchQuery.All, _token);
foreach (var id in uidList)
{
// get the MimeMessage
var message = await folder.GetMessageAsync(id, _token);
// ... some other stuff happening here
// add the MessagId and the uid to a dictionary
_mailDictionary.TryAdd(messageId, id);
}
// create the archive folder
var toplevelFolder = _mailClient.GetFolder(_mailClient.PersonalNamespaces[0]);
_archiveFolder = await toplevelFolder.CreateAsync("Archive", true, _token);
// ... lots of logic happens now in other classes / files etc.
// now assuming processing was successful I want to flag it and move it
message.Headers.Add("X-UNIQUE-FLAG", "processed");
// update the message with the new header element
// Here is the issue - it does not actually update the message, but creates a new Email element in the INBOX folder
var uid = await folder.AppendAsync(message, MessageFlags.Seen, _token);
// move message from inbox to archive folder
// Here it's moving the original one to the archive folder, but as the AppendSync above creates a duplicate, it stays in the INBOX
await folder.MoveToAsync(_mailDictionary[message.MessageId], _archiveFolder, _token); In the other issue mentioned at top - you gave the hint, that I can update the message header and then append it directly to the new folder. Is this still the way to go? And to ensure that the Email is gone from the I am using MailKit |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Welll I found #985 - guess this answers my question - if not please reopen it with further tipps. Thanks anyways. |
Beta Was this translation helpful? Give feedback.
-
Hi @rogatec Yes, you can download the message, add a custom header to it, and then Append the message to the Archive folder, but as your comment above the IMAP has a feature called "keywords" (which are just custom There are 2 ways to figure out if an IMAP server supports setting (persistent) custom keywords on a message:
if (folder.PermamentFlags.HasFlag (MessageFlags.UserDefined)) {
...
} Assuming that the PermanentFlags allows setting custom defined keywords, you can set a custom keyword like this: // The newer API is ImapFolder.Store(...)
// Silent=true just means that we don't need the server to send us a notification that the message keywords changed
// as a result of this Store request.
var request = new StoreFlagsRequest (StoreAction.Add, MessageFlags.None) {
Silent = true
};
request.Keywords.Add ("$MyProcessedKeyword");
await folder.StoreAsync (uid, request, _token); See Also:
Another way using the older API (which in MailKit v4.x uses an Extension method) would look like this: var keywords = new HashSet<string> (new [] { "$MyProcessedKeyword" });
await folder.AddFlagsAsync (uid, MessageFlags.None, keywords, true, _token); See Also:
Custom keywords, by convention at least, typically start with a Hopefully that helps you decide how to proceed with your implementation. |
Beta Was this translation helpful? Give feedback.
Hi @rogatec
Yes, you can download the message, add a custom header to it, and then Append the message to the Archive folder, but as your comment above the
AppendAsync
suggests, it does not "update" the existing message on the server. Instead, it appends a brand new message to the folder.IMAP has a feature called "keywords" (which are just custom
MessageFlags
) that might be better for this kind of thing because it would require less work to implement (assuming the IMAP server allows custom keywords - which most do these days).There are 2 ways to figure out if an IMAP server supports setting (persistent) custom keywords on a message:
S…