要从启用了多因子身份验证(MFA)的Office 365帐户使用VB.NET或C#代码发送电子邮件,你需要使用Microsoft Graph API。Microsoft Graph API允许你访问Office 365的数据和服务,包括发送电子邮件。
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
string tenantId = "your-tenant-id";
string clientId = "your-client-id";
string clientSecret = "your-client-secret";
string resource = "https://graph.microsoft.com/";
string authority = $"https://login.microsoftonline.com/{tenantId}";
string redirectUri = "https://login.microsoftonline.com/common/oauth2/nativeclient";
// Step 1: Get authorization code
string authorizationCode = await GetAuthorizationCode(authority, clientId, redirectUri);
// Step 2: Get access token
string accessToken = await GetAccessToken(authority, clientId, clientSecret, authorizationCode, redirectUri);
// Step 3: Send email using Microsoft Graph API
await SendEmail(accessToken);
}
static async Task<string> GetAuthorizationCode(string authority, string clientId, string redirectUri)
{
// Implement OAuth 2.0 authorization code flow to get the authorization code
// This is a simplified example, in practice you would need to handle the browser redirection and user consent
return "your-authorization-code";
}
static async Task<string> GetAccessToken(string authority, string clientId, string clientSecret, string authorizationCode, string redirectUri)
{
using (var client = new HttpClient())
{
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "authorization_code"),
new KeyValuePair<string, string>("client_id", clientId),
new KeyValuePair<string, string>("client_secret", clientSecret),
new KeyValuePair<string, string>("code", authorizationCode),
new KeyValuePair<string, string>("redirect_uri", redirectUri),
new KeyValuePair<string, string>("resource", resource)
});
var response = await client.PostAsync(authority + "/oauth2/token", content);
var result = await response.Content.ReadAsStringAsync();
var tokenData = System.Text.Json.JsonDocument.Parse(result).RootElement.GetProperty("access_token").GetString();
return tokenData;
}
}
static async Task SendEmail(string accessToken)
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var content = new StringContent(@"
{
""message"": {
""subject"": ""Hello"",
""body"": {
""contentType"": ""Text"",
""content"": ""Hello, World!""
},
""toRecipients"": [
{
""emailAddress"": {
""name"": ""John Doe"",
""address"": ""john.doe@contoso.com""
}
}
]
}
}", System.Text.Encoding.UTF8, "application/json");
var response = await client.PostAsync("https://graph.microsoft.com/v1.0/me/sendMail", content);
if (response.IsSuccessStatusCode)
{
Console.WriteLine("Email sent successfully.");
}
else
{
Console.WriteLine($"Failed to send email. Status code: {response.StatusCode}");
}
}
}
}
通过以上步骤和代码示例,你应该能够从启用了多因子身份验证的Office 365帐户使用VB.NET或C#代码发送电子邮件。
领取专属 10元无门槛券
手把手带您无忧上云