Using C# .Net Libraries to Check for Imap Messages from Gmail Servers

using c# .net libraries to check for IMAP messages from gmail servers

The URL listed here might be of interest to you

http://www.codeplex.com/InterIMAP

which was extension to

http://www.codeproject.com/KB/IP/imaplibrary.aspx?fid=91819&df=90&mpp=25&noise=5&sort=Position&view=Quick&fr=26&select=2562067#xx2562067xx

Reading Emails from Gmail C# - IMAP

You should use the solution of the second post of the topic were you have found something, S22.Imap.
(Here you can find a compiled version).

After download, you can read the documentation. It is simple and readable.

Actually, I use this library and that works well !

.NET how to retrieve list of emails from Gmail server with out third party

There is no .NET framework support for the IMAP or POP3 protocols you would use to do this. You would need to use a 3rd party DLL - there are free ones available. Or implement the protocol yourself using C# - I'm not going to attempt to provide sample code for that, though.

See also this question which discusses some of the components available, as well as having an answer which specifically states that there is no .NET framework support.

Reading emails from Gmail in C#

Using the library from: https://github.com/pmengal/MailSystem.NET

Here is my complete code sample:

Email Repository

using System.Collections.Generic;
using System.Linq;
using ActiveUp.Net.Mail;

namespace GmailReadImapEmail
{
public class MailRepository
{
private Imap4Client client;

public MailRepository(string mailServer, int port, bool ssl, string login, string password)
{
if (ssl)
Client.ConnectSsl(mailServer, port);
else
Client.Connect(mailServer, port);
Client.Login(login, password);
}

public IEnumerable<Message> GetAllMails(string mailBox)
{
return GetMails(mailBox, "ALL").Cast<Message>();
}

public IEnumerable<Message> GetUnreadMails(string mailBox)
{
return GetMails(mailBox, "UNSEEN").Cast<Message>();
}

protected Imap4Client Client
{
get { return client ?? (client = new Imap4Client()); }
}

private MessageCollection GetMails(string mailBox, string searchPhrase)
{
Mailbox mails = Client.SelectMailbox(mailBox);
MessageCollection messages = mails.SearchParse(searchPhrase);
return messages;
}
}
}

Usage

[TestMethod]
public void ReadImap()
{
var mailRepository = new MailRepository(
"imap.gmail.com",
993,
true,
"yourEmailAddress@gmail.com",
"yourPassword"
);

var emailList = mailRepository.GetAllMails("inbox");

foreach (Message email in emailList)
{
Console.WriteLine("<p>{0}: {1}</p><p>{2}</p>", email.From, email.Subject, email.BodyHtml.Text);
if (email.Attachments.Count > 0)
{
foreach (MimePart attachment in email.Attachments)
{
Console.WriteLine("<p>Attachment: {0} {1}</p>", attachment.ContentName, attachment.ContentType.MimeType);
}
}
}
}

Another example, this time using MailKit

public class MailRepository : IMailRepository
{
private readonly string mailServer, login, password;
private readonly int port;
private readonly bool ssl;

public MailRepository(string mailServer, int port, bool ssl, string login, string password)
{
this.mailServer = mailServer;
this.port = port;
this.ssl = ssl;
this.login = login;
this.password = password;
}

public IEnumerable<string> GetUnreadMails()
{
var messages = new List<string>();

using (var client = new ImapClient())
{
client.Connect(mailServer, port, ssl);

// Note: since we don't have an OAuth2 token, disable
// the XOAUTH2 authentication mechanism.
client.AuthenticationMechanisms.Remove("XOAUTH2");

client.Authenticate(login, password);

// The Inbox folder is always available on all IMAP servers...
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadOnly);
var results = inbox.Search(SearchOptions.All, SearchQuery.Not(SearchQuery.Seen));
foreach (var uniqueId in results.UniqueIds)
{
var message = inbox.GetMessage(uniqueId);

messages.Add(message.HtmlBody);

//Mark message as read
//inbox.AddFlags(uniqueId, MessageFlags.Seen, true);
}

client.Disconnect(true);
}

return messages;
}

public IEnumerable<string> GetAllMails()
{
var messages = new List<string>();

using (var client = new ImapClient())
{
client.Connect(mailServer, port, ssl);

// Note: since we don't have an OAuth2 token, disable
// the XOAUTH2 authentication mechanism.
client.AuthenticationMechanisms.Remove("XOAUTH2");

client.Authenticate(login, password);

// The Inbox folder is always available on all IMAP servers...
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadOnly);
var results = inbox.Search(SearchOptions.All, SearchQuery.NotSeen);
foreach (var uniqueId in results.UniqueIds)
{
var message = inbox.GetMessage(uniqueId);

messages.Add(message.HtmlBody);

//Mark message as read
//inbox.AddFlags(uniqueId, MessageFlags.Seen, true);
}

client.Disconnect(true);
}

return messages;
}
}

Usage

[Test]
public void GetAllEmails()
{
var mailRepository = new MailRepository("imap.gmail.com", 993, true, "YOUREMAILHERE@gmail.com", "YOURPASSWORDHERE");
var allEmails = mailRepository.GetAllMails();

foreach(var email in allEmails)
{
Console.WriteLine(email);
}

Assert.IsTrue(allEmails.ToList().Any());
}

Looking for a real-time IMAP notification of new Emails

You need to handle IMAP IDLE.

This will notify you when new messages arrive, without constant polling (which is bad).


A very good, commercial .NET IMAP library is MailBee.Net. I used it for a small project a while back, and it seemed to handle things very well, and be fairly easy to work with. There may be others - just search in your library for sending the IDLE command or IDLE command handling, and you'll likely find something.



Related Topics



Leave a reply



Submit