c# - Office 365 客户端 API SendMailAsync 返回未经授权

标签 c# asp.net-mvc api email office365

我正在尝试从 C# 中的 ASP MVC Web 应用程序设置 Office 365 集成,为此我使用 Outlook Mail REST API(客户端版本)。我一直在这里使用 API 引用:https://msdn.microsoft.com/office/office365/APi/mail-rest-operations

我可以正常登录 Office 365,获取 token ,然后读取邮件文件夹(即已发送邮件/收件箱),但当我尝试发送电子邮件时,出现以下错误:


未经授权

描述:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。

异常详细信息:Microsoft.OData.Client.DataServiceClientException:未经授权


我已添加读/写和发送电子邮件的权限,因此当我登录 Office 365 时,它会显示:

Office Integration Test App needs permission to:

Access your data anytime
Sign in as you
Send mail as you
Read and write access to your mail 

所以我认为“以您的身份发送邮件”就是我需要的。但是我仍然收到未经授权的错误消息。

这是我运行的用于发送电子邮件的代码:

    string AccessToken = (string)Session["Office365Token"];

    OutlookServicesClient client = new OutlookServicesClient(new Uri("https://outlook.office.com/api/v2.0"),
        async () =>
        {
            return AccessToken;
        });

    ItemBody EmailBody = new ItemBody
    {
        Content = "Test email from the project",
        ContentType = BodyType.HTML
    };

    List<Recipient> toRecipients = new List<Recipient>();

    toRecipients.Add(new Recipient() { EmailAddress = new EmailAddress() { Address = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1064756364757d71797c50647563643e737f7d" rel="noreferrer noopener nofollow">[email protected]</a>" } });

    Message newMessage = new Message
    {
        Subject = "Test Subject For Email",
        Body = EmailBody,
        ToRecipients = toRecipients
    };

    await client.Me.SendMailAsync(newMessage, true);

当我调用 SendMailAsync 时,错误发生在最后一行。我不太确定还可以尝试什么,也找不到任何有关导致此问题的信息。

非常感谢任何帮助!

最佳答案

我也一直在尝试使用 OutlookServicesClient 的 SendMailAsync 方法并已收到未经授权的回复。

Fiddler显示该 token 不包含在 SendMailAsync 创建的请求中,但是我可以看到当 OutlookServicesClient 时包含该 token 。生成读取消息的 Get 请求。 this GitHub issue 为我证实了这一点。其中还指出它曾经在库的 1.0.22 版本中工作,但从 1.0.34 版本开始就不再工作。

环顾四周后,我发现其他人已经尝试过 alternative approach to sending mail首先创建草稿,然后发送。看起来是这样的:

OutlookServicesClient outlookClient = new OutlookServicesClient(new Uri("https://outlook.office.com/api/v2.0"),
    async () =>
    {
        // already retrieved my AccessToken using AuthenticationContext
        return AccessToken;
    });

// Save to Drafts folder
await outlookClient.Me.Messages.AddMessageAsync(newMessage);

// Now send
await newMessage.SendAsync();

我的结论是 SendMailAsync 方法根本就被破坏了。通过首先将新消息保存为草稿,我们可以解决这个问题。

关于c# - Office 365 客户端 API SendMailAsync 返回未经授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34359005/

相关文章:

c# - 如何本地化统一的Android游戏? (以便Google Play可以看到它)

c# - 两个具有相同命名空间的不同 DLL

c# - 来自默认 ASP.NET MVC 新项目的异步操作的解释?

javascript - 混合 Javascript 和 Razor 代码

java - 谁能帮助我解决在 Android 中使用 AsyncTask 时收到错误代码 400 的问题

python - 使用 django-tastypie 创建、更新和删除调用

C# - 传递委托(delegate)参数并提取它

c# - FormsAuthentication.RedirectToLoginPage() 不工作

jquery - 以编程方式启动 div 并发出关闭相同 div 的问题,遇到没有匹配开始标记的结束标记 "div"。

python : atlassian. 错误。ApiValueError : No space or no content type, 或设置了错误的版本类型设置为内容,或状态参数不是草稿