MailKit delete Emails Xamarin C# - c#

I am using mailkit on monotouch xamarin. I am creating an app that will receive emails(email client). I give to the user the option to choose if he is using Pop3 or IMAP connection protocol. My issue is that I cant find solution on how he can delete a message on Pop3 and on IMAP.
I have tried to use this code:
client.Inbox.AddFlags (new int[] { index }, MessageFlags.Deleted);
from this post: MailKit Delete single message from gmail
but is not seems to work for me.
My code for capturing the Pop3 acount emails is
using (var client = new Pop3Client ()) {
var credentials = new NetworkCredential (Convert.ToString (username), Convert.ToString (password));
var uri = new Uri (Convert.ToString ("pops://"+pop3));
using (var cancel = new CancellationTokenSource ()) {
client.Connect (uri, cancel.Token);
var _emailItems=new List<EmailItem>() ;
client.Authenticate (credentials, cancel.Token);
string[] mycell = new string[200];
int count = client.GetMessageCount (cancel.Token);
int lastcount;
for (int i = 0; i < count; i++) {
lastcount = (count - 1) - i;
var message = client.GetMessage (lastcount, cancel.Token);
}
}
}

Different protocols have different ways of deleting messages.
For POP3, this is how you would delete a message:
client.DeleteMessage (lastcount, cancel.Token);
(Note: unless you are actually allowing the user to cancel the operations, you do not need to use cancel.Token)
The other way of deleting messages that you pasted is meant for IMAP.

Related

The IMAP server replied to the 'NAMESPACE' command with a 'BAD' response: User is authenticated but not connected

I am trying to connect into Outlook mailbox, fetching, extracting data from mails and working with them in DataGrid. My problem is, that after few seconds(around 30seconds) my app gets freeze and will show this exception
The IMAP server replied to the 'NAMESPACE' command with a 'BAD'
response: User is authenticated but not connected.
I have read here in stackoverflow, that this is caused by more loggins, that Microsoft allows(more than 20, I think).
My Question is: How to fix it?
My app is in development, so I didn't think for OAUTH2.0 verification. But could be this the solution?
Thanks for any advice.
This is how I am using Mailkit library
public static void MailKitLib(EmailParser emailParser)
{
bool help = true;
do
{
using (var client = new ImapClient())
{
using (var cancel = new System.Threading.CancellationTokenSource())
{
client.Connect(emailParser.ServerName, emailParser.Port, emailParser.IsSSLuse,
cancel.Token);
client.Authenticate(emailParser.Username, emailParser.Password, cancel.Token);
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadOnly, cancel.Token);
for (int i = 0; i < inbox.Count; i++)
{
var message = inbox.GetMessage(i, cancel.Token);
GetBodyText = message.TextBody;
Problem problem = new Problem(message.MessageId);
if (!dAOProblem.GetAll().Any(x => x.Message_Id.Equals(problem.Message_Id)))
{
dAOProblem.Save(problem);
Alert alert = new Alert(message.MessageId, message.Date.DateTime, message.From.ToString(), 1, problem.Id);
if (!dAOAlert.GetAll().Any(x => x.Id_MimeMessage.Equals(alert.Id_MimeMessage)))
{
dAOAlert.Save(alert);
}
}
}
//client.Disconnect(true, cancel.Token);
}
}
} while (help != false);
}
This is a weird state that Exchange gets into. Search StackOverflow for similar questions. I know it's been answered before (even if not for MailKit specifically).
The error means that you used the correct username and password but your user account does not have access permissions to email over the IMAP protocol.

How deal with MailKit.Security.AuthenticationException: 'LOGIN failed.'

