2

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 メソッドを変更すると、電子メールは送信されますが、「複合型 'System.Object' には設定可能なプロパティがありません」というメッセージとともに InvalidOperationException がスローされます。

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;
    }  
}

ここに何か問題があるかどうか、誰でも指摘できますか。

4

2 に答える 2

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

認証の場合、ADALは正常に機能します:)

于 2015-12-19T20:32:15.317 に答える
0

以下のサンプル プロジェクトを確認してください。これには実際の例があります (app.config に ClientID などを入力した後)。

Office 365 API デモ アプリケーション

メールを送信するために、以下の関数を使用します。これは、正しく設定すると機能します。また、 Authorization Code Grant Flowを使用して認証するための関数も多数あります。

    public async Task SendMail(string to, string subject, string body)
    {
        var client = await this.AuthenticationHelper
            .EnsureOutlookServicesClientCreatedAsync(
            Office365Capabilities.Mail.ToString());

        Message mail = new Message();
        mail.ToRecipients.Add(new Recipient() 
        {
            EmailAddress = new EmailAddress
            {
                Address = to,
            }
        });
        mail.Subject = subject;
        mail.Body = new ItemBody() { Content = body, ContentType = BodyType.HTML };

        await client.Me.SendMailAsync(mail, true);
    }
于 2015-08-15T22:04:27.900 に答える