使用统一API使用Office 365发送电子邮件

时间:2015-08-07 11:47:35

标签: ms-office office365 office365api

我们正在尝试使用O365 Unified API从我们的业务线应用发送电子邮件。我使用以下代码发送电子邮件。这会抛出 DataServiceQueryException异常" Unauthorized"

public async Task SendEmailAsUserAsync(EmailMessage message)
{
    try
    {
        var graphClient = await _authenticationHelper.GetGraphClientAsync();
        Message m = InitializeMessage(message);
        await graphClient.Me.SendMailAsync(m, true);
    }
    catch (DataServiceQueryException dsqe)
    {
        _logger.Error("Could not get files: " + dsqe.InnerException.Message, dsqe);
        throw;
    }
}

private static Message InitializeMessage(EmailMessage message)
{
    ItemBody body = new ItemBody {Content = message.Body, ContentType = BodyType.HTML};
    Message m = new Message
    {
        Body = body,
        Subject = message.Subject,
        Importance = Importance.Normal,
    };
    //Add all the to email ids
    if (message.ToRecipients != null)
        foreach (Models.Messaging.EmailAddress emailAddress in message.ToRecipients)
        {
            m.ToRecipients.Add(new Recipient { EmailAddress = new Microsoft.Graph.EmailAddress { Address = emailAddress.Address, Name = emailAddress.Name } });
        }
    return m;
}

_authenticationHelper.GetGraphClientAsync()的代码是

public async Task<GraphService> GetGraphClientAsync()
{
    Uri serviceRoot = new Uri(appConfig.GraphResourceUriBeta + appConfig.Tenant);
    _graphClient = new GraphService(serviceRoot,
        async () => await AcquireTokenAsyncForUser(appConfig.GraphResourceUri, appConfig.Tenant));
    return _graphClient;
}

private async Task<string> AcquireTokenAsyncForUser(string resource, string tenantId)
{
    AuthenticationResult authenticationResult = await GetAccessToken(resource, tenantId);
    _accessCode = authenticationResult.AccessToken;
    return _accessCode;
}

private async Task<AuthenticationResult> GetAccessToken(string resource, string tenantId)
{
    string authority = appConfig.Authority;
    AuthenticationContext authenticationContext = new AuthenticationContext(authority);
    ClientCredential credential = new ClientCredential(appConfig.ClientId, appConfig.ClientSecret);
    string authHeader = HttpContext.Current.Request.Headers["Authorization"];
    string userAccessToken = authHeader.Substring(authHeader.LastIndexOf(' ')).Trim();
    UserAssertion userAssertion = new UserAssertion(userAccessToken);
    var authenticationResult = await authenticationContext.AcquireTokenAsync(resource, credential, userAssertion);
    return authenticationResult;
}

但是,如果我更改SendEmailAsUserAsync方法,如下所示,将发送电子邮件,但会抛出 InvalidOperationException并显示消息&#34;复杂类型&#39; System.Object&#39;没有可设置的属性。&#34;

public async Task SendEmailAsUserAsync(EmailMessage message)
{
    try
    {
        var graphClient = await _authenticationHelper.GetGraphClientAsync();
        Message m = InitializeMessage(message);
        //await graphClient.Me.SendMailAsync(m, true); //This did not work
        var user = await graphClient.Me.ExecuteAsync();
        await user.SendMailAsync(m, true);
    }
    catch (DataServiceQueryException dsqe)
    {
        _logger.Error("Could not get files: " + dsqe.InnerException.Message, dsqe);
        throw;
    }  
}

如果这里有什么问题,任何人都可以指出。

1 个答案:

答案 0 :(得分:0)

实际上,图API没有汇编包装器。

Microsoft.Graph.dll已被弃用。

所以,你应该:

  1. 处理REST请求:请参阅此处:http://graph.microsoft.io/docs/api-reference/v1.0/api/message_send
  2. 使用Microsoft.Vipr项目生成包装器:请参阅此处:https://github.com/microsoft/vipr
  3. 对于身份验证,ADAL工作正常:)