Quantcast
Channel: ImapX 2
Viewing all articles
Browse latest Browse all 1952

Commented Unassigned: Folders are not getting initialized [1754]

$
0
0
I an new with IMAPX library. I am using the sample code provided on the IMAPX home page. I got an null reference exception while trying to get mails from Inbox, after debugging came to know that this exception is thrown by 'Folders' initialization. All the folders are set as null because of which I am receiving this error.

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.....


Viewing all articles
Browse latest Browse all 1952

Trending Articles