Please help I need to implement this in one of my projects.
Thanks in Advance
Comments: ** Comment from web user: marizwan20 **
Below is the code I am using to connect to the server and get mails.
```
public void GetInboxEmail(string imapServer, int imapPort, string email, string password)
{
try
{
var client = new ImapClient(imapServer, imapPort, true,false);
bool result = client.Connect();
if (result)
{
result = client.Login(email, password);
if (result)
{
foreach (ImapX.Message m in client.Folders.Inbox.Messages)
{
try { m.Process(); }
catch { continue; }
//we can get message Id from message heders
//it can be helpful for identification messages if you saving this messages in own database
var messId = m.Headers.FirstOrDefault(h => h.Key.ToLower().Trim() == "message-id");
var emailHistory = new EmailHistory();
if (!messId.Equals(null) && !string.IsNullOrEmpty(messId.Value))
{
//here you can verify - exists message in your database or not exists
emailHistory.MessageId = messId.Value;
//bool existMessage = db.EmailHistories.Any(em => em.MessageId.Equals(emailHistory.MessageId));
//if (existMessage) continue;
continue;
}
else { continue; }
bool isHtml;
emailHistory.Body = m.GetDecodedBody(out isHtml);
emailHistory.DateTime = m.Date;
emailHistory.FromEmail += m.From.Address + ";";
emailHistory.FromName += m.From.DisplayName + ";";
foreach (var address in m.To)
{
emailHistory.ToEmail += address.Address + ";";
emailHistory.ToName += address.DisplayName + ";";
}
emailHistory.IsNew = true;
emailHistory.Subject = m.Subject;
}
}
}
}
catch (Exception ex)
{
}
}
```
Getting the exception at below line of code
```
foreach (ImapX.Message m in client.Folders.Inbox.Messages)
```
Please Help...
Once again Thanks in Advance.....