Simple reproduction code using Aspose Email 23.5.1 (buggy) and plain Microsoft (working fine) :
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Aspose.Email.Clients;
using Aspose.Email.Clients.Graph;
using Aspose.Email.Mapi;
using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Graph.Models;
namespace ConsoleAppAsposeMailOAuth
{
internal class Program
{
static void Main(string[] args)
{
MSGraphAPI();
AsposeGraphAPI();
}
public static void MSGraphAPI()
{
// The client credentials flow requires that you request the
// /.default scope, and preconfigure your permissions on the
// app registration in Azure. An administrator must grant consent
// to those permissions beforehand.
var scopes = new[] { "https://graph.microsoft.com/.default" };
// Multi-tenant apps can use "common",
// single-tenant apps must use the tenant ID from the Azure portal
var tenantId = "XYZ";
// Values from app registration
var clientId = "XYZ";
var clientSecret = "XYZ";
// using Azure.Identity;
var options = new TokenCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};
// https://learn.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret, options);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
byte[] contentBytes = System.IO.File.ReadAllBytes(".\\test.ps1");
string contentType = "text/plain";
var requestBody = new Microsoft.Graph.Users.Item.SendMail.SendMailPostRequestBody
{
Message = new 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 = "firstname.lastname@mycompany.com",
},
},
},
HasAttachments = true,
Attachments = new List<Microsoft.Graph.Models.Attachment>
{
new FileAttachment
{
OdataType = "microsoft.graph.fileAttachment",
ContentBytes = contentBytes,
ContentType = contentType,
ContentId = "testing",
Name = "tesing.txt"
}
}
},
SaveToSentItems = false,
};
var task = Task.Run(async () => { await graphClient.Users["daemon@mycompany.com"].SendMail.PostAsync(requestBody); });
task.Wait();
}
public static void AsposeGraphAPI()
{
var accessParams = new AccessParameters()
{
TenantId = "XYZ",
ClientId = "XYZ",
ClientSecret = "XYZ",
UserId = "daemon@mycompany.com"
};
// prepare the message
var msg2 = new Aspose.Email.MailMessage();
msg2.Subject = "My MailMessage";
msg2.Body = "Hi, it is my message";
msg2.To.Add(new Aspose.Email.MailAddress("firstname.lastname@mycompany.com"));
msg2.Attachments.Add(new Aspose.Email.Attachment(".\\test.ps1"));
var options = new MapiConversionOptions(OutlookMessageFormat.Unicode);
var msg = MapiMessage.FromMailMessage(msg2, options);
var tokenProvider = new GraphTokenProvider(accessParams);
using (var client = GraphClient.GetClient(tokenProvider, accessParams.TenantId))
{
client.Resource = ResourceType.Users;
client.ResourceId = accessParams.UserId;
try
{
// send message
client.Send(msg);
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}
}
Using Aspose Email is not sending attachement, when using plain MS is sending attachement, see screenshots :
image.png (15.2 KB)
Same problem for embedded elements (like embedded images)