Why is my mail not getting sent?

asked 2020-09-22T13:08:32.48+00:00
Abhijit 411 Reputation points

I have this bit of code that should send an email from the Office365 domain. This code doesn't throw any errors, but doesn't send any email either.

using Microsoft.Graph;
using Microsoft.Graph.Auth;
using Microsoft.Identity.Client;
using OutlookMail.Classes;
using System;
using System.Collections.Generic;
using System.Web.UI;

namespace OutlookMail
{
public partial class _Default : Page
{
protected void send_Click(object sender, System.EventArgs e)
{

        const string tenantId = "9188040d-6c67-4c5b-b112-36a304b66dad";
        const string redirectUri = "https://localhost:44316/";
        const string clientSecret = "APPSECRET";
        const string clientId = "1406d9bf-45b2-4347-8dbd-23506423ee07";
        const string MSGraphScope = "https://graph.microsoft.com/.default";

    const string AuthorityFormat = "https://login.microsoftonline.com/{0}/v2.0";
        IConfidentialClientApplication daemonClient;
        daemonClient = ConfidentialClientApplicationBuilder.Create(clientId)
            .WithAuthority(string.Format(AuthorityFormat, tenantId))
            .WithRedirectUri(redirectUri)
            .WithClientSecret(clientSecret)
            .Build();

        ClientCredentialProvider authProvider = new ClientCredentialProvider(daemonClient,"Mail.Send");

        var authResult = daemonClient.AcquireTokenForClient(new[] { MSGraphScope }).ExecuteAsync();
        var authResult2 = authResult.Result;

        GraphServiceClient graphClient = new GraphServiceClient(authProvider);
        SendEmail(graphClient);
    }

    private void SendEmail(GraphServiceClient graphClient)
    {
        var message = new Message
        {
            Subject = "Hello From Daemon Process",
            Body = new ItemBody
            {
                ContentType = BodyType.Text,
                Content = "The new cafeteria is open."
            },
            ToRecipients = new List<Recipient>()
                    {
                    new Recipient
                    {
                        EmailAddress = new EmailAddress
                        {
                            Address = "AdeleV@testariesllc.onmicrosoft.com"
                        }
                    }
        },
            CcRecipients = new List<Recipient>()
            {
                new Recipient
                {
                    EmailAddress = new EmailAddress
                    {
                        Address = "abhims365@testariesllc.onmicrosoft.com"
                    },

                },
                new Recipient
                {
                    EmailAddress = new EmailAddress
                    {
                        Address = "AlexW@testariesllc.onmicrosoft.com"
                    }
                }
            }
        };

        var saveToSentItems = true;
        try
        {
            graphClient.Users["adelev@testariesllc.onmicrosoft.com"].SendMail(message, saveToSentItems).Request().PostAsync();
        }
        catch(Exception e)
        {
            Label1.Text = e.Message;
        }
    }

}

}

Azure Active Directory
Azure Active Directory
An Azure enterprise identity service that provides single sign-on and multi-factor authentication.
12,581 questions
{count} votes