I have a work around.
I noticed that when the error occured, the mail was in the drafts folder, but got stuck there.
Work around;
Step 1 : instead of message.send() or message.sendandsave() , use message.Save(WellKnownFolderName.Drafts);
Step 2 : read the drafts folder (you can make a filter list of the mails in the drafts folder) and sendandsave() the drafts mail.
try
{
var cca = ConfidentialClientApplicationBuilder
.Create(L_FactuurmailAppid)
.WithClientSecret(L_FactuurmailClientSecret)
.WithTenantId(L_FactuurmailTenant)
.Build();
var ewsScopes = new string[] { "https://outlook.office365.com/.default" };
var authResult = cca.AcquireTokenForClient(ewsScopes).ExecuteAsync().GetAwaiter().GetResult();
ExchangeService exchange = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
exchange.Timeout = 100000000;
exchange.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
exchange.Credentials = new OAuthCredentials(authResult.AccessToken);
// exchange.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, L_FactuurmailFrom);
exchange.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "your_email_adres@test.com");
exchange.Timeout = 1000000;
exchange.KeepAlive = true;
//// verstuur alle mails uit DRAFTS FOLDER
// Bind to the Drafts folder
FolderId draftsFolderId = new FolderId(WellKnownFolderName.Drafts);
// Define a search filter for the subject containing "your_mailsubject_selection_text"
SearchFilter searchFilter = new SearchFilter.ContainsSubstring(ItemSchema.Subject, "your_mailsubject_selection_text");
// Define the item view to specify the number of items to retrieve
ItemView view = new ItemView(100); // Adjust the number if needed
// Find items in the Drafts folder matching the search filter
FindItemsResults<Item> draftItems = exchange.FindItems(draftsFolderId, searchFilter, view);
foreach (Item item in draftItems.Items)
{
if (item is EmailMessage draftEmail)
{
// Send the email
draftEmail.SendAndSaveCopy();
}
}
}
catch (Exception ex)
{
MessageBox.Show("Sending from DRAFTS FOLDER failes !\n" + ex.Message.ToString());
}
```Hope this helps
Additional notice:
I was too optimistic. I had to build a loop around the emails in Drafts folder untill Drafts folder is empty