Dear @KRANTHI KUMAR REDDY ,
To send emails using SendGrid and Microsoft OAuth in Node.js, you will need to install the SendGrid Node.js library and the passport-azure-ad library. You can install these libraries using the following command:
npm install @sendgrid/mail passport-azure-ad
Then, you will need to require the libraries in your code and set up your SendGrid API key and Azure AD OAuth configuration. Here is an example of how you can set up the SendGrid API key and Azure AD OAuth configuration:
const sendgrid = require('@sendgrid/mail');
const passport = require('passport');
const OAuth2Strategy = require('passport-azure-ad').OIDCStrategy;// Set up SendGrid API key
sendgrid.setApiKey(process.env.SENDGRID_API_KEY);// Set up Azure AD OAuth configuration
passport.use(new OAuth2Strategy({
clientID: process.env.AZURE_CLIENT_ID,
clientSecret: process.env.AZURE_CLIENT_SECRET,
callbackURL: process.env.AZURE_CALLBACK_URL,
resource: 'https://graph.microsoft.com',
tenant: process.env.AZURE_TENANT_ID,
responseType: 'code',
responseMode: 'query'
},
function (accessToken, refreshToken, params, profile, done) {
return done(null, { accessToken, refreshToken, params, profile });
}
));
Once you have set up the API key and OAuth configuration, you can use the SendGrid API to send emails using the following code:
// Set up email parameters
const msg = {
to: 'recipient@ssss .com',
from: 'sender@ssss .com',
subject: 'Test email using SendGrid and Microsoft OAuth',
text: 'This is a test email sent using SendGrid and Microsoft OAuth.'
};// Send the email
sendgrid.send(msg);
Good luck!