Friday, September 20, 2024

Send email using graph api if multi-factor authentication is enable using Function Account

 

If you are new to Graph API and want to send an email, then you are in the right place. If your account has multi-factor authentication disabled, just like a noreply account, let’s not waste any time and start coding. Please note, I am using Graph API version 5.56. So, let’s go! 

1st create interface

public interface IGraphMailService

{

 Task SendMailAsync(string fromAddress,string toAddress, string subject, string content, string ccEmail);

}

Service code 

using Azure.Identity;

using Dow.Mjeapi.Biz.Interfaces;

using Dow.Mjeapi.Data.Models;

using Microsoft.Extensions.Configuration;

using Microsoft.Graph;

using Microsoft.Graph.Me.SendMail;

using Microsoft.Graph.Models;

using Microsoft.Identity.Client;

using System;

using System.Collections.Generic;

using System.Threading.Tasks;

using static System.Formats.Asn1.AsnWriter;

namespace 

{

    public class GraphMailService : IGraphMailService

    {

        private readonly IConfiguration _config;

        private const string TenantId = "TenantId ";

        private const string ClientId = "ClientId "; 

        private static string[] scopes = new string[] { "https://graph.microsoft.com/.default" };

        private const string ClientSecret = "YOURClientSecret ";

        public GraphMailService(IConfiguration config)

        {

            _config = config;

        }

        private static IPublicClientApplication CreatePublicClientApplication()

        {

            return PublicClientApplicationBuilder.Create(ClientId)

                .WithAuthority(AzureCloudInstance.AzurePublic, TenantId)

                .WithDefaultRedirectUri()

                .Build();

        }

        public async Task SendMailAsync( string fromAddress,string toAddress, string subject, string content, string ccEmail)

    {

        try

        {

            string? tenantId = _config[TenantId];

            string? clientId = _config[ClientId];

            string? userName =  _config[UserName];

            string? password = _config[Password];


            var scopes = new[] { "https://graph.microsoft.com/.default" };


            var usernamePasswordCredential = new UsernamePasswordCredential(userName, password, tenantId, clientId);


            var graphClient = new GraphServiceClient(usernamePasswordCredential, scopes);


            var message = new Message

            {

                Subject = subject,

                Body = new ItemBody

                {

                    ContentType = BodyType.Text,

                    Content = content

                },

                ToRecipients = new List<Recipient>

                {

                    new Recipient

                    {

                        EmailAddress = new EmailAddress

                        {

                            Address = toAddress

                        }

                    }

                },

                CcRecipients = new List<Recipient>

                {

                    new Recipient

                    {

                        EmailAddress = new EmailAddress

                        {

                            Address = ccEmail

                        }

                    }

                }

                //,

                //BccRecipients = new List<Recipient>

                //{

                //    new Recipient

                //    {

                //        EmailAddress = new EmailAddress

                //        {

                //            Address = bccEmail

                //        }

                //    }

                //}

            };


            var sendMailRequestBody = new Microsoft.Graph.Me.SendMail.SendMailPostRequestBody

            {

                Message = message,

                SaveToSentItems = true

            };


            await graphClient.Me.SendMail.PostAsync(sendMailRequestBody);


            

            Console.WriteLine("Email sent successfully");

        }

        catch (Exception ex)

        {

            Console.WriteLine($"An error occurred: {ex.Message}");

        }

    }

and Controller code 

using Microsoft.AspNetCore.Mvc;

using System.Threading.Tasks;

using System;

using Microsoft.AspNetCore.Authorization;


namespace ANCD.Test.Controllers

{

    [ApiController]

    [Route("api/[controller]")]

    [Authorize]

    public class EmailController : ControllerBase

    {

        private readonly IGraphMailService _graphMailService;


        public EmailController(IGraphMailService graphMailService)

        {

            _graphMailService = graphMailService;

        }

        [HttpPost("send")]

        public async Task<IActionResult> SendMail([FromBody] MailRequestNew mailRequest)

        {

            if (mailRequest == null || string.IsNullOrEmpty(mailRequest.FromAddress) || string.IsNullOrEmpty(mailRequest.ToAddress) || string.IsNullOrEmpty(mailRequest.Subject) || string.IsNullOrEmpty(mailRequest.Content))

            {

                return BadRequest("Invalid mail request.");

            }


            try

            {

                await _graphMailService.SendMailAsync(mailRequest.FromAddress, mailRequest.ToAddress, mailRequest.Subject, mailRequest.Content, mailRequest.CcEmail);

                return Ok("Mail sent successfully.");

            }

            catch (Exception ex)

            {

                return StatusCode(500, $"Internal server error: {ex.Message}");

            }

        }


    }


    public class MailRequestNew

    {

        public string FromAddress { get; set; }

        public string ToAddress { get; set; }

        public string Subject { get; set; }

        public string Content { get; set; }

        public string CcEmail { get; set; }

    }


}

 adjust the azure permission  if your are issue to send email these code is tested from my end  Thank you.

Happy codding


Send email using graph api if multi-factor authentication is disable

If you are new to Graph API and want to send an email, then you are in the right place. If your account has multi-factor authentication disabled, just like a noreply account, let’s not waste any time and start coding. Please note, I am using Graph API version 5.56. So, let’s go! 

1st create interface

public interface IGraphMailService

{

  Task SendEmailAsync(Message mail);

}

Service code 

using Azure.Identity;

using Dow.Mjeapi.Biz.Interfaces;

using Dow.Mjeapi.Data.Models;

using Microsoft.Extensions.Configuration;

using Microsoft.Graph;

using Microsoft.Graph.Me.SendMail;

using Microsoft.Graph.Models;

using Microsoft.Identity.Client;

using System;

using System.Collections.Generic;

using System.Threading.Tasks;

using static System.Formats.Asn1.AsnWriter;

namespace 

{

    public class GraphMailService : IGraphMailService

    {

        private readonly IConfiguration _config;

        private const string TenantId = "TenantId ";

        private const string ClientId = "ClientId "; 

        private static string[] scopes = new string[] { "https://graph.microsoft.com/.default" };

        private const string ClientSecret = "YOURClientSecret ";

        public GraphMailService(IConfiguration config)

        {

            _config = config;

        }

        private static IPublicClientApplication CreatePublicClientApplication()

        {

            return PublicClientApplicationBuilder.Create(ClientId)

                .WithAuthority(AzureCloudInstance.AzurePublic, TenantId)

                .WithDefaultRedirectUri()

                .Build();

        }

        public async Task SendEmailAsync(Message mail)

        {

            var clientSecretCredential = new ClientSecretCredential(TenantId, ClientId, ClientSecret);

var graphClient = new GraphServiceClient(clientSecretCredential, scopes);

var sendMailBody = new Microsoft.Graph.Users.Item.SendMail.SendMailPostRequestBody

            {

                Message = mail,

                SaveToSentItems = true

            };

            try

            {

                await graphClient.Users["mjesupport@dow.com"].SendMail.PostAsync(sendMailBody);

            }

            catch (Exception e)

            {

                Console.WriteLine($"Error sending email: {e.Message}");

            }

        }   

    }

}

and Controller code 

[HttpPost("send")]

public async Task<IActionResult> SendEmail(string recipientEmail, string subject, string body)

{

    var message = CreateEmailMessage(recipientEmail, subject, body);

    await _graphMailService.SendEmailAsync(message);

    return Ok("Email sent successfully");

}

 private Message CreateEmailMessage(string recipientEmail, string subject, string body)

 {

     return new Message

     {

         Subject = subject,

         Body = new ItemBody

         {

             ContentType = BodyType.Text,

             Content = body

         },

         ToRecipients = new List<Recipient>

     {

         new Recipient

         {

             EmailAddress = new EmailAddress

             {

                 Address = recipientEmail

             }

         }

     }

     };

 } adjust the azure permission if your are issue to send email these code is tested from my end  Thank you.

Happy codding


Tuesday, July 23, 2024

https://login.microsoftonline.com/c3e32f53-cb7f-4809-968d-1cc4ccc785fe/oauth2/v2.0/token error in angular 17

