无法通过Office 365 C#客户端库发送电子邮件

时间:2015-03-23 20:21:18

标签: c# office365 outlook-restapi

我正在尝试使用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);
            }
        }

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

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

1 个答案:

答案 0 :(得分:0)

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