IMAP searchParse异常?

时间:2013-12-05 10:41:23

标签: c# imap

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web;
using ActiveUp.Net.Mail;
using ActiveUp.Net.Imap4;

namespace imapClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Imap4Client client = new Imap4Client();
            client.ConnectSsl("imap.gmail.com", 993);

                MessageBox.Show("connected!");
            client.Login("soham.elf", "********");
                MessageBox.Show("signed in!");
            Mailbox mail = client.SelectMailbox("INBOX");
//exception thrown here
            MessageCollection msgs = mail.SearchParse("ALL"); 
            textBox1.Text = msgs.Count.ToString();

        }
    }
}

Expection:“索引和长度必须指向字符串中的位置。 参数名称:长度“

我正在尝试测试IMAP客户端;我刚刚开始。我正在使用mailsystem.NET。我的代码出了什么问题?

1 个答案:

答案 0 :(得分:1)

尝试使用该文档中的短语:http://www.limilabs.com/blog/imap-search-requires-parentheses

    private void SearchFromTest()
    {

        try
        {
            var _selectedMailBox = "INBOX";
            var _searchWithEmailFrom = "email@domain.com";
            using (var _clientImap4 = new Imap4Client())
            {

                _clientImap4.ConnectSsl(_imapServerAddress, _imapPort);
                //_clientImap4.Connect(_mailServer.address, _mailServer.port);

                _clientImap4.Login(_imapLogin, _imapPassword); // Efetua login e carrega as MailBox da conta.
                //_clientImap4.LoginFast(_imapLogin, _imapPassword); // Efetua login e não carrega as MailBox da conta.

                var _mailBox = _clientImap4.SelectMailbox(_selectedMailBox);

                // A0001 SEARCH CHARSET utf-8 BODY "somestring" OR TO "someone@me.com" FROM "someone@me.com"
                var searchPhrase = "CHARSET utf-8 FROM \"" + _searchWithEmailFrom +  "\"";
                foreach (var messageId in _mailBox.Search(searchPhrase).AsEnumerable())
                {
                    var message = _mailBox.Fetch.Message(messageId);
                    var _imapMessage = Parser.ParseMessage(message);
                }

                _clientImap4.Disconnect();
            }

            Assert.IsTrue(true);
        }
        catch (Exception e)
        {
            Assert.Fail("Don't work.", e);
        }

    }