Closed alasdair-richardson closed 6 months ago
PS just adding here in issues as Discussions seems to be empty and not sure where elks to ask for help ...
"INBOX 10107 not moved to ." That means the destination is blank. Try "INBOX/Processed" 🙂
Also there's an easier way of doing this that I forgot to put in the wiki, https://github.com/danzuep/MailKitSimplified/blob/4680ab7afd2edb8468e2bcc692448ca5b1815d84/source/MailKitSimplified.Receiver/Extensions/MessageSummaryExtensions.cs#L392
await msg.MoveToAsync(destinationMailFolder, ct);
I did it this way:
private async Task SendToArchiveAsync(IMessageSummary messageSummary)
{
IMailFolder archiveFolder;
try
{
archiveFolder = await messageSummary.Folder.GetSubfolderAsync(_archiveFolderName);
}
catch (FolderNotFoundException)
{
archiveFolder = await messageSummary.Folder.CreateAsync(_archiveFolderName, isMessageFolder: true);
}
await messageSummary.MoveToAsync(archiveFolder);
}
Because of it thows exception if you try to find subfolder that not exist.
Thanks for your quick responses. I have to go out to play music now but will try these options first thing tomorrow and let you know if that is working ...
I did it this way
That's great @Sylar-A! Do you mind if I add this to the wiki?
For @alasdair-richardson I'd suggest getting or creating the folder before going into the idle loop though.
Here's that translated into code:
var destinationFolder = await GetOrCreateFolderAsync("INBOX/Processed", ct);
foreach (var msg in await imapReceiver.ReadMail.GetMessageSummariesAsync(ct))
{
var mimeMessage = await msg.GetMimeMessageAsync(ct);
// Do stuff with the message
await msg.MoveToAsync(destinationFolder, ct);
}
This is the method for getting the folder:
public async Task<IMailFolder> GetOrCreateFolderAsync(string mailFolderName, CancellationToken cancellationToken = default)
{
var client = _imapReceiver.ImapClient;
var baseFolder = client.GetFolder(client.PersonalNamespaces[0]);
folder = baseFolder.GetSubfolders(false, CancellationToken.None).FirstOrDefault(x =>
mailFolderName.Equals(x.Name, StringComparison.OrdinalIgnoreCase));
try
{
folder ??= await client.GetFolderAsync(mailFolderName, cancellationToken).ConfigureAwait(false);
}
catch (FolderNotFoundException)
{
folder = await baseFolder.CreateAsync(mailFolderName, isMessageFolder: true, cancellationToken);
}
}
I've added these methods to a pre-release version 🙂
I tried this solution this morning and it works beautifully, thanks for your help @danzuep and @Sylar-A. Incidentally Dan you suggested using folder INBOX/Processed instead of INBOX.Processed but that was what I first tried and I got MailKit.FolderNotFoundException: The requested folder could not be found. the one iwht the dot was in the list of folders returned by imapReceiver.GetMailFolderNamesAsync, so maybe my email provider does things differently. Anyway when i do this for real for the charity I am doing work for it will be on gmail which I suspect will be a whole other bundle of fun.
PS thanks for providing this great library.
Actually looking more closely at your solution I see you are calling MoveToAsync on the MimeMessage rather than the summary - but that does not appear to have that method? At least in the version I am using which seems to be 4.5.0. I based my code on Sylvans which yuses the summary.
Here's the latest version of this library: https://github.com/danzuep/MailKitSimplified/releases
I've fixed my post, getting the folder once is much more efficient than getting it every single time a new email comes in.
PS thanks for providing this great library.
Please star the repository to show your thanks!
I am trying to use MailKitSimplified to monitor an inbox, and try to process messages as they appear. Thi is wokring fine and was very easy to implement with MKS. What I want to do is move successfully processed messages to a Processed folder, but I can't seem to work out how to do it. Something like this
But the last line always gives me the error
I have tried opening the source and destination folders - but if I do that I open the source (it is open and connected ReadWrite) then the desitnation (open, readwrite) but after opening the destination folder the source is closed. It looks like in the MailKit code it should open the folders as necessary, but it always says 'not currently open in read-write mode', though I don't know which folder it is complaining about. I have also tried passing the opened destination folder to the MKS method which takes a folder, and just calling MoveToAsync on the source (INBOX) folder. Nothing seems to work, I must be doing somehting worng but no idea what.