首先让我们说这个问题让我发疯:-)
背景信息: 我需要向旧项目 ASP.NET、Framework 4.7.2 和 webform 添加图形支持。 我知道,这是一项古老而糟糕的技术,但我需要它发挥作用。
下面的代码 (C#) 在新项目 ASP.NET core Web 应用程序模板中运行良好
using Azure.Identity;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Graph;
using Microsoft.Graph.Models;
namespace WebApplication_testGraph.Pages
{
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
public IndexModel(ILogger<IndexModel> logger)
{
_logger = logger;
}
public void OnGet()
{
var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = "xxx";
// Values from app registration
var clientId = "xxx";
var clientSecret = "xxx";
// using Azure.Identity;
var options = new TokenCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};
// https://learn.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret, options);
var accessToken = clientSecretCredential.GetTokenAsync(new Azure.Core.TokenRequestContext(scopes) { });
// Console.WriteLine(accessToken.Token);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
var requestBody = new Microsoft.Graph.Users.Item.SendMail.SendMailPostRequestBody
{
Message = new Message
{
Subject = "Meet for lunch?",
Body = new ItemBody
{
ContentType = BodyType.Text,
Content = "The new cafeteria is open.",
},
ToRecipients = new List<Recipient>
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = "[email protected]",
},
},
},
},
SaveToSentItems = false,
};
graphClient.Users["[email protected]"].SendMail.PostAsync(requestBody);
}
}
}
但是在使用 ASP.NET Framework 4.7.2、C# 和 Web Form 的项目中,大约相同的代码不起作用。 我没有错误,但什么也没发生
这是代码:
using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Graph.Me.SendMail;
using Microsoft.Graph.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
public partial class test_email : System.Web.UI.Page
{
protected async Task Page_LoadAsync(object sender, EventArgs e)
{
var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = "xxx";
// Values from app registration
var clientId = "xxxx";
var clientSecret = "xxxx";
// using Azure.Identity;
var options = new TokenCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};
// https://learn.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret, options);
var accessToken = await clientSecretCredential.GetTokenAsync(new Azure.Core.TokenRequestContext(scopes) { });
//Console.WriteLine(accessToken.Token);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
var requestBody = new Microsoft.Graph.Users.Item.SendMail.SendMailPostRequestBody
{
Message = new Message
{
Subject = "Meet for lunch?",
Body = new ItemBody
{
ContentType = BodyType.Text,
Content = "The new cafeteria is open.",
},
ToRecipients = new List<Recipient>
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = "[email protected]",
},
},
},
},
SaveToSentItems = false,
};
await graphClient.Users["[email protected]"].SendMail.PostAsync(requestBody);
}
}
您能帮我理解为什么代码在这种情况下不起作用吗?
TNX
我尝试着改变
protected async Task Page_LoadAsync(object sender, EventArgs e)
在
public async Task Page_LoadAsync(object sender, EventArgs e)
I have no errors, but nothing really happens
-> 我创建了一个新的.net Framework 4.8 MVC项目(我没有在我的机器上安装4.7),然后在HomeController/Index方法中,我添加了下面的代码,效果很好。
using Azure.Identity;
using Microsoft.Graph;
public async Task<ActionResult> Index()
{
var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = "tenantId ";
var clientId = "clientId ";
var clientSecret = "clientSecret ";
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
var users = await graphClient.Users.GetAsync();
return View();
}
但是如果方法名称是
IndexAsync()
,我会得到404错误。
领先一步。 现在,下面的代码可以运行并在 ASP.NET 4.8、Web 表单、Framework 4.7.2 中发送电子邮件。 现在我需要添加一些附件,但由于其保护级别,MessageAttachmentsCollectionPage 无法访问
ublic partial class Test_email : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
public async void Button1_Click(object sender, EventArgs e)
{
try
{
//
await GetGizmosSvcAsync();
//
Response.Redirect("Post.aspx");
}
catch (Exception ex)
{
}
}
public async Task GetGizmosSvcAsync()
{
try
{
var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = "";
var clientId = "";
var clientSecret = "";
var attachments = new MessageAttachmentsCollectionPage();
var options = new TokenCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret, options);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
var requestBody = new Microsoft.Graph.Users.Item.SendMail.SendMailPostRequestBody
{
Message = new Message
{
Subject = "Meet for lunch XXXX1?",
Body = new ItemBody
{
ContentType = BodyType.Text,
Content = "The new cafeteria is open.",
},
ToRecipients = new List<Recipient>
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = "[email protected]",
},
},
},
},
SaveToSentItems = false,
};
await graphClient.Users["[email protected]"].SendMail.PostAsync(requestBody);
}
catch (Exception ex)
{
}
}
}