MailKit Delete single message from gmail - c#

I am using MailKit (https://github.com/jstedfast/MailKit) to connect to google apps via imap, how can I delete a single message though ? (I am fine to have it moved to trash, just need it out of the inbox.
So far I have it connected, downloading, parsing links from message bodies. I just need this one last action to have what I need.
Thanks!

To delete a message from a folder on the IMAP server, this is all you need to do:
client.Inbox.AddFlags (new int[] { index }, MessageFlags.Deleted);
or
client.Inbox.AddFlags (new UniqueId[] { uid }, MessageFlags.Deleted);
Now the message is marked as \Deleted on the server.
You can then purge the folder of all deleted items by calling:
client.Inbox.Expunge ();
If you are using UIDs instead of indexes and the IMAP server supports the UIDPLUS extension (check the client.Capabilities), you can expunge just a selected set of messages like this:
if (client.Capabilities.HasFlag (ImapCapabilities.UidPlus))
client.Inbox.Expunge (new UniqueId[] { uid });

Related

Fetched Emails not ordered when I use pop3

First I were Fetching emails by POP3 using this library OpenPop.Pop3 and it was working ok and it was returns emails ordered from last email to first email
but when I change the library to mailkit library the returned messages not ordered and couldn't know based on what mailkit order fetched emails
that's my code after I change to mailkit library
using (Pop3Client client = new Pop3Client())
{
// Connect to the server
client.Connect(hostname, port, useSsl);
client.AuthenticationMechanisms.Remove("XOAUTH2");
client.Authenticate((username), password);
int messageCount = client.Count;
// We want to download all messages
List<MimeMessage> allMessages = new List<MimeMessage>(messageCount);
for (int i = messageCount-1; i > 0; i--)
{
var msg = client.GetMessage(i);
allMessages.Add(msg);
}
}
by this way allmessages variable should contains emails ordered from last email to first email but that's not happened emails not ordered at all
although I were using the same authenticated email before with OpenPop.Pop3 and fetched emails were ordered
I don't know why they wouldn't be ordered for you since MailKit is not doing any kind of sorting.
That said, MailKit uses 0-based indexes while I suppose OpenPOP.NET must have used 1-based indexes, so your loop should make the following change:
for (int i = messageCount-1; i >= 0; i--)
{
var msg = client.GetMessage(i);
allMessages.Add(msg);
}
Perhaps this will produce the expected results?
Update: It turns out that MailKit was correctly downloading the messages in reverse order just as his code was trying to do (as mentioned in his follow-up question). The problem this user was facing is that his GMail account settings were only providing MailKit's Pop3Client with a subset of his total Inbox as is explained in Google's FAQ in the section titled "Emails aren't downloading correctly", where it states:
After you set up POP in your Gmail settings, your emails become available
in batches. It might take a while to see all your emails.
Note: Gmail downloads a copy of every email you send or receive, except
for emails in Chats, Spam, and Trash. To avoid duplicates, Gmail doesn't
download emails sent within your mail client, but you can still see them
if you log in to Gmail.
If you continue to have problems downloading emails, try using recent
mode:
In your email client's POP settings page, find the "Email address" or
"User name" field.
Add recent: in front of your email address. For example,
recent:example#gmail.com.
If that doesn't fix the problem, try deleting your Gmail address from your email client, then re-adding it.

C# Exchange - Forward message As

I'm using C# for an Exchange Online related utility. I authenticate to the server using an admin account, and that account performs some tasks on user's messages in specific folders.
If I want to send a message from a user (as opposed to the admin account in the From field) I can do this:
EmailMessage confirmationMessage = new EmailMessage(service);
confirmationMessage.ToRecipients.Add("recipient#contoso.com");
confirmationMessage.Subject = "Confirmation: " + message.Subject + " Sent";
confirmationMessage.From = "testuser#contoso.com";
confirmationMessage.Send();
And the message is sent with "testuser#contoso.com" in the From field instead of "admin#contoso.com" (the account used to authenticate to Exchange Online). This of course requires 'Send As' permissions, which have been granted.
However, if I want to forward a message out of a user's mailbox instead of sending a new message "admin#contoso.com" ends up in the From field:
foreach (EmailMessage message in itemResults)
{
//forward the message to each of the users assistants
ResponseMessage forward = message.CreateForward();
forward.ToRecipients.Add("recipient#contoso.com");
forward.Send();
}
There doesn't seem to be any way to edit the ResponseMessage From property, and changing the From property of the message before converting it to a ResponseMessage doesn't make a difference. Is there way way to forward a message with a specified address in the From field?

MimeKit: How to delete an email by MessageId?

I am using MailKit/MimeKit 1.2.7 (latest NuGet version).
Deleting an email with the ImapClient is pretty easy ...
client.Inbox.AddFlags(uniqueId, MessageFlags.Deleted, silent: true);
... if you know the emails UniqueId or its Index.
In my case, I don't know either one nor the other. All I have is the message itself (MimeMessage) ad its MessageId.
I was hoping that MessageId == UniqueId, but obviously this is not the case.
Do I have any chance to delete an email by just having the corresponding MimeMessage/MessageId?
You could try doing something like this:
var uids = folder.Search (SearchQuery.HeaderContains ("Message-Id", message.MessageId));
folder.AddFlags (uids, MessageFlags.Deleted, silent: true);
Ideally, though, you'd keep track of the UniqueId that you used to fetch the message so that you can just use that value.

How to mark message as read, properly get from address and delete message

It seems as though I can finally access the service and get all unread mails from the inbox by using the EWS API but it seems as though the message doesn't send
EmailMessage message = new EmailMessage(emailService);
message.Subject = string.Format("Electricity token for: {0}", house.Number);
message.Body = html.HTMLCode;
message.ToRecipients.Add(email.From);
message.SendAndSaveCopy();
"email" refers to the list of unread emails I get back
what must I do to correctly get the senders email address and how do I mark it as read and then delete it?
Based on your other post, it appears that the problem was indeed that you hadn't bound to the original mail.

OpenPop - Gmail - GetMessageCount() returns 0 (zero)

When connecting to Gmail with OpenPop, I can only retrieve an email once, even if I do not delete it. Using GetMessageCount() I always receive 0 emails. How can I get all the emails that are there?
Only after reading them and processing them do I give order to delete. I am using the following code to get the emails:
using (var client = new Pop3Client())
{
// Connect to the server
client.Connect(serverData.Hostname, serverData.Port, serverData.UseSsl);
// Authenticate ourselves towards the server
client.Authenticate(serverData.Username, serverData.Password, AuthenticationMethod.UsernameAndPassword);
var emailAmount = client.GetMessageSizes().Count;
// Fetch all the current uids seen
var msgCount = client.GetMessageCount();
.....
}
Gmail is special. Take a look at this StackOverflow post which explains the non-standard behavior.
What you are interested in, is that Gmail will only show a message in ONE POP3 session, unless you do special stuff, like prepending recent: in front of your username.
Getting only the unread mails is how POP3 is supposed to work. If you want to see and manage older mails, you should use IMAP instead.

Categories