|
| 1 | +--- |
| 2 | +title: include file |
| 3 | +description: include file |
| 4 | +author: yogeshmo |
| 5 | +manager: koagbakp |
| 6 | +services: azure-communication-services |
| 7 | +ms.author: yogeshmo |
| 8 | +ms.date: 11/21/2023 |
| 9 | +ms.topic: include |
| 10 | +ms.service: azure-communication-services |
| 11 | +ms.custom: mode-other |
| 12 | +--- |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | + |
| 16 | +- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/dotnet/). |
| 17 | +- An Azure Email Communication Services Resource ready to provision domains. [Get started creating an Email Communication Resource](../create-email-communication-resource.md). |
| 18 | +- An [Azure Managed Domain](../add-azure-managed-domains.md) or [Custom Domain](../add-custom-verified-domains.md) provisioned and ready to send emails. |
| 19 | +- We're using a [service principal for authentication](../../../../active-directory/develop/howto-create-service-principal-portal.md). Set the values of the client ID, tenant ID, and client secret of the Azure Active Directory (AD) application as the following environment variables: `AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, and `AZURE_CLIENT_SECRET`. |
| 20 | + |
| 21 | +## Install the required packages |
| 22 | + |
| 23 | +Add the following dependencies to your `pom.xml`. |
| 24 | + |
| 25 | +``` |
| 26 | +<dependency> |
| 27 | + <groupId>com.azure.resourcemanager</groupId> |
| 28 | + <artifactId>azure-resourcemanager-communication</artifactId> |
| 29 | + <version>2.2.0</version> |
| 30 | +</dependency> |
| 31 | +<dependency> |
| 32 | + <groupId>com.azure</groupId> |
| 33 | + <artifactId>azure-identity</artifactId> |
| 34 | + <version>1.11.1</version> |
| 35 | +</dependency> |
| 36 | +``` |
| 37 | + |
| 38 | + |
| 39 | +## Initialize the management client |
| 40 | + |
| 41 | +Set the environment variable `AZURE_SUBSCRIPTION_ID` with the subscription ID of the subscription your Domain and Email resources are in. |
| 42 | + |
| 43 | +Add the following imports at the top of your file. |
| 44 | + |
| 45 | +```java |
| 46 | +import com.azure.core.credential.TokenCredential; |
| 47 | +import com.azure.core.management.AzureEnvironment; |
| 48 | +import com.azure.core.management.profile.AzureProfile; |
| 49 | +import com.azure.identity.DefaultAzureCredentialBuilder; |
| 50 | +import com.azure.resourcemanager.communication.CommunicationManager; |
| 51 | +``` |
| 52 | + |
| 53 | +Run the code sample to initialize the management client. |
| 54 | + |
| 55 | +```java |
| 56 | +AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE); |
| 57 | +TokenCredential credential = new DefaultAzureCredentialBuilder() |
| 58 | + .authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint()) |
| 59 | + .build(); |
| 60 | + |
| 61 | +CommunicationManager manager = CommunicationManager |
| 62 | + .authenticate(credential, profile); |
| 63 | +``` |
| 64 | + |
| 65 | +## Add a suppression list to a domain resource |
| 66 | + |
| 67 | +To block your email messages from being sent to certain addresses, the first step is setting up a suppression list in your domain resource. |
| 68 | + |
| 69 | +Update the code sample with the resource group name, the email service name, and the domain resource name for which you would like to create the suppression list. Find this information in the portal by navigating to the domain resource you created when setting up the prerequisites. The title of the resource is `<your-email-service-name>/<your-domain-name>`. Find the resource group name and subscription ID in the Essentials sections in the domain resource overview. Choose any name for your suppression list resource and update that field in the sample as well. |
| 70 | + |
| 71 | +For the list name, make sure it's the same as the sender username of the MailFrom address you would like to suppress emails from. These MailFrom addresses can be found in the "MailFrom addresses" section of your domain resource in the portal. For example, you may have a MailFrom address that looks like " [email protected]". The sender username for this address would be "donotreply" so a list name of "donotreply" should be used. |
| 72 | + |
| 73 | +```java |
| 74 | +String resourceGroupName = "<your-resource-group-name>"; // Found in the essentials section of the domain resource portal overview |
| 75 | +String emailServiceName = "<your-email-service-name>"; // Found in the first part of the portal domain resource title |
| 76 | +String domainResourceName = "<your-domain-name>"; // Found in the second part of the portal domain resource title |
| 77 | +String suppressionListResourceName = "<your-suppression-list-resource-name>"; |
| 78 | + |
| 79 | +manager.suppressionLists().define(suppressionListResourceName) |
| 80 | + .withExistingDomain(resourceGroupName, emailServiceName, domainResourceName) |
| 81 | + .withListName("<your-sender-username>") // Should match the sender username of the MailFrom address you would like to suppress emails from |
| 82 | + .create(); |
| 83 | +``` |
| 84 | + |
| 85 | +If you would like to suppress emails from all the sender usernames in particular domain, you can pass in an empty string for the list name. |
| 86 | + |
| 87 | +```java |
| 88 | +manager.suppressionLists().define(suppressionListResourceName) |
| 89 | + .withExistingDomain(resourceGroupName, emailServiceName, domainResourceName) |
| 90 | + .withListName("") |
| 91 | + .create(); |
| 92 | +``` |
| 93 | + |
| 94 | +## Add an address to a suppression list |
| 95 | + |
| 96 | +After setting up the suppression list, you can now add specific email addresses to which you wish to prevent your email messages from being sent. |
| 97 | + |
| 98 | +Update the code sample with the suppression list address ID. Every suppression list address ID you add needs to be unique. We recommend using a GUID. Update the email address you want to block from receiving your messages as well. |
| 99 | + |
| 100 | +To add multiple addresses to the suppression list, you need to repeat this code sample multiple times. |
| 101 | + |
| 102 | +```java |
| 103 | +String suppressionListAddressId = "<your-suppression-list-address-id>"; |
| 104 | + |
| 105 | +manager.suppressionListAddresses().define(suppressionListAddressId) |
| 106 | + .withExistingSuppressionList(resourceGroupName, emailServiceName, domainResourceName, suppressionListResourceName) |
| 107 | + .withEmail("<email-address-to-suppress>") // Should match the email address you would like to block from receiving your messages |
| 108 | + .create(); |
| 109 | +``` |
| 110 | + |
| 111 | +You can now try sending an email to the suppressed address from the [`TryEmail` section of your Communication Service resource or by using one of the Email SDKs](../send-email.md). Make sure to send the email using the MailFrom address with the sender username you've chosen to suppress. Your email won't send to the suppressed address. |
| 112 | + |
| 113 | +If you try sending an email from a sender username that is not suppressed the email still successfully sends. |
| 114 | + |
| 115 | +## Remove an address from a suppression list |
| 116 | + |
| 117 | +Call the `delete` method on `suppressionListAddresses` to remove an address from the suppression list. |
| 118 | + |
| 119 | +```java |
| 120 | +manager.suppressionListAddresses() |
| 121 | + .delete(resourceGroupName, emailServiceName, domainResourceName, suppressionListResourceName, suppressionListAddressId); |
| 122 | +``` |
| 123 | + |
| 124 | +You can now try sending an email to the suppressed address from the [`TryEmail` section of your Communication Service resource or by using one of the Email SDKs](../send-email.md). Make sure to send the email using the MailFrom address with the sender username you choose to suppress. Your email will successfully send to the previously suppressed address. |
| 125 | + |
| 126 | +## Remove a suppression list from a domain resource |
| 127 | + |
| 128 | +Call the `delete` method on `suppressionLists` to remove a suppression list from the domain resource. |
| 129 | + |
| 130 | +```java |
| 131 | +manager.suppressionLists() |
| 132 | + .delete(resourceGroupName, emailServiceName, domainResourceName, suppressionListResourceName); |
| 133 | +``` |
0 commit comments