Accessing Imap in C#

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

Issues fetching sent Mail using IMAP in c#

I solved the issue by setting the default mailbox. In my case, the default mailbox was always INBOX due to which i was not able to fetch the sent mail. it took a single line to solve the issue. This is the code which i'm using now:

ImapClient client = new ImapClient("ExampleHost", port, ssl);
client.DefaultMailbox = "[Gmail]/Sent Mail";
try
{
client.Login("ExampleEmail", "ExamplePass", AuthMethod.Login);
IEnumerable<uint> units = client.Search(SearchCondition.Seen());
DataTable TempTaskTable = new DataTable();
TempTaskTable.Columns.Add("FromEmail", typeof(string));
TempTaskTable.Columns.Add("ToEmail", typeof(string));
TempTaskTable.Columns.Add("Subject", typeof(string));
foreach (var uid in units)
{
System.Net.Mail.MailMessage email = client.GetMessage(uid,true, "[Gmail]/Sent Mail");
DataRow TempTaskRow2 = TempTaskTable.NewRow();
TempTaskRow2["FromEmail"] = email.Sender;
TempTaskRow2["ToEmail"] = email.From;
TempTaskRow2["Subject"] = email.Subject;
}
}
catch (Exception ex)
{
string exceptionCheck = ex.Message;
}

You can also see all of your mailboxes using:

List<String> mailBoxesCheck = new List<string>();
foreach (var folder in client.ListMailboxes())
{
mailBoxesCheck.Add(folder);
}

Authenticate Office 365 IMAP Account using Unattended C# Console

This is an answear to your latest comment, as it's my final recommendation. So, first of all, you should decide if you want to acess the data on behalf of user, or as an app granted permissions by admin.

First step is to register your app.

Second step is getting the acess token. This is going to differ based on the method you chose. Tutorial for each: acting on behalf of the user or acting without the user, but granted permission from admin.

Once you have the acess token, you can call the Microsoft Graph API. The important thing is, you always have to call Microsoft Graph API. There is no other official way (as far as I know) of comunicating with Microsoft's services. You can try the requests with the Microsoft Graph Explorer, however it's VERY limited with it's defaul urls/parameters, so I suggest taking a look at the docs.

From what you've described, you first want to obtain UserID. The way of doing this is going to vary based on what type of auth you chose.

  • If you chose to act on behalf of user, you should be able to get that (ID) using this endpoint: https://graph.microsoft.com/v1.0/me/
  • If you chose to act as an app with admin consent, you should be able to search for user using the https://graph.microsoft.com/v1.0/me/people/?$search= with search query parameters. Here are the docs for this endpoint

Now, the only thing left, is to supply that ID to one of the Outlook api methods. You can find docs for them here. Specifically, it seems like you want to list all messages and then read a specific message.

Also, keep an eye on what methods you use with which type of auth. On behalf of user, you usually want url's that contain /me, on behalf of app with given admin privelages, you usually want some endpoint that enables you to pass user id.

Hope I helped!

PS: There is no code in this response, because there is a lot of stuff that just cannot be coded without your decisions, actions on Azure and so on. I suggest you read a little bit about auth and graph api using microsoft docs I linked earlier.



Related Topics



Leave a reply



Submit