// We create Imap client
Imap4Client imap = new Imap4Client();
try
{
// We connect to the imap4 server
imap.Connect(_tbImap4Server.Text);
this.AddLogEntry(string.Format("Connection to {0} successfully", _tbImap4Server.Text));
// Login to mail box
imap.Login(_tbUserName.Text, _tbPassword.Text);
this.AddLogEntry(string.Format("Login to {0} successfully", _tbImap4Server.Text));
Mailbox inbox = imap.SelectMailbox("inbox");
if (inbox.MessageCount > 0)
{
for (int i = 1; i < inbox.MessageCount + 1; i++)
{
ActiveUp.Net.Mail.Message message = inbox.Fetch.MessageObject(i);
ListViewItem lvi = new ListViewItem();
lvi.Text = i.ToString("0000");
lvi.SubItems.AddRange(new string[] { message.Subject});
lvi.Tag = message;
_lvMessages.Items.Add(lvi);
this.AddLogEntry(string.Format("{3} Subject: {0} From :{1} Message Body {2}"
, message.Subject, message.From.Email, message.BodyText, i.ToString("0000")));
}
}
else
{
this.AddLogEntry("There is no unanswered messages in the imap4 account");
}
}
catch (Imap4Exception iex)
{
this.AddLogEntry(string.Format("Imap4 Error: {0}", iex.Message));
}
catch (Exception ex)
{
this.AddLogEntry(string.Format("Failed: {0}", ex.Message));
}
finally
{
if (imap.IsConnected)
{
imap.Disconnect();
}
}
try
{
int index = int.Parse(_lvMessages.SelectedItems[0].Text);
FlagCollection flags = new FlagCollection();
flags.Add("Answered");
inbox.SetFlags(1, flags);
this.AddLogEntry(string.Format("Flat answered setted to the message with index {0}",index.ToString()));
}
catch (Exception ex)
{
this.AddLogEntry(string.Format("Failed: {0}", ex.Message));
}