我裝盤使用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