sending email asynchronous from viewmodel
I am trying to send emails asynchronous, to several contacts. But for some reason it destroys the thread, before sending the email, and if I check my gmail sent messages, I don't see the message sent, however, if I change to synchronous, it will work, but it will block the UI.
try {
var smtpClient = new SmtpClient("smtp.gmail.com") {
Port = 587,
Credentials = new System.Net.NetworkCredential(Constants.EMAIL, Constants.APP_PASSWORD),
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network
};
foreach(var recipient in users) {
var mail = new MailMessage() {
From = new MailAddress(Constants.EMAIL),
Subject = "DemyIA meeting",
IsBodyHtml = true,
};
var body = GenerateHttmlTemplate(roomName, meetingLink, user!.Name, dateTimeMeeting);
mail.Body = body;
// Add the recipient to the message
mail.To.Add(recipient.Email);
// Send the email asynchronously and await the task
await smtpClient.SendMailAsync(mail);
}
Console.WriteLine("Email sent successfully to the SMTP server.");
} catch(Exception e) {
await Console.Out.WriteLineAsync(e.Message);
}
I deug, I can see
thread '.NET TP Worker' (0x6a00) has exited with code 0 (0x0).The thread 'NET TP Worker' (0x23a0) has exited with code 0 (0x0).The thread 0x9f00 has exited with code 0 (0x0).The thread '.NET TP Worker' (0x93d0) has exited with code 0 (0x0).The thread '.NET TP Worker' (0x9874) has exited with code 0 (0x0).The thread '.NET TP Worker' (0xaf78) has exited with code 0 (0x0).The thread '.NET TP Worker' (0x428c) has exited with code 0 (0x0).
I am using that method here.
[RelayCommand]
async Task StartMeeting() {
if(string.IsNullOrEmpty(RoomName)) {
await appService.DisplayAlert("Error", "The room name cannot be empty", "OK");
return;
}
var loogedUser = await authenticationService.GetLoggedInUser();
var databaseUser = await dataService.GetByUidAsync<User>("Users", loogedUser!.Uid);
var meetingOptions = new MeetingOptions {
EnableAdvancedChat = true,
EnableEmojiReactions = true,
EnableNoiseCancellationUi = true,
EnableHandRaising = true,
EnablePrejoinUi = true,
EnablePipUi = true,
EnableScreenshare = true,
EnableVideoProcessingUi = true,
EnablePeopleUi = false,
EnableChat = true,
// Set other meeting option properties as needed
};
try {
var roomURL = await meetingService.CreateMeetingAsync(RoomName, meetingOptions, Constants.DAILY);
await EmailHelper.SendEmail(Invited, RoomName, databaseUser, roomURL, null);
} catch(Exception ex) {
// Handle any exceptions that might occur during the meeting creation
await appService.DisplayAlert("Error", message: $"Error creating meeting: {ex.Message}", "OK");
}