João Paulo
João Paulo
CC#
Created by João Paulo on 5/8/2024 in #help
How can i detect a new e-mail arrival using GraphClientServer
current working solution is as follows
const int TIMEOUT_MAIL_WAIT_m = 8;
const int ANTIDDOS_LAG_ms = 2900;

using var timeout_cts = new CancellationTokenSource();
using var timeout = Task.Delay(TimeSpan.FromMinutes(TIMEOUT_MAIL_WAIT_m), timeout_cts.Token);

string query = $"ReceivedDateTime gt {DateTime.UtcNow:yyyy-MM-ddTHH:mm:ssZ} and from/emailAddress/address eq '{emailAddress}'";
var messageRepository = graphClient.Users[config.UserMailAddress].MailFolders.Inbox.Messages;

while (timeout.IsCompleted is false)
{
var msg = await messageRepository.Request().Filter(query).GetAsync();
if (msg.Count is not 0)
{
Console.WriteLine("Message detected");
// TODO: write logic to the captured message right here

timeout_cts.Cancel();
break;
}

if (Task.WhenAny(Task.Delay(TimeSpan.FromMinutes(1)), timeout) == timeout) break;
Thread.Sleep(ANTIDDOS_LAG_ms);
}

if (timeout.IsCompleted && timeout.IsCanceled is false)
{
// TODO: better exception message, mail not received whitin time limit frame
throw new TimeoutException("damn");
}
const int TIMEOUT_MAIL_WAIT_m = 8;
const int ANTIDDOS_LAG_ms = 2900;

using var timeout_cts = new CancellationTokenSource();
using var timeout = Task.Delay(TimeSpan.FromMinutes(TIMEOUT_MAIL_WAIT_m), timeout_cts.Token);

string query = $"ReceivedDateTime gt {DateTime.UtcNow:yyyy-MM-ddTHH:mm:ssZ} and from/emailAddress/address eq '{emailAddress}'";
var messageRepository = graphClient.Users[config.UserMailAddress].MailFolders.Inbox.Messages;

while (timeout.IsCompleted is false)
{
var msg = await messageRepository.Request().Filter(query).GetAsync();
if (msg.Count is not 0)
{
Console.WriteLine("Message detected");
// TODO: write logic to the captured message right here

timeout_cts.Cancel();
break;
}

if (Task.WhenAny(Task.Delay(TimeSpan.FromMinutes(1)), timeout) == timeout) break;
Thread.Sleep(ANTIDDOS_LAG_ms);
}

if (timeout.IsCompleted && timeout.IsCanceled is false)
{
// TODO: better exception message, mail not received whitin time limit frame
throw new TimeoutException("damn");
}
4 replies
CC#
Created by João Paulo on 5/8/2024 in #help
How can i detect a new e-mail arrival using GraphClientServer
the security code e-mail generally takes 1 minute to arrive
4 replies