I'm trying to read an email from my mailbox using the MailKit library. Unfortunately the program throws this MailKit.Security.AuthenticationException: 'LOGIN failed.'
The credentials match (I tried to log in through the browser).
I'm trying to read an email from my mailbox using the MailKit library. Unfortunately the program throws this
MailKit.Security.AuthenticationException: 'LOGIN failed.'
I would be grateful for any help, I don't know what to do. I googled, tried this client.Connect("imap.outlook.com", 993, SecureSocketOptions.None);
But nothing came up
when I changed SecureSocketOptions to Auto, the same exception was thrown
using (var client = new ImapClient())
{
using (var cancel = new CancellationTokenSource())
{
client.Connect("imap.outlook.com", 993, true);
//client.AuthenticationMechanisms.Remove("XOAUTH");
client.Authenticate("mail#test.cz", "password", cancel.Token);
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadOnly, cancel.Token);
Console.WriteLine("Total messages: {0}", inbox.Count);
Console.WriteLine("Recent messages: {0}", inbox.Recent);
for (int i = 0; i < inbox.Count; i++)
{
var message = inbox.GetMessage(i, cancel.Token);
Console.WriteLine("Subject: {0}", message.Subject);
}
var query = SearchQuery.DeliveredAfter(DateTime.Parse("2013-01-12"))
.And(SearchQuery.SubjectContains("MailKit"))
.And(SearchQuery.Seen);
foreach (var uid in inbox.Search(query, cancel.Token))
{
var message = inbox.GetMessage(uid, cancel.Token);
Console.WriteLine("[match] {0}: {1}", uid, message.Subject);
}
client.Disconnect(true, cancel.Token);
}
}
The AuthenticationException has nothing to do with SSL and therefor is not a problem with client.Connect(...). It's a problem with client.Authenticate (username, password)
Microsoft's public email servers no longer allow the use of usernames and passwords. They now require authentication via OAuth2.
For information about how to authenticate using OAuth2, see the MailKit docs: https://github.com/jstedfast/MailKit/blob/master/ExchangeOAuth2.md

MailKit - ImapClient() .search() does not read EVERY emails C#

I'm using the MailKit client librairy to use their IMAP protocol in order to read my mails and search in it for a specific mail. It does work well for recent mail (less than a month and a half). But when I'm trying to search for an email that contains "myWordToSearch" from a long time (6 months old or stuff like that) it tells me that it does not exist, because it does not go that deep into the mails. Is there a way to retrieve/read ALL emails in order to filter them (like the search bar on gmail or stuff like that) ?
Here is my code :
using (var client = new ImapClient())
{
using (var cancel = new CancellationTokenSource())
{
client.Connect("imap." + provider , 993, true, cancel.Token);
client.Authenticate(myMail, myPassword, cancel.Token);
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadOnly, cancel.Token);
var mail = (SearchQuery.SubjectContains(myWordToSearch));
foreach (var uid in inbox.Search(mail, cancel.Token))
{
var message = inbox.GetMessage(uid, cancel.Token);
}
}
}

MailKit: throws Authentication Failed while calling Authenticate method from the Pop3Client class

I am using MailKit for sending and receiving emails. but when I try to get emails, I get the Authentication Failed error. here is my code:
using MailKit.Net.Pop3;
using MimeKit;
public List<Email> GetEmails()
{
using var emailClient = new Pop3Client();
emailClient.Connect("pop.mail.yahoo.com", 995, true);
emailClient.AuthenticationMechanisms.Remove("XOAUTH2"); //commenting this line won't help.
emailClient.Authenticate("email", "password"); // this line throws 'Authentication Failed' exception.
var emails = new List<Email>();
for (int i = 0; i < 10; i++)
{
var message = emailClient.GetMessage(i);
var emailMessage = new Email
{
Content = message.TextBody,
Subject = message.Subject
};
emails.Add(emailMessage);
}
return emails;
}
Does anyone have any idea?
I know this sounds obvious, but the odds are that you're providing the wrong username and/or password. That's what the error is saying.
You can verify this by looking at the log file:
var emailClient = new Pop3Client(new ProtocolLogger("pop3.log"))
Maybe you can try creating a new Yahoo! Mail account that fails to authenticate just like your real account and give me the username/password for that new account so that I can debug the issue?
Does your username or password contain non-ASCII characters? How about punctuation characters?
If you want me to dig into this, I need to know what the username/password are and what MailKit is sending in case it's sending the wrong strings (pretty sure it's not, but I'll look into it anyway). If you don't give me that info, I can't diagnose the issue.

How to get mail sent successfully with MailKit IMap?

I'd like to know if my outbox mail has been sent successfully..
var client = new ImapClient();
....
var folders = client.GetFolders(client.PersonalNamespaces[0]);
var folder = client.GetFolder("已发送");//get sent mail floder in Chinese
var folderAccess = folder.Open(FolderAccess.ReadOnly);
string path = #"C:\temp\";
for (int i = folder.Count - 1; i >= 0; i--)
{
var message = folder.GetMessage(i);
}
You can't send messages via IMAP, you can only send them via SMTP.
Sending messages via SMTP do not put them into any IMAP folder. You have to put them there yourself.

Categories

Resources