1

我正在尝试使用 Office 365 C# 客户端库发送电子邮件。我已经成功地使用它执行了其他操作,即获取文件夹和获取消息,但无法发送电子邮件。

我正在使用 Microsoft 示例中提供的 MailHelper 类。这是方法:

internal async Task<String> ComposeAndSendMailAsync(string subject,
                                                            string bodyContent,
                                                            string recipients)
        {
            // The identifier of the composed and sent message.
            string newMessageId = string.Empty;

            // Prepare the recipient list
            var toRecipients = new List<Recipient>();
            string[] splitter = { ";" };
            var splitRecipientsString = recipients.Split(splitter, StringSplitOptions.RemoveEmptyEntries);
            foreach (string recipient in splitRecipientsString)
            {
                toRecipients.Add(new Recipient
                {
                    EmailAddress = new EmailAddress
                    {
                        Address = recipient.Trim(),
                        Name = recipient.Trim(),
                    },
                });
            }

            // Prepare the draft message.
            var draft = new Message
            {
                Subject = subject,
                Body = new ItemBody
                {
                    ContentType = BodyType.HTML,
                    Content = bodyContent
                },
                ToRecipients = toRecipients
            };

            try
            {
                // Make sure we have a reference to the Outlook Services client.
                var outlookClient = await AuthenticationHelper.GetOutlookClientAsync("Mail");

                //Send the mail.
                await outlookClient.Me.SendMailAsync(draft, true);

                return draft.Id;
            }

            //Catch any exceptions related to invalid OData.
            catch (Microsoft.OData.Core.ODataException ode)
            {

                throw new Exception("We could not send the message: " + ode.Message);
            }
            catch (Exception e)
            {
                throw new Exception("We could not send the message: " + e.Message);
            }
        }

我的论点不是空的,而且似乎是正确的。我得到的错误是:“无法读取请求正文。 ”。

我已经确保我的应用程序注册了正确的权限,所以我不知所措。有谁知道我的代码发生了什么?

4

1 回答 1

0

尝试捕获请求的 Fiddler 跟踪并检查 Content-Type 标头。

于 2015-03-24T13:03:34.557 回答