【问题标题】:Read Office365 emails from Exchange Server using .Net C# IMAP client使用 .Net C# IMAP 客户端从 Exchange Server 读取 Office365 电子邮件
【发布时间】: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;
            }
        }
    }
}

【问题讨论】:

    标签: c# .net imap mailkit


    【解决方案1】:

    您需要为您的应用程序https://support.office.com/en-us/article/create-an-app-password-for-office-365-3e7c860f-bda4-4441-a618-b53953ee1183生成应用程序密码并更改行

    client.Connect("outlook.office365.com", 993, SecureSocketOptions.SslOnConnect);
    

    【讨论】:

    • 感谢 msestak,此电子邮件帐户似乎没有进行多重身份验证,因此我没有看到创建应用程序密码的选项。你认为我需要在提供密码之前以某种方式加密我的密码吗?
    • 您确实需要提供端口 993,您可以使用 SecureSocketOptions.Auto
    【解决方案2】:

    我求助于使用 Microsoft Exchange Web 服务而不是 IMAP。

    例子:

    using System;
    using Microsoft.Exchange.WebServices.Data;
    
    
    
    ExchangeService _service;
    
    Console.WriteLine("Registering Exchange connection");
    
    _service = new ExchangeService
    {
        Credentials = new WebCredentials(username, password)
    };
    
    // This is the office365 webservice URL
    _service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
    
    PropertySet propSet = new PropertySet(BasePropertySet.FirstClassProperties);
    propSet.Add(ItemSchema.MimeContent);
    propSet.Add(ItemSchema.TextBody);
    
    foreach (EmailMessage email in _service.FindItems(WellKnownFolderName.Inbox, new ItemView(10)))
    {
        var message = EmailMessage.Bind(_service, email.Id, propSet);
    
        Console.WriteLine("Email body: " + message.TextBody);
        Console.WriteLine();
    }
    

    Reference

    注意:这似乎是to be deprecated。另外,我不确定这是否使用基本身份验证。我想不会,因为这应该在 2020 年 10 月之后停止工作,而我刚刚在 2021 年 7 月使用此代码进行了快速测试。

    【讨论】:

    • 使用 Microsoft Exchange Web 服务 的完整示例?
    • 您也可以使用 Microsoft Graph
    • 注意:基本身份验证的关闭被推迟了一年,主要是由于 Covid 因素。相反,关闭是在 2021 年开始的。主要关闭时间是 2022 年 10 月,仅允许一些例外情况在此之后的一两年内停止。
    【解决方案3】:

    提供端口 993,您可以使用 SecureSocketOptions.Auto

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 2013-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 2011-01-17
      相关资源
      最近更新 更多