【发布时间】:2020-06-22 10:58:51
【问题描述】:
我正在编写一个 Windows 控制台应用程序来读取来自 Office365 上专门设置的电子邮件帐户的电子邮件。该帐户已全部设置,我们能够接收来自 Outlook 的电子邮件。控制台应用程序将从远程服务器按计划运行,并从特定电子邮件中提取特定附件,然后将这些电子邮件移动到另一个文件夹。我选择使用 MimeKit 的 MailKit 库并开始编写一个小型测试应用程序,我在下面列出:
在此代码上运行调试器时,我在 client.Authenticate 处遇到错误,异常引发为“AuthenticationException”。我在代码中使用的用户名和密码是正确的,与我在 Outlook 中使用的相同。我在这里做基础吗?我可以以纯文本形式提供密码,还是需要使用特定格式?如果我没有提供听到的所有信息,请告诉我,我会得到它们并在这里发布。
using MailKit.Net.Imap;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace CAppHarvestEmail
{
class Program
{
static void Main(string[] args)
{
try
{
var userName = "bi@mydomain.co";
var passWord = "mybipassword";
using (var client = new ImapClient())
{
client.Connect("outlook.office365.com", true);
client.AuthenticationMechanisms.Remove("XOAUTH2");
client.Authenticate(userName, passWord);
var inbox = client.Inbox;
inbox.Open(MailKit.FolderAccess.ReadOnly);
Console.WriteLine("Total messages: {0}", inbox.Count);
Console.WriteLine("Recent messages: {0}", inbox.Recent);
client.Disconnect(true);
}
}
catch (Exception e)
{
throw;
}
}
}
}
【问题讨论】: