Aspose.Email OAuth2 Office 365

Hi, I’m having very similar problems. I have an access token and when I try to connect, I also get an obfuscated “authentication failed” Exception. Is this problem fixed anyhow?

@SBWDeveloper

I suggest you to please try using the following implementation on your end using Aspose.Email for .NET 20.8 and in case there is still an issue, please share the details of issue incurring.

Hi, ImapClient works now, I had to regenerate the tokens and delete the cached ones with the new EWS scope. SmtpClient still gives me an “Operation has been cancelled” exception. Any idea?

@SBWDeveloper

I suggest you to please visit the following post on your end and in case there is still an issue then please share the working sample project reproducing the issue on your end.

Hi, this is my class in which I tested the SMTP client. I added the additional scopes just to be safe, but I still get cancelled operations.

class Program
{
    static void Main(string[] args)
    {
        string[] scopeAr = new string[]
                    {
                        "https://outlook.office.com/EWS.AccessAsUser.All",
                        "https://outlook.office.com/SMTP.Send",
                        "https://outlook.office.com/POP.AccessAsUser.All",
                        "https://outlook.office.com/IMAP.AccessAsUser.All"
                    };

        string username // = "test@testdomain.com";
        string password // = "password";

        var provider = new AzureROPCTokenProvider("softbauware.onmicrosoft.com", "4f399c00-b610-4d6d-83a9-9fcb5d3934ae", "9.Ur5_Nod~pUMh88K_m4zI_ff8s01--.2A", username, password, scopeAr);

        // using (ImapClient imapClient = new ImapClient("outlook.office365.com", 993, "test@softbauware.de", token, true))
        ImapClient imapClient = new ImapClient("outlook.office365.com", 993, "test@softbauware.de", provider, SecurityOptions.Auto);

        // SmtpClient smtpClient = new SmtpClient("smtp.office365.com", 587, username, password, SecurityOptions.SSLExplicit);

        // SmtpClient smtpClient = new SmtpClient("smtp.office365.com", 587, "test@softbauware.de", provider.GetAccessToken(false).Token , true);
        
        SmtpClient smtpClient = new SmtpClient("smtp.office365.com", 587, username, provider);

        smtpClient.SecurityOptions = SecurityOptions.SSLAuto;
        // smtpClient.UseTnef = false;
        // ExchangeClient exchangeClient = new ExchangeClient("https://outlook.office365.com/EWS/Exchange.asmx", username, password);

        try
        {
            /*
            imapClient.SupportedEncryption = Aspose.Email.Clients.Base.EncryptionProtocols.Tls12;
            imapClient.SecurityOptions = Aspose.Email.Clients.SecurityOptions.SSLImplicit;
            imapClient.ConnectionCheckupPeriod = 1000 * 60;
            */
            
            System.Console.WriteLine(imapClient.Username);
            /*
            imapClient.SelectFolder("INBOX");
            

            ImapMessageInfoCollection col = imapClient.ListMessages();
            foreach (ImapMessageInfo m in col)
            {
                Console.WriteLine(m.UniqueId + ", " + m.Subject);
            }
            */


            // smtpClient.SupportedEncryption = Aspose.Email.Clients.Base.EncryptionProtocols.Ssl3;
            smtpClient.Timeout = 50000;

            // smtpClient.CreateConnection();
            MailMessage message = new MailMessage(username, "receiver@domain.com", "Hello", "World");

            smtpClient.Send(message);
            Console.WriteLine(smtpClient.ConnectionState);

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }

        Console.WriteLine();
        Console.WriteLine("Please press any key..");
        Console.ReadKey();

    }
}