.Net - 通过 IMAP 协议从 Gmail 获取未读邮件时出错
.Net - Error when get unread mail from Gmail by IMAP protocol
我正在尝试通过 IMAP 协议从 Gmail 中获取未读邮件。我遵循了这个教程: https://briancaos.wordpress.com/2012/04/24/reading-mails-using-imap-and-mailsystem-net/#comment-4999 ,它收到了很多好评。
但是当我打电话给
MessageCollection messages = mails.SearchParse("UNSEEN");
我收到错误消息 "index and length must refer to a location within the string"。
我只是调用了一个简单的函数,所以我不知道哪里出了问题。有关详细信息,这是我的代码片段:
public class MailRepository
{
private Imap4Client _client = null;
public MailRepository(string mailServer, int port, bool ssl, string login, string password)
{
if (ssl)
Client.ConnectSsl(mailServer, port);
else
Client.Connect(mailServer, port);
Client.Login(login, password);
}
public IEnumerable<Message> GetAllMails(string mailBox)
{
return GetMails(mailBox, "ALL").Cast<Message>();
}
public IEnumerable<Message> GetUnreadMails(string mailBox)
{
return GetMails(mailBox, "UNSEEN").Cast<Message>();
}
protected Imap4Client Client
{
get
{
if (_client == null)
_client = new Imap4Client();
return _client;
}
}
private MessageCollection GetMails(string mailBox, string searchPhrase)
{
Mailbox mails = Client.SelectMailbox(mailBox);
MessageCollection messages = mails.SearchParse(searchPhrase);
return messages;
}
}
当您的邮件包含非 ASCII 字符时,可能会出现此错误。由于我在这里看不到简单的修复方法,并且不再支持 MailSystem.NET,因此我建议使用替代库。
Mailkit seems to be a good option. Also look here for further reference:
我正在尝试通过 IMAP 协议从 Gmail 中获取未读邮件。我遵循了这个教程: https://briancaos.wordpress.com/2012/04/24/reading-mails-using-imap-and-mailsystem-net/#comment-4999 ,它收到了很多好评。 但是当我打电话给
MessageCollection messages = mails.SearchParse("UNSEEN");
我收到错误消息 "index and length must refer to a location within the string"。 我只是调用了一个简单的函数,所以我不知道哪里出了问题。有关详细信息,这是我的代码片段:
public class MailRepository
{
private Imap4Client _client = null;
public MailRepository(string mailServer, int port, bool ssl, string login, string password)
{
if (ssl)
Client.ConnectSsl(mailServer, port);
else
Client.Connect(mailServer, port);
Client.Login(login, password);
}
public IEnumerable<Message> GetAllMails(string mailBox)
{
return GetMails(mailBox, "ALL").Cast<Message>();
}
public IEnumerable<Message> GetUnreadMails(string mailBox)
{
return GetMails(mailBox, "UNSEEN").Cast<Message>();
}
protected Imap4Client Client
{
get
{
if (_client == null)
_client = new Imap4Client();
return _client;
}
}
private MessageCollection GetMails(string mailBox, string searchPhrase)
{
Mailbox mails = Client.SelectMailbox(mailBox);
MessageCollection messages = mails.SearchParse(searchPhrase);
return messages;
}
}
当您的邮件包含非 ASCII 字符时,可能会出现此错误。由于我在这里看不到简单的修复方法,并且不再支持 MailSystem.NET,因此我建议使用替代库。
Mailkit seems to be a good option. Also look here for further reference: