简体   繁体   中英

Getting The Url property on the ExchangeService object must be set. While sending email from O365 EWS modern authentication in .net framework 4.6.1

I am getting The Url property on the ExchangeService object must be set , while sending email from O365 EWS using modern authentication.

My code works perfectly from local machine. But while deploying it on PROD IIS server it gives me error while sending email.

Below is my code.

public async void SendMail(string fromId, string toId, string body, string priMailId, string appId, string clientSecret, string tenantId)
{
    var ewsClient = await ModernAuthenticate(priMailId, appId, clientSecret, tenantId);
    if (!errorMsg)
    {
        var msg = new EmailMessage(ewsClient);
        msg.ToRecipients.Add(toId.Trim());
        msg.From = fromId;
        msg.Subject = "Test Email Kindly Ignore";

        msg.Body = "Test Email Kindly Ignore";

        System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
        ServicePointManager.SecurityProtocol = (SecurityProtocolType)768 | (SecurityProtocolType)3072;
        msg.Send();
    }
}

public async Task<ExchangeService> ModernAuthenticate(string priMailId, string appId, string clientSecret, string tenantId)
{   
    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
    ServicePointManager.SecurityProtocol = (SecurityProtocolType)768 | (SecurityProtocolType)3072;
   
    var ewsClient = new ExchangeService();

    var cca = ConfidentialClientApplicationBuilder
        .Create(appId)
        .WithClientSecret(clientSecret)
        .WithTenantId(tenantId)
        .Build();

    var ewsScopes = new string[] { "https://outlook.office365.com/.default" };

    try
    {
        var authResult = await cca.AcquireTokenForClient(ewsScopes).ExecuteAsync();

        ewsClient.Url = new Uri("https://outlook.office365.com/ews/exchange.asmx");
        ewsClient.Credentials = new OAuthCredentials(authResult.AccessToken);
        ewsClient.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, priMailId);
        
        ewsClient.HttpHeaders.Add("X-AnchorMailbox", priMailId);
    }
    catch (Exception ex)
    {
        
    }
    
    return ewsClient;
}

Can anyone help me out in this

Looking at your code you don't do anything with the exception in

    try
{
    var authResult = await cca.AcquireTokenForClient(ewsScopes).ExecuteAsync();

    ewsClient.Url = new Uri("https://outlook.office365.com/ews/exchange.asmx");
    ewsClient.Credentials = new OAuthCredentials(authResult.AccessToken);
    ewsClient.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, priMailId);
    
    ewsClient.HttpHeaders.Add("X-AnchorMailbox", priMailId);
}
catch (Exception ex)
{
    
}

So if your authentication if failing you would never know and it would never reach

ewsClient.Url = new Uri("https://outlook.office365.com/ews/exchange.asmx");

so the error your getting is what you would expect. You need to improve your error handing and debugging so you can work out what's happening in your production server (eg it could be proxying, connectivity etc).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM