使用.Net C#IMAP客户端从Exchange Server中读取Office365电子邮件

问题描述 投票:0回答:2

我正在编写Windows控制台应用程序,以从Office365上的特殊设置的电子邮件帐户读取电子邮件。该帐户已全部设置,我们能够接收来自Outlook的电子邮件。控制台应用程序将从远程服务器按计划运行,并从特定电子邮件中提取特定附件,然后将这些电子邮件移至另一个文件夹。我选择使用MimeKit中的MailKit库,并已开始编写一个小型测试应用程序,下面列出了我的代码:

[在此代码上运行调试器时,我在client上遇到错误。对引发的异常进行身份验证,将其显示为“ AuthenticationException”。我在代码中使用的userName和passWord正确,并且与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 = "[email protected]";
                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
2个回答
0
投票

您需要为应用程序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);

0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.