I searched a lot how to read an email from gmail and then mark it as unread (unseen), now that I found I wanted to share with everyone.
Asked
Active
Viewed 5,859 times
2 Answers
5
using library http://mailsystem.codeplex.com/
Source: http://mailsystem.codeplex.com/discussions/269058
add reference: activeup.net.common, activeup.net.imap4, activeup.net.mail
code:
Imap4Client imap = new Imap4Client();
imap.ConnectSsl("imap.gmail.com", 993);
imap.Login("aaaaa@gmail.com", "xxxxxxx");
imap.Command("capability");
Mailbox inbox = imap.SelectMailbox("inbox");
int[] ids = inbox.Search("UNSEEN");
if (ids.Length > 0)
{
ActiveUp.Net.Mail.Message msg_first = inbox.Fetch.MessageObject(ids[0]);
//ignore this gmail_data stuff // undefined in this scope // checking to make sure it's a "new" unread msg
//if (gmail_data != msg_first.Date.ToString())
//{
// gmail_data = msg_first.Date.ToString();
XElement xmail = new XElement("gmail",
new XAttribute("count", ids.Length.ToString()),
new XAttribute("modified", msg_first.Date.ToString())
);
string name = "", address = "", from = "";
Regex reg_name = new Regex("\"[^\"]+");
Regex reg_address = new Regex("<[^>]+");
ActiveUp.Net.Mail.Message msg = null;
for (var i = 0; i < ids.Length; i++)
{
msg = inbox.Fetch.MessageObject(ids[i]);
from = msg.HeaderFields["from"];
name = reg_name.Match(from).Value.Replace("\"", "");
address = reg_address.Match(from).Value.Replace("<", "");
xmail.Add(new XElement("entry",
new XAttribute("id", msg.MessageId),
new XAttribute("modified", msg.Date.ToString()),
new XAttribute("name", name),
new XAttribute("address", address),
new XElement("subject", msg.Subject),
new XElement("body-text", msg.BodyText.TextStripped),
new XElement("body-html", msg.BodyHtml.Text)
));
//mark as unread
var flags = new FlagCollection();
flags.Add("Seen");
inbox.RemoveFlags(ids[i], flags);
}
File.WriteAllText("gmail.xml", xmail.ToString());
}
}

Mike Caron
- 14,351
- 4
- 49
- 77

Leandro Almeida
- 123
- 1
- 7
-
1You might want to strip down the example code to the really relevant part. RemoveFlags() is the method doing the trick. The rest is bewildering. – Lars Jun 06 '14 at 14:01
0
- When you get all e-mail´s from server, the API automaticaly marks all e-mail´s as "seen/readed", and i think there´s no way to use the "search method" filtering by messageId, so you must get all e-mail to mark one (seen) e-mail as unread.
- You should get allways just the "unread massages" and add on a list the messageId of the e-mail´s that you want to mark as "unread", then you must pass the list to the method bellow, that will mark as unread all messages of the list.
Possible code:
public void MarkAsUnread(List<string> messageIdList)
{
Mailbox inbox = Client.SelectMailbox("inbox");
int[] ids = inbox.Search("ALL");
int ListCount = messageIdList.Count;
int MarkedAsUnread = 0;
if (ids.Length > 0)
{
ActiveUp.Net.Mail.Message msg = null;
for (var i = 0; i < ids.Length; i++)
{
msg = inbox.Fetch.MessageObject(ids[i]);
// if messageId is on the list, mark as unread.
if (String.Join(",", messageIdList).Contains(msg.MessageId))
{
var flags = new FlagCollection { "Seen" };
inbox.RemoveFlagsSilent(i+1, flags);
MarkedAsUnread = MarkedAsUnread + 1;
}
// optimization
if (MarkedAsUnread == ListCount)
{
break;
}
}
}
}
*If you really don´t want to get all, maybe you could filter by date using some code like that:
var box = imap.SelectMailbox("inbox");
var ids = box.Search("OR (CC @cc.lieser-online.de) (HEADER Envelope-To @cc.lieser-online.de)");

Gevard
- 1
- 1