Я использую MS Graph API, чтобы попытаться прочитать сообщения с помощью сообщений. GetAsync ()
Я смог подключиться к API Graph, но нет ничего печатающего в консоли из электронных писем, таких как сообщение. Мне интересно, связано ли это со мной в ожидании функции getAsync .
Когда я перемещаю всю логику электронной почты в программу. С архитектурой услуг он не будет печатать ничего в консоли.
// Program.cs
builder.Services.AddScoped();
using IHost host = builder.Build();
using (IServiceScope scope = host.Services.CreateScope())
{
StartUp startUp = scope.ServiceProvider.GetRequiredService();
startUp.Start();
}
// startup.cs
public class StartUp
{
private readonly IEmailService _emailService;
public StartUp(IEmailService emailService)
{
_emailService = emailService;
}
public void Start()
{
_emailService.ProcessEmails();
}
}
// emailservice.cs
public async Task ProcessEmails()
{
try
{
using (var graphContext = _graphService.GetGraphContext())
{
string userId = _emailConfig.EmailName;
var messages = await graphContext.Users[userId].MailFolders["Inbox"].Messages.GetAsync();
if (messages != null && messages.Value != null)
{
foreach (var message in messages.Value)
{
Console.WriteLine($"From: {message.From?.EmailAddress?.Address}");
Console.WriteLine($"Subject: {message.Subject}");
message.Body.ContentType = BodyType.Text;
var updateMessage = new Message { IsRead = false };
await graphContext.Users[userId].Messages[message.Id].PatchAsync(updateMessage);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error in EmailService: {ex.Message}");
}
}
// graphservice.cs
public GraphServiceClient GetGraphContext()
{
string clientID = ClientID;
string clientSecret = ClientSecret;
string tenantID = TenantID;
ClientSecretCredential credential = new ClientSecretCredential(
tenantID,
clientID,
clientSecret
);
GraphServiceClient graphClient = new GraphServiceClient(credential);
return graphClient;
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... w-to-await
График API вызовы в сообщениях. GetAsync ничего не регистрируя, как ждать ⇐ C#
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
SQLite3 Async Queries ждать, пока тяжелые вызовы API закончится, чтобы выполнить
Anonymous » » в форуме Python - 0 Ответы
- 19 Просмотры
-
Последнее сообщение Anonymous
-