Hi
I noticed that when sending an email with a MailMessage containing custom headers, if I use Aspose.Email.Clients.Smtp.SmtpClient it works fine and I can see my custom header in the received email. But if I use Aspose.Email.Clients.Exchange.WebService.IEWSClient then the received email does not contain my custom header at all.
using System;
using System.Threading.Tasks;
using Aspose.Email;
using Aspose.Email.Clients;
using Aspose.Email.Clients.Exchange.WebService;
using Aspose.Email.Clients.Smtp;
using Microsoft.Identity.Client;
namespace EmailHeadersTest
{
public class Program
{
private const string ToAddress = "target@****.onmicrosoft.com";
private const string Username = "source@****.onmicrosoft.com";
private const string Password = "******";
private const string TenantId = "********-****-****-****-************";
private const string ClientId = "********-****-****-****-************";
private const string EwsUrl = "https://outlook.office365.com/ews/exchange.asmx";
private const string SmtpUrl = "outlook.office365.com";
private const int SmtpPort = 587;
private static readonly string[] Scopes = new[] { "https://outlook.office.com/.default" };
public static async Task Main()
{
string accessToken = await GetAccessToken();
using (IEWSClient client = CreateEwsClient(accessToken))
{
string messageBody = $"This is a test email sent by EWS at {DateTime.Now:u}";
var message = new MailMessage(Username, ToAddress, "TEST EMAIL FROM ASPOSE - EWS", messageBody);
message.Headers.Add("x-custom123", "abc-ews"); //THIS HEADER GETS STRIPPED OUT
client.Send(message);
}
using (SmtpClient client = CreateSmtpClient(accessToken))
{
string messageBody = $"This is a test email sent by SMTP at {DateTime.Now:u}";
var message = new MailMessage(Username, ToAddress, "TEST EMAIL FROM ASPOSE - SMTP", messageBody);
message.Headers.Add("x-custom123", "def-smtp"); //THIS HEADER WORKS FINE
client.Send(message);
}
}
private static async Task<string> GetAccessToken()
{
IPublicClientApplication clientApp = PublicClientApplicationBuilder.Create(ClientId)
.WithAuthority(AzureCloudInstance.AzurePublic, TenantId, true).Build();
AuthenticationResult authResult = await clientApp.AcquireTokenByUsernamePassword(
Scopes, Username, new System.Net.NetworkCredential(string.Empty, Password).SecurePassword).ExecuteAsync();
return authResult.AccessToken;
}
private static IEWSClient CreateEwsClient(string accessToken)
{
IEWSClient client = EWSClient.GetEWSClient(EwsUrl, new OAuthNetworkCredential(accessToken));
client.LogFileName = $"{AppDomain.CurrentDomain.BaseDirectory}_log_ews.txt";
return client;
}
private static SmtpClient CreateSmtpClient(string accessToken)
{
var client = new SmtpClient();
client.Host = SmtpUrl;
client.Port = SmtpPort;
client.SecurityOptions = SecurityOptions.SSLAuto;
client.UseAuthentication = true;
client.UseDefaultCredentials = false;
client.Username = Username;
client.AccessToken = accessToken;
client.EnableLogger = true;
client.LogFileName = $"{AppDomain.CurrentDomain.BaseDirectory}_log_smtp.txt";
return client;
}
}
}
The message details of the received SMTP email show my custom header:
image.png (5.8 KB)
but there is no such header in the received EWS email.
I can see in both SMTP and EWS log files, that the custom header is being used when sending the email. For example in the EWS log file, I see this:
...
<ExtendedProperty>
<ExtendedFieldURI DistinguishedPropertySetId="InternetHeaders"
PropertyName="x-custom123" PropertyType="String" />
<Value>abc-ews</Value>
</ExtendedProperty>
...
If I look at the message properties of the emails in my Sent Items folder (annoyingly I first have to move these messages into the Inbox just so I can view the headers), then I can see that the sent messages do contain the custom headers. But the received message only contains the custom header for SMTP, and not for EWS.
This is using version 20.10.0 of Aspose.Email for .NET but I can reproduce it identically in the latest version 24.5.0.
It feels to me like this might be a Microsoft issue, but if custom headers are not supported by EWS Send then I think Aspose.Email needs to make users aware of this limitation, throw an exception on the Send or deprecate the MailMessage.Headers property etc.