package com.mycorp; import com.microsoft.graph.auth.confidentialClient.ClientCredentialProvider; import com.microsoft.graph.auth.enums.NationalCloud; import java.util.List; import java.util.Map; import java.util.HashMap; import com.microsoft.graph.models.extensions.IGraphServiceClient; import com.microsoft.graph.models.extensions.Invitation; import com.microsoft.graph.models.extensions.InvitedUserMessageInfo; import com.microsoft.graph.requests.extensions.GraphServiceClient; import java.util.Arrays; import com.google.gson.JsonElement; import com.google.gson.JsonObject; public class sendCustomInvitationEmail { public static String TENANT="your TenantID";// replace your tenantId not client Id,tenantId is diff from client Id public static NationalCloud NATIONAL_CLOUD=NationalCloud.Global; public static String CLIENT_ID = "your ClientID"; public static String SCOPE = "https://graph.microsoft.com/.default"; public static ListSCOPES = (List) Arrays.asList(SCOPE); public static String CLIENT_SECRET = "your Client Secret"; public static void main(String[] args) { ClientCredentialProvider authProvider = new ClientCredentialProvider(CLIENT_ID, SCOPES, CLIENT_SECRET, TENANT, NATIONAL_CLOUD); IGraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider(authProvider).buildClient(); JsonObject respJson = new JsonObject(); Invitation invitation = new Invitation(); invitation.invitedUserEmailAddress = "abcd@gmail.com";// to whom we want to send the invitation.this email should exist, you can give your gmail to see the invitation email body... invitation.inviteRedirectUrl = "https://google.co.in";// on click of invitation acceptance link, user will land in this page invitation.sendInvitationMessage = false; /** From this eclipse for testing purpose we can make sendInvitationMessage=true so that invitation email triggers to the "invitedUserEmailAddress" we have given above in line # 38. in that way we can see/test that the email is going out with customized body or not ?? true will send the invite, false will NOR send the invite, so from eclipse you can make it as true to see the actual email going out with the Customized body, But when we migrate to IIQ, we need make this as false. **/ invitation.invitedUserMessageInfo = createYourOwnCustomzedInvitation();// *** kindly go through this method which provided control over customizations Invitation invokeInvitation = graphClient.invitations().buildRequest().post(invitation);//this actually sends out an invitation email with the custom body /* till above line #52 is sufficient to send invitation email with the customized text from java program , once we confirm that email is going with * the customized body with the text , now we can make sendInvitationMessage as false(i.e. stop sending this email) * why we want to stop sending email bcz the email template that triggers from Microsoft AZURE will not in line with * many of the customers email conventions/standards. * * That is why invoke the invite by making this flag false and collect the response body(literally everything you can get like redeemUrl,invitedUserEmailAddress,inviteRedirectUrl..etc) * Then construct your own IIQ email template with HTML tags, client specific images client specific company Logo and so on * From line # 52 collect the response Json then send IIQ Email with your own template. * below lines are to send the customized email from IIQ * */ if(null != invokeInvitation) { respJson = invokeInvitation.getRawObject();// we need to catch from the response json and need to parse HashMap iiqSendEmailMap = parseResponseJson(respJson); /** * now by using IIQ context.sendEmailNotification(,emailOptions) you need to parse the iiqSendEmailMap and send the email * note:iiqSendEmailMap contains everything that you need(i.e. what ever OOTB Azure email template has) \ * in that way we are bypassing the AZURE OOTB email and we can customize this email template the way we want */ } } /** * purpose of this method is from the json response get all the required attributes and values and create our own map so that we * can send email by using IIQ * **/ private static HashMap parseResponseJson(JsonObject respJson) { Map returnMap = new HashMap(); /** while sending the email from IIQ this map will be used , so we are putting all the values in this map and keeping it ready **/ JsonObject responseJson = respJson.getAsJsonObject(); JsonElement customizedBody = null; JsonElement invitationRedeemURL = responseJson.get("inviteRedeemUrl"); if(null !=invitationRedeemURL) { returnMap.put("inviteRedeemUrl", invitationRedeemURL.toString()); } JsonElement invitedUserEmailAddress = responseJson.get("invitedUserEmailAddress"); if(null !=invitedUserEmailAddress) { returnMap.put("invitedUserEmailAddress", invitedUserEmailAddress.toString()); } JsonElement inviteRedirectUrl = responseJson.get("inviteRedirectUrl"); if(null !=inviteRedirectUrl) { returnMap.put("inviteRedirectUrl", inviteRedirectUrl.toString()); } JsonObject invitedUserMsgInfoJson = responseJson.getAsJsonObject("invitedUserMessageInfo"); if(null != invitedUserMsgInfoJson) { customizedBody = invitedUserMsgInfoJson.get("customizedMessageBody"); if(null != customizedBody){ returnMap.put("customizedBody", customizedBody.toString()); } } return null; } private static InvitedUserMessageInfo createYourOwnCustomzedInvitation() { InvitedUserMessageInfo invitedUserMessageInfo = new InvitedUserMessageInfo(); invitedUserMessageInfo.customizedMessageBody = "Here you can customize the invitation msg body"; /** you can customize the body what ever you want like HTML tags stuff like that.. **/ return invitedUserMessageInfo; } }