|
| 1 | +# Azure Communication Email client library for Java |
| 2 | + |
| 3 | +This package contains the Java SDK for Azure Communication Services for Email. |
| 4 | + |
| 5 | +## Getting started |
| 6 | + |
| 7 | +### Prerequisites |
| 8 | + |
| 9 | +- [Azure subscription][azure_sub] |
| 10 | +- [Communication Service Resource][communication_resource_docs] |
| 11 | +- [Email Communication Resource][email_resource_docs] with an active [Domain][domain_overview] |
| 12 | +- [Java Development Kit (JDK)](https://docs.microsoft.com/java/azure/jdk/?view=azure-java-stable) version 8 or above |
| 13 | +- [Apache Maven](https://maven.apache.org/download.cgi) |
| 14 | + |
| 15 | +To create these resource, you can use the [Azure Portal][communication_resource_create_portal], the [Azure PowerShell][communication_resource_create_power_shell], or the [.NET management client library][communication_resource_create_net]. |
| 16 | + |
| 17 | +### Include the package |
| 18 | +#### Include the BOM file |
| 19 | + |
| 20 | +Please include the azure-sdk-bom to your project to take dependency on the General Availability (GA) version of the library. In the following snippet, replace the {bom_version_to_target} placeholder with the version number. |
| 21 | +To learn more about the BOM, see the [AZURE SDK BOM README](https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/boms/azure-sdk-bom/README.md). |
| 22 | + |
| 23 | +```xml |
| 24 | +<dependencyManagement> |
| 25 | + <dependencies> |
| 26 | + <dependency> |
| 27 | + <groupId>com.azure</groupId> |
| 28 | + <artifactId>azure-sdk-bom</artifactId> |
| 29 | + <version>{bom_version_to_target}</version> |
| 30 | + <type>pom</type> |
| 31 | + <scope>import</scope> |
| 32 | + </dependency> |
| 33 | + </dependencies> |
| 34 | +</dependencyManagement> |
| 35 | +``` |
| 36 | + |
| 37 | +and then include the direct dependency in the dependencies section without the version tag. |
| 38 | + |
| 39 | +```xml |
| 40 | +<dependencies> |
| 41 | + <dependency> |
| 42 | + <groupId>com.azure</groupId> |
| 43 | + <artifactId>azure-communication-email</artifactId> |
| 44 | + </dependency> |
| 45 | +</dependencies> |
| 46 | +``` |
| 47 | + |
| 48 | +#### Include direct dependency |
| 49 | +If you want to take dependency on a particular version of the library that is not present in the BOM, |
| 50 | +add the direct dependency to your project as follows. |
| 51 | + |
| 52 | +[//]: # ({x-version-update-start;com.azure:azure-communication-email;current}) |
| 53 | +```xml |
| 54 | +<dependency> |
| 55 | + <groupId>com.azure</groupId> |
| 56 | + <artifactId>azure-communication-email</artifactId> |
| 57 | + <version>1.0.0-beta.1</version> |
| 58 | +</dependency> |
| 59 | +``` |
| 60 | +## Key concepts |
| 61 | +> More details coming soon. |
| 62 | +
|
| 63 | +## Examples |
| 64 | + |
| 65 | +`EmailClient` provides the functionality to send email messages . |
| 66 | + |
| 67 | +### Client Creation and Authentication |
| 68 | + |
| 69 | +Email clients can be created and authenticated using the connection string acquired from an Azure Communication Resource in the [Azure Portal][azure_portal]. |
| 70 | + |
| 71 | +```java readme-sample-createEmailClientWithConnectionString |
| 72 | +String connectionString = "https://<resource-name>.communication.azure.com/;<access-key>"; |
| 73 | + |
| 74 | +EmailClient emailClient = new EmailClientBuilder() |
| 75 | + .connectionString(connectionString) |
| 76 | + .buildClient(); |
| 77 | +``` |
| 78 | + |
| 79 | +Email clients can also be created and authenticated using the endpoint and Azure Key Credential acquired from an Azure Communication Resource in the [Azure Portal][azure_portal]. |
| 80 | + |
| 81 | +```java readme-sample-createEmailClientUsingAzureKeyCredential |
| 82 | +String endpoint = "https://<resource-name>.communication.azure.com"; |
| 83 | +AzureKeyCredential azureKeyCredential = new AzureKeyCredential("<access-key>"); |
| 84 | + |
| 85 | +EmailClient emailClient = new EmailClientBuilder() |
| 86 | + .endpoint(endpoint) |
| 87 | + .credential(azureKeyCredential) |
| 88 | + .buildClient(); |
| 89 | +``` |
| 90 | + |
| 91 | + |
| 92 | +### Send an Email Message |
| 93 | + |
| 94 | +To send an email message, call the `send` function from the `EmailClient`. |
| 95 | + |
| 96 | +```java readme-sample-sendEmailToSingleRecipient |
| 97 | +EmailAddress emailAddress = new EmailAddress("<recipient-email-address>"); |
| 98 | + |
| 99 | +ArrayList<EmailAddress> addressList = new ArrayList<>(); |
| 100 | +addressList.add(emailAddress); |
| 101 | + |
| 102 | +EmailRecipients emailRecipients = new EmailRecipients(addressList); |
| 103 | + |
| 104 | +EmailContent content = new EmailContent("test subject") |
| 105 | + .setPlainText("test message"); |
| 106 | + |
| 107 | +EmailMessage emailMessage = new EmailMessage("<sender-email-address>", content) |
| 108 | + .setRecipients(emailRecipients); |
| 109 | + |
| 110 | +SendEmailResult response = emailClient.send(emailMessage); |
| 111 | +System.out.println("Message Id: " + response.getMessageId()); |
| 112 | +``` |
| 113 | + |
| 114 | +### Send an Email Message to Multiple Recipients |
| 115 | + |
| 116 | +To send an email message to multiple recipients, add a object for each recipient type and an object for each recipient. |
| 117 | + |
| 118 | +```java readme-sample-sendEmailToMultipleRecipients |
| 119 | +EmailAddress emailAddress = new EmailAddress("<recipient-email-address>"); |
| 120 | +EmailAddress emailAddress2 = new EmailAddress("<recipient-2-email-address>"); |
| 121 | + |
| 122 | +ArrayList<EmailAddress> toAddressList = new ArrayList<>(); |
| 123 | +toAddressList.add(emailAddress); |
| 124 | +toAddressList.add(emailAddress2); |
| 125 | + |
| 126 | +ArrayList<EmailAddress> ccAddressList = new ArrayList<>(); |
| 127 | +ccAddressList.add(emailAddress); |
| 128 | + |
| 129 | +ArrayList<EmailAddress> bccAddressList = new ArrayList<>(); |
| 130 | +bccAddressList.add(emailAddress); |
| 131 | + |
| 132 | +EmailRecipients emailRecipients = new EmailRecipients(toAddressList) |
| 133 | + .setCc(ccAddressList) |
| 134 | + .setBcc(bccAddressList); |
| 135 | + |
| 136 | +EmailContent content = new EmailContent("test subject") |
| 137 | + .setPlainText("test message"); |
| 138 | + |
| 139 | +EmailMessage emailMessage = new EmailMessage("<sender-email-address>", content) |
| 140 | + .setRecipients(emailRecipients); |
| 141 | + |
| 142 | +SendEmailResult response = emailClient.send(emailMessage); |
| 143 | +System.out.println("Message Id: " + response.getMessageId()); |
| 144 | +``` |
| 145 | + |
| 146 | +### Send Email with Attachments |
| 147 | + |
| 148 | +Azure Communication Services support sending email with attachments. |
| 149 | + |
| 150 | +```java readme-sample-sendEmailWithAttachment |
| 151 | +File file = new File("C:/attachment.txt"); |
| 152 | + |
| 153 | +byte[] fileContent = null; |
| 154 | +try { |
| 155 | + fileContent = Files.readAllBytes(file.toPath()); |
| 156 | +} catch (Exception e) { |
| 157 | + System.out.println(e); |
| 158 | +} |
| 159 | + |
| 160 | +String b64file = Base64.getEncoder().encodeToString(fileContent); |
| 161 | + |
| 162 | +EmailAddress emailAddress = new EmailAddress("<recipient-email-address>"); |
| 163 | + |
| 164 | +ArrayList<EmailAddress> addressList = new ArrayList<>(); |
| 165 | +addressList.add(emailAddress); |
| 166 | + |
| 167 | +EmailRecipients emailRecipients = new EmailRecipients(addressList); |
| 168 | + |
| 169 | +EmailContent content = new EmailContent("test subject") |
| 170 | + .setPlainText("test message"); |
| 171 | + |
| 172 | +EmailAttachment attachment = new EmailAttachment("attachment.txt", EmailAttachmentType.TXT, b64file); |
| 173 | + |
| 174 | +ArrayList<EmailAttachment> attachmentList = new ArrayList<>(); |
| 175 | +attachmentList.add(attachment); |
| 176 | + |
| 177 | +EmailMessage emailMessage = new EmailMessage("<sender-email-address>", content) |
| 178 | + .setRecipients(emailRecipients) |
| 179 | + .setAttachments(attachmentList); |
| 180 | + |
| 181 | +SendEmailResult response = emailClient.send(emailMessage); |
| 182 | +System.out.println("Message Id: " + response.getMessageId()); |
| 183 | +``` |
| 184 | + |
| 185 | +### Get Email Message Status |
| 186 | + |
| 187 | +The result from the `send` call contains a `messageId` which can be used to query the status of the email. |
| 188 | + |
| 189 | +```java readme-sample-getMessageStatus |
| 190 | +SendStatusResult response = emailClient.getSendStatus("<sent-message-id>"); |
| 191 | +System.out.println("Status: " + response.getStatus()); |
| 192 | +``` |
| 193 | + |
| 194 | +## Troubleshooting |
| 195 | +> More details coming soon, |
| 196 | +
|
| 197 | +## Next steps |
| 198 | + |
| 199 | +- [Read more about Email in Azure Communication Services][nextsteps] |
| 200 | + |
| 201 | +## Contributing |
| 202 | + |
| 203 | +This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit [cla.microsoft.com][cla]. |
| 204 | + |
| 205 | +This project has adopted the [Microsoft Open Source Code of Conduct][coc]. For more information see the [Code of Conduct FAQ][coc_faq] or contact [opencode@microsoft.com][coc_contact] with any additional questions or comments. |
| 206 | + |
| 207 | +<!-- LINKS --> |
| 208 | + |
| 209 | +[azure_sub]: https://azure.microsoft.com/free/dotnet/ |
| 210 | +[azure_portal]: https://portal.azure.com |
| 211 | +[cla]: https://cla.microsoft.com |
| 212 | +[coc]: https://opensource.microsoft.com/codeofconduct/ |
| 213 | +[coc_faq]: https://opensource.microsoft.com/codeofconduct/faq/ |
| 214 | +[coc_contact]: mailto:opencode@microsoft.com |
| 215 | +[communication_resource_docs]: https://docs.microsoft.com/azure/communication-services/quickstarts/create-communication-resource?tabs=windows&pivots=platform-azp |
| 216 | +[email_resource_docs]: https://aka.ms/acsemail/createemailresource |
| 217 | +[communication_resource_create_portal]: https://docs.microsoft.com/azure/communication-services/quickstarts/create-communication-resource?tabs=windows&pivots=platform-azp |
| 218 | +[communication_resource_create_power_shell]: https://docs.microsoft.com/powershell/module/az.communication/new-azcommunicationservice |
| 219 | +[communication_resource_create_net]: https://docs.microsoft.com/azure/communication-services/quickstarts/create-communication-resource?tabs=windows&pivots=platform-net |
| 220 | +[package]: https://www.nuget.org/packages/Azure.Communication.Common/ |
| 221 | +[product_docs]: https://aka.ms/acsemail/overview |
| 222 | +[nextsteps]: https://aka.ms/acsemail/overview |
| 223 | +[nuget]: https://www.nuget.org/ |
| 224 | +[source]: https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/communication |
| 225 | +[domain_overview]: https://aka.ms/acsemail/domainsoverview |
0 commit comments