我装盘使用ImapClient阅读来自Gmail的邮件 :阅读电子邮件
using AE.Net.Mail;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Gmail.v1;
using Google.Apis.Gmail.v1.Data;
using Google.Apis.Plus.v1;
using Google.Apis.Plus.v1.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Web;
namespace Web.FrontOffice.Utils
{
public class Program
{
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/gmail-dotnet-quickstart.json
public static void ReadMailFromGmail()
{
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = "My_ClientId", ClientSecret = "My_ClientSecret" },
new[] { "https://mail.google.com/","https://www.googleapis.com/auth/userinfo.email"}, "user", CancellationToken.None, new FileDataStore("Analytics.Auth.Store")).Result;
// Create Gmail API service.
PlusService service = new PlusService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = "My App"});
// Define parameters of request.
Person me = service.People.Get("me").Execute();
Person.EmailsData myAccountEmail = me.Emails.Where(a => a.Type == "account").FirstOrDefault();
// List labels.
ImapClient ic = new ImapClient("imap.gmail.com", myAccountEmail.Value, credential.Token.AccessToken, AE.Net.Mail.AuthMethods.SaslOAuth, 993, true);
ic.SelectMailbox("INBOX");
// MailMessage represents, well, a message in your mailbox
var uids = ic.Search(SearchCondition.SentSince(new DateTime(2017, 4, 13)));
foreach (var uid in uids)
{
MailMessage message = ic.GetMessage(uid);
Debug.WriteLine(message.Body+" "+message.Subject+" "+message.Date);
}
}
}
}
类型的异常“System.Exception的”发生在AE.Net.Mail.dll,但没有处理在用户代码
其他信息:xm003 BAD无法解析命令
谢谢@Luke – Ahmado
AE.Net.Mail已经死了好几年了,在这一点上,你可能要考虑切换到我的[MailKit](https:/ /github.com/jstedfast/MailKit)库,而不是这些错误。 – jstedfast