 Dear Friend,

If you are working on a single-page application using MSAL Angular, I am 100% sure you have encountered this error in your app.

Let’s cut to the chase; here is the solution to fix it. But before we get to the solution, let’s understand what the error is.



 

The error appears to be the same; the issue arises after login, when it redirects to a URL that is not available or accessible.
The solution is 

{
 
  "msalConfig": {
    "auth": {
      "clientId": "abcs",
      "authority": "https://login.microsoftonline.com/",
      "redirectUri": "https://127.0.0.1:8007" //https://localhost:8007
    }
}

Change this url "redirectUri": "https://127.0.0.1:8007" to https://localhost:8007

Thank you, Hope it will help you

Thursday, July 18, 2024

NU1101 Unable to find package Azure.Identity. No packages exist with this id in source(s): C:\Program Files\dotnet\library-packs, Microsoft Visual Studio Offline Packages test.Common C:\Users\NE18310\source\repos\APM0021023-mje\svc-mje-api\source\test.Common\test.csproj

 NU1101 Unable to find package Azure.Identity. No packages exist with this id in source(s): C:\Program Files\dotnet\library-packs, Microsoft Visual Studio Offline Packages test.Common C:\Users\NE18310\source\repos\APM0021023-mje\svc-mje-api\source\test.Common\test.csproj

if you get this error then how can you fix this in window 11.
Ans:- Probably, you need to add another valid package source for VS to find the specific packages.

Please click Tools > NuGet Package Manager > Package Manager Settings > Package Sources > check if nuget.org package source has been added. If it hasn’t been added, please click the “Add” button on top right corner > change the Name to nuget.org and the Source to https://api.nuget.org/v3/index.json > click Update > click OK, then rebuild your project.

If it has been added, please try to remove other invalid package sources. Hope it will helpful to you.



Friday, February 2, 2024

Execute an SSIS package at regular intervals using SQL Server Agent

you can execute an SSIS package at regular intervals using SQL Server Agent, which is a component of SQL Server used for scheduling and automating tasks, including SSIS package execution. Here's how you can schedule the execution of an SSIS package at intervals:

  1. Create SQL Server Agent Job:

    • Open SQL Server Management Studio (SSMS).
    • Connect to your SQL Server instance.
    • Expand the "SQL Server Agent" node.
    • Right-click on "Jobs" and select "New Job...".
  2. Define Job Details:

    • In the "General" tab of the "New Job" dialog, provide a name for the job and an optional description.
  3. Add Job Step:

    • In the "Steps" tab of the "New Job" dialog, click on "New..." to add a new job step.
    • In the "New Job Step" dialog, provide a name for the step and select "SQL Server Integration Services Package" as the type.
    • Choose the appropriate SSIS package source (e.g., File System, SSIS Package Store, SQL Server).
    • Select the SSIS package you want to execute.
  4. Schedule Job:

    • In the "Schedules" tab of the "New Job" dialog, click on "New..." to add a new schedule.
    • Configure the schedule by specifying the frequency (e.g., daily, weekly, monthly) and the interval at which you want the job to run.
    • Set the start date and time for the schedule.
    • Optionally, configure other schedule options such as end date, recurrence pattern, and notification settings.
  5. Configure Notifications (Optional):

    • In the "Notifications" tab of the "New Job" dialog, configure email notifications to be sent upon job completion or failure.
  6. Save Job:

    • Click "OK" to save the job configuration.

Once the job is created and scheduled, SQL Server Agent will automatically execute the SSIS package according to the defined schedule.

Additionally, you can also use external scheduling tools or services (e.g., Windows Task Scheduler, Azure Automation) to execute SSIS packages at intervals if you prefer. These tools provide additional flexibility and options for scheduling and managing automated tasks. 

Monday, January 15, 2024

Var Vs Dynamic Keywords In C#

If you're working with C# code, I assume you're utilizing the 'var' and 'dynamic' keywords. Here are some highlighted points that I hope you find helpful.

Var Vs Dynamic Keywords In C#

When were they introduced

  • var was introduced in C# 3.0
  • dynamic was introduced in C# 4.0.

Type inference of variables

  • var is a statically typed variable. It results in a strongly typed variable, in other words the data type of these variables are inferred at compile time. This is done based on the type of value that these variables are initialized with.
  • dynamic are dynamically typed variables. This means, their type is inferred at run-time and not the compile time in contrast to var type.

Initialization of variables

  • var type of variables are required to be initialized at the time of declaration or else they encounter the compile time error: Implicitly-typed local variables must be initialized.
  • dynamic type variables need not be initialized when declared.

If you want to understand kindly refer this link click here  


Thank you ,

Friday, August 4, 2023

Angular Upgrade | How to upgrade from Angular 10 to 11 ?

 In this article, we will learn how to implement angular 10 to angular 11 upgrades. you will learn to update @angular 10 to 11. this example will help you upgrade angular CLI to 11 in Ubuntu. Here you will learn to update angular 10 to 11. Follow the below tutorial step on how to update angular versions 10 to 11.

I have two ways to upgrade your angular 10 to 11 first we will directly update your angular cli version. if it's not working for you then you can uninstall and reinstall your angular cli with the below steps.

Let's see both ways:


Way 1:- 
Step 1) Update Angular CLI 
          ng update @angular/core@11 @angular/cli@11 --force

Step 2 ) If you're using angular/material the update command is as below 
           ng update @angular/material@11 --force

Step 3) Use ng update command for typescript update. 

If everything is ok the use ng version command to check the version of angular CLI.

Way 2:- 
Step 1) Uninstall Angular CLI:
          npm uninstall -g @angular/cli
Clear Cache:
         npm cache clean --force
         npm cache verify  

Install Angular CLI:
         ng update @angular/cli@11 @angular/core@11 --allow-dirty --force



Then check the version using ng version, 

Hope it is helpful for you Keep learning.