private void ListMessages()
    {
        lblMessage.ForeColor = Color.Green;
        lblMessage.Text = "";

        try
        {
            // initialize exchange client
            NetworkCredential credential = new NetworkCredential(txtUsername.Text, Session["Password"].ToString(), txtDomain.Text);
            Aspose.Email.Exchange.ExchangeWebServiceClient client = new Aspose.Email.Exchange.ExchangeWebServiceClient(txtHost.Text, credential);

            // get list of messages
            Aspose.Email.Exchange.ExchangeMailboxInfo exchangeMailboxInfo = client.GetMailboxInfo();
            Aspose.Email.Exchange.ExchangeMessageInfoCollection msgCollection = client.ListMessages(exchangeMailboxInfo.InboxUri);
            gvMessages.DataSource = msgCollection;
            gvMessages.DataBind();

            lblMessage.Text = "Successfully connected to Microsoft Exchange server.<br><hr>";
        }
        catch (Exception ex)
        {
            lblMessage.ForeColor = Color.Red;
            lblMessage.Text = "Error: " + ex.Message;
        }
    }
    protected void brnSendEmail_Click(object sender, EventArgs e)
    {
        lblMessage.ForeColor = Color.Green;
        lblMessage.Text = "";

        try
        {
            // initialize exchange client
            NetworkCredential credential = new NetworkCredential(txtUsername.Text, Session["Password"].ToString(), txtDomain.Text);
            Aspose.Email.Exchange.ExchangeWebServiceClient client = new Aspose.Email.Exchange.ExchangeWebServiceClient(txtHost.Text, credential);

            // build message
            MailMessage msg = new MailMessage();
            msg.From = txtFrom.Text;
            msg.To = txtTo.Text;
            msg.Subject = txtSubject.Text;
            msg.TextBody = txtTextBody.Text;

            // send email
            client.Send(msg);

            lblMessage.Text = "Successfully sent email using Microsoft Exchange server.<br><hr>";
        }
        catch (Exception ex)
        {
            lblMessage.ForeColor = Color.Red;
            lblMessage.Text = "Error: " + ex.Message;
        }
    }
    protected void brnSendEmail_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "";
        lblMessage.ForeColor = Color.Green;

        try
        {
            // initialize exchange client
            NetworkCredential credential = new NetworkCredential(txtUsername.Text, txtPassword.Text, txtDomain.Text);
            Aspose.Email.Exchange.ExchangeWebServiceClient client = new Aspose.Email.Exchange.ExchangeWebServiceClient(txtHost.Text, credential);

            // get mailbox and folders information
            Aspose.Email.Exchange.ExchangeMailboxInfo exchangeMailboxInfo = client.GetMailboxInfo();
            lblMailboxURI.Text = exchangeMailboxInfo.MailboxUri;
            lblInboxURI.Text = exchangeMailboxInfo.InboxUri;
            lblSentItemsURI.Text = exchangeMailboxInfo.SentItemsUri;
            lblDraftsURI.Text = exchangeMailboxInfo.DraftsUri;
            lblCalendarURI.Text = exchangeMailboxInfo.CalendarUri;
            lblDeletedItemsURI.Text = exchangeMailboxInfo.DeletedItemsUri;

            lblMessage.Text = "Successfully connected to Microsoft Exchange server.<br><hr>";
        }
        catch (Exception ex)
        {
            lblMessage.ForeColor = Color.Red;
            lblMessage.Text = "Error: " + ex.Message;
        }
    }
    protected void gvMessages_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "DisplayMessage")
        {
            pnlMessage.Visible = true;
            string msgURI = e.CommandArgument.ToString();

            try
            {
                // initialize exchange client
                NetworkCredential credential = new NetworkCredential(txtUsername.Text, Session["Password"].ToString(), txtDomain.Text);
                Aspose.Email.Exchange.ExchangeWebServiceClient client = new Aspose.Email.Exchange.ExchangeWebServiceClient(txtHost.Text, credential);

                lblMessage.ForeColor = Color.Green;
                lblMessage.Text = "Successfully connected to Microsoft Exchange Server.<br><hr>";

                Aspose.Email.Exchange.ExchangeMailboxInfo exchangeMailboxInfo = client.GetMailboxInfo();
                // get the message
                MailMessage msg = client.FetchMessage(msgURI);
                // sender name and address
                litFrom.Text = msg.From[0].DisplayName + " <" + msg.From[0].Address + ">";
                // to addresses
                litTo.Text = "";
                foreach (MailAddress address in msg.To)
                {
                    litTo.Text += address.DisplayName + " <" + address.Address + "> , ";
                }
                // cc addresses
                litCc.Text = "";
                foreach (MailAddress address in msg.CC)
                {
                    litCc.Text += address.DisplayName + " <" + address.Address + "> , ";
                }
                // subject
                litSubject.Text = msg.Subject;
                litBody.Text = msg.HtmlBody;
            }
            catch (Exception ex)
            {
                lblMessage.ForeColor = Color.Red;
                lblMessage.Text = "Error: " + ex.Message;
            }
        }
    }
    protected void brnSendEmail_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "";
        lblMessage.ForeColor = Color.Green;

        try
        {
            // initialize exchange client
            NetworkCredential credential = new NetworkCredential(txtUsername.Text, txtPassword.Text, txtDomain.Text);
            Aspose.Email.Exchange.ExchangeWebServiceClient client = new Aspose.Email.Exchange.ExchangeWebServiceClient(txtHost.Text, credential);

            lblMessage.Text = "Successfully connected to Microsoft Exchange server.<br><hr>";
        }
        catch (Exception ex)
        {
            lblMessage.ForeColor = Color.Red;
            lblMessage.Text = "Error: " + ex.Message;
        }
    }
    private void DownloadFile(string msgSequenceNumber, string format)
    {
        try
        {
            // initialize exchange client
            NetworkCredential credential = new NetworkCredential(txtUsername.Text, Session["Password"].ToString(), txtDomain.Text);
            Aspose.Email.Exchange.ExchangeWebServiceClient client = new Aspose.Email.Exchange.ExchangeWebServiceClient(txtHost.Text, credential);

            lblMessage.ForeColor = Color.Green;
            lblMessage.Text = "Successfully connected to Microsoft Exchange Server.<br><hr>";

            // get the message
            MemoryStream stream = new MemoryStream();
            MailMessage msg = client.FetchMessage(msgSequenceNumber);
            if (format == "eml")
                msg.Save(stream, MessageFormat.Eml);
            else
                msg.Save(stream, MessageFormat.Msg);
            stream.Position = 0;
            byte[] msgBytes = new byte[stream.Length];
            stream.Read(msgBytes, 0, (int)stream.Length);

            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("Content-Length", msgBytes.Length.ToString());
            Response.AddHeader("Content-Disposition", "attachment; filename=Message." + format);
            Response.ContentType = "application/octet-stream";
            Response.BinaryWrite(msgBytes);
            Response.End();

        }
        catch (Exception ex)
        {
            lblMessage.ForeColor = Color.Red;
            lblMessage.Text = "Error: " + ex.Message;
        }
    }