Comments (11)
I recently saw a ticket with the same error in the MailKit repository, maybe this will somehow help in solving the problem
from mailkitsimplified.
Hi @q00Dree, thanks for taking the time to post about this issue.
The IMAP receiver can only have one call at a time (it's not re-entrant) so this is a bug. I thought it'd be a quick and easy fix by doing a deep copy of the receiver in the idle client, but not sure yet why it's not working for me yet. Thanks for pointing it out!
In the mean-time, do something like this as a work-around (it does the same thing as your code):
// Don't do this in real code! Use Dependancy Injection instead.
using var imapReceiver1 = ImapReceiver.Create("imapHost:imapPort")
.SetCredential("login", "applicationPassword");
using var imapReceiver2 = ImapReceiver.Create("imapHost:imapPort")
.SetCredential("login", "applicationPassword");
await imapReceiver1.MonitorFolder
.SetIgnoreExistingMailOnConnect(false)
.OnMessageArrival(OnArrivalAsync)
.IdleAsync(cancellationToken);
async Task OnArrivalAsync(IMessageSummary messageSummary)
{
try
{
Console.WriteLine($"{imapReceiver1} message #{messageSummary.UniqueId} arrived.");
var mimeMessage = await imapReceiver2.ReadMail.GetMimeMessageAsync(messageSummary.UniqueId);
}
catch (Exception ex)
{
Console.WriteLine(ex, ex.Message);
}
}
from mailkitsimplified.
If you use Dependancy Injection it becomes much simpler. Refer to the Samples folder examples.
await _mailFolderMonitor
.OnMessageArrival(OnArrivalAsync)
.IdleAsync(cancellationToken);
async Task OnArrivalAsync(IMessageSummary messageSummary)
{
try
{
_logger.LogInformation($"{_imapReceiver} message #{messageSummary.UniqueId} arrived.");
var mimeMessage = await _imapReceiver.ReadMail.GetMimeMessageAsync(messageSummary.UniqueId);
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
}
}
from mailkitsimplified.
I've got a simple deep copy fix working for the idle client, but it won't work for var mimeMessage = await messageSummary.GetMimeMessageAsync();
as that uses a readonly MailFolder assigned by MailKit in a Fetch function. I'll keep looking.
from mailkitsimplified.
Oh, got you.
Thanks for samples. I keep several idle clients for different emails at the same time, so I have to create them without using DI.
from mailkitsimplified.
I got await messageSummary.GetMimeMessageAsync();
working as well now in a new branch.
from mailkitsimplified.
Update to the latest version to get the fix.
from mailkitsimplified.
Thank you for the quick solution. But I wonder, how it works in MailKit ImapIdle using one ImapClient for idling and fetching. Or I miss something?
from mailkitsimplified.
This code from sample works without any problem
async Task FetchMessageSummariesAsync(bool print)
{
IList<IMessageSummary> fetched;
do
{
try
{
// fetch summary information for messages that we don't already have
int startIndex = messages.Count;
fetched = client.Inbox.Fetch(startIndex, -1, request, cancel.Token);
break;
}
catch (ImapProtocolException)
{
// protocol exceptions often result in the client getting disconnected
await ReconnectAsync();
}
catch (IOException)
{
// I/O exceptions always result in the client getting disconnected
await ReconnectAsync();
}
} while (true);
foreach (var message in fetched)
{
if (print)
{
var mimeMessage = await client.Inbox.GetMessageAsync(message.UniqueId);
Console.WriteLine("{0}: new message: {1}", client.Inbox, mimeMessage.TextBody);
}
messages.Add(message);
}
}
from mailkitsimplified.
It's a good idea but it could raise some other issues.
- It relies on exception handling to run, normally not a good practice.
- If anyone tries to download the IMessageSummary returned using the internal IMailFolder it will throw another exception.
from mailkitsimplified.
I keep several idle clients for different emails at the same time, so I have to create them without using DI.
This could be a good use-case for the Factory Method Design Pattern. Please let me know if you'd like to make one and submit a merge request.
from mailkitsimplified.
Related Issues (20)
- Reconnect and reopen for _fetchReceiver and _fetchFolder HOT 13
- MailFolderReader disposing HOT 4
- How to connect STARTTLS enabled server? HOT 6
- Use Flags - Follow up #9 HOT 10
- Add MoveMessagesAsync to ImapReceiver
- Add an Exception template and check DefaultFrom usage HOT 15
- Special Folder Monitoring HOT 7
- MailKit Dependency Injection With Quartz HOT 6
- Arithmetic Operation Overflow HOT 17
- .net Core MVC Dependency Injection Issue HOT 3
- Download Mime Message without Attachment HOT 10
- GetMessageSummariesAsync in v2.5.6 takes a long time HOT 3
- Monitor multiple emails HOT 4
- ReplyTo Issue HOT 3
- Storing mail in Draft folder HOT 3
- IMAP Oauth2 for O365 HOT 11
- SecureSocketOptions are not transmitted to ConnectAsync method HOT 2
- How can I see conversations in Mail? HOT 3
- can support a dynamic template? HOT 3
- Remove obsolete methods
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mailkitsimplified.