While refactoring code from "Microsoft.Graph 4.48" to the latest 5.25 (via NuGet) I was unable to send a message nor alternately create a draft message.
The code stalls at PostAsync().Wait when sending email and stalls at PostAsync().Result when creating a Draft message.
For this example, I created a new "Windows Forms App (.NET Framework)" project. Using NuGet, added the latest "Microsoft Graph" and "Azure Identity".
If I call the "SendTest()" from Program.cs, the call works.
namespace TestSendEmail
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1.SendTest();
Application.Run(new Form1());
}
}
}
If I call "SendTest()" in the Form1() code, it hangs waiting for the call to complete.
public Form1()
{
SendTest();
InitializeComponent();
}
If I interact with the UI while the "task" is waiting, then the call will work (See Simple Test 3).
public static void SendTest()
{
var tenantId = "your-tenant-id";
var clientId = "your-client-id";
var clientSecret = "your-client-secret";
var toAddress = "test@test.com.au";
var fromAddress = "testfrom@test.com.au";
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var scopes = new[] { "https://graph.microsoft.com/.default" };
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
Microsoft.Graph.Models.Message msg = new Microsoft.Graph.Models.Message
{
Subject = "Meet for lunch?",
Body = new ItemBody
{
ContentType = BodyType.Text,
Content = "The new cafeteria is open.",
},
ToRecipients = new List<Recipient>
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = toAddress,
},
},
},
};
//Simple Test 1
//This sits forever waiting for the ".Wait()"
//var requestBody = new Microsoft.Graph.Users.Item.SendMail.SendMailPostRequestBody
//{
// Message = msg,
//};
//graphClient.Users[fromAddress].SendMail.PostAsync(requestBody).Wait();
//Simple Test 2
//This sits forever waiting for the ".Result"
var draftMessage = graphClient.Users[fromAddress].Messages.PostAsync(msg).Result;
System.Diagnostics.Debug.WriteLine(draftMessage.Id);
//Simple Test 3
//var task = graphClient.Users[fromAddress].Messages.PostAsync(msg);
//MessageBox.Show("a message here fixes the call");
//var draftMessage2 = task.Result;
//System.Diagnostics.Debug.WriteLine(draftMessage2.Id);
MessageBox.Show("success");
}