|
| 1 | +--- |
| 2 | +title: include file |
| 3 | +description: include file |
| 4 | +author: yogeshmo |
| 5 | +manager: koagbakp |
| 6 | +services: azure-communication-services |
| 7 | +ms.author: ymohanraj |
| 8 | +ms.date: 09/09/2022 |
| 9 | +ms.topic: include |
| 10 | +ms.service: azure-communication-services |
| 11 | +ms.custom: private_preview, event-tier1-build-2022 |
| 12 | +--- |
| 13 | + |
| 14 | +Get started with Azure Communication Services by using the Communication Services Java Email SDK to send Email messages. |
| 15 | + |
| 16 | +Completing this quickstart incurs a small cost of a few USD cents or less in your Azure account. |
| 17 | + |
| 18 | +## Prerequisites |
| 19 | + |
| 20 | +- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F). |
| 21 | +- [Java Development Kit (JDK)](/java/azure/jdk/) version 8 or later. |
| 22 | +- [Apache Maven](https://maven.apache.org/download.cgi). |
| 23 | +- An active Azure Communication Services resource connected to an Email Domain and its connection string. [Get started by connecting an Email Communication Resource with a Azure Communication Resource](../connect-email-communication-resource.md). |
| 24 | + |
| 25 | +### Prerequisite check |
| 26 | +- In a terminal or command window, run `mvn -v` to check that Maven is installed. |
| 27 | +- To view the domains verified with your Email Communication Services resource, sign in to the [Azure portal](https://portal.azure.com/). Locate your Email Communication Services resource and open the **Provision domains** tab from the left navigation pane. |
| 28 | + |
| 29 | +## Set up the application environment |
| 30 | + |
| 31 | +To set up an environment for sending emails, take the steps in the following sections. |
| 32 | + |
| 33 | +### Create a new Java application |
| 34 | +Open your terminal or command window and navigate to the directory where you would like to create your Java application. Run the following command to generate the Java project from the maven-archetype-quickstart template. |
| 35 | + |
| 36 | +```console |
| 37 | +mvn archetype:generate -DgroupId=com.communication.quickstart -DartifactId=communication-quickstart -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false |
| 38 | +``` |
| 39 | + |
| 40 | +The `generate` goal creates a directory with the same name as the `artifactId` value. Under this directory, the **src/main/java** directory contains the project source code, the **src/test/java directory** contains the test source, and the **pom.xml** file is the project's Project Object Model (POM). |
| 41 | + |
| 42 | +### Install the package |
| 43 | + |
| 44 | +Open the **pom.xml** file in your text editor. Add the following dependency element to the group of dependencies. |
| 45 | + |
| 46 | +```xml |
| 47 | +<dependency> |
| 48 | + <groupId>com.azure</groupId> |
| 49 | + <artifactId>azure-communication-email</artifactId> |
| 50 | + <version>1.0.0-beta.1</version> |
| 51 | +</dependency> |
| 52 | +``` |
| 53 | + |
| 54 | +### Set up the app framework |
| 55 | + |
| 56 | +Open **/src/main/java/com/communication/quickstart/App.java** in a text editor, add import directives, and remove the `System.out.println("Hello world!");` statement: |
| 57 | + |
| 58 | +```java |
| 59 | +package com.communication.quickstart; |
| 60 | + |
| 61 | +import com.azure.communication.email.models.*; |
| 62 | +import com.azure.communication.email.*; |
| 63 | +import java.util.ArrayList; |
| 64 | + |
| 65 | +public class App |
| 66 | +{ |
| 67 | + public static void main( String[] args ) |
| 68 | + { |
| 69 | + // Quickstart code goes here. |
| 70 | + } |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +## Object model |
| 75 | + |
| 76 | +The following classes and interfaces handle some of the major features of the Azure Communication Services Email SDK for Python. |
| 77 | + |
| 78 | +| Name | Description | |
| 79 | +| ---- |-------------| |
| 80 | +| EmailAddress | This interface contains an email address and an option for a display name. | |
| 81 | +| EmailAttachment | This interface creates an email attachment by accepting a unique ID, email attachment type, and a string of content bytes. | |
| 82 | +| EmailClient | This class is needed for all email functionality. You instantiate it with your connection string and use it to send email messages. | |
| 83 | +| EmailClientOptions | This interface can be added to the EmailClient instantiation to target a specific API version. | |
| 84 | +| EmailContent | This interface contains the subject, plaintext, and html of the email message. | |
| 85 | +| EmailCustomHeader | This interface allows for the addition of a name and value pair for a custom header. | |
| 86 | +| EmailMessage | This interface combines the sender, content, and recipients. Custom headers, importance, attachments, and reply-to email addresses can optionally be added as well. | |
| 87 | +| EmailRecipients | This interface holds lists of EmailAddress objects for recipients of the email message, including optional lists for CC & BCC recipients. | |
| 88 | +| SendStatusResult | This interface holds the messageId and status of the email message delivery. | |
| 89 | + |
| 90 | +## Authenticate the client |
| 91 | + |
| 92 | +To authenticate a client, you instantiate an `EmailClient` with your connection string. Learn how to [manage your resource's connection string](../../create-communication-resource.md#store-your-connection-string). You can also initialize the client with any custom HTTP client that implements the `com.azure.core.http.HttpClient` interface. |
| 93 | + |
| 94 | +To instantiate a client, add the following code to the `main` method: |
| 95 | + |
| 96 | +```java |
| 97 | +// You can get your connection string from your resource in the Azure portal. |
| 98 | +String connectionString = "endpoint=https://<resource-name>.communication.azure.com/;accesskey=<access-key>"; |
| 99 | + |
| 100 | +EmailClient emailClient = new EmailClientBuilder() |
| 101 | + .connectionString(connectionString) |
| 102 | + .buildClient(); |
| 103 | +``` |
| 104 | + |
| 105 | +For simplicity, this quickstart uses connection strings, but in production environments, we recommend using [service principals](../../../quickstarts/identity/service-principal.md). |
| 106 | + |
| 107 | +## Send an email message |
| 108 | + |
| 109 | +To send an email message, you need to |
| 110 | +- Construct the EmailContent |
| 111 | +- Create an EmailAddress for the recipient |
| 112 | +- Construct the EmailRecipients |
| 113 | +- Construct the EmailMessage with the EmailContent, EmailAddress, and the sender information from the MailFrom address of your verified domain |
| 114 | +- Call the send method |
| 115 | + |
| 116 | +```java |
| 117 | +EmailContent content = new EmailContent("Welcome to Azure Communication Services Email") |
| 118 | + .setPlainText("This email message is sent from Azure Communication Services Email using the Python SDK."); |
| 119 | + |
| 120 | +EmailAddress emailAddress = new EmailAddress( "<[email protected]>"); |
| 121 | +ArrayList<EmailAddress> addressList = new ArrayList<>(); |
| 122 | +addressList.add(emailAddress); |
| 123 | + |
| 124 | +EmailRecipients emailRecipients = new EmailRecipients(addressList); |
| 125 | + |
| 126 | +EmailMessage emailMessage = new EmailMessage( |
| 127 | + |
| 128 | + content, |
| 129 | + emailRecipients |
| 130 | +); |
| 131 | + |
| 132 | +SendEmailResult response = emailClient.send(emailMessage); |
| 133 | +``` |
| 134 | + |
| 135 | +Make these replacements in the code: |
| 136 | + |
| 137 | +- Replace `<[email protected]>` with the email address you would like to send a message to. |
| 138 | +- Replace `<[email protected]>` with the MailFrom address of your verified domain. |
| 139 | + |
| 140 | +## Retrieve the Message ID of the email delivery |
| 141 | + |
| 142 | +To track the status of the email delivery, you will need the `messageId` from the response. |
| 143 | + |
| 144 | +```java |
| 145 | +String message_id = response.getMessageId(); |
| 146 | +``` |
| 147 | + |
| 148 | +## Get the status of the email delivery |
| 149 | + |
| 150 | +We can keep checking the email delivery status until the status is `OutForDelivery`. |
| 151 | + |
| 152 | +```java |
| 153 | +long waitTime = 120*1000; |
| 154 | +boolean timeout = true; |
| 155 | +while (waitTime > 0) |
| 156 | +{ |
| 157 | + SendStatusResult sendStatus = emailClient.getSendStatus(messageId); |
| 158 | + System.out.printf("Send mail status for MessageId : <{%s}>, Status: [{%s}]", messageId, sendStatus.getStatus()); |
| 159 | + |
| 160 | + if (!sendStatus.getStatus().toString().toLowerCase().equals(SendStatus.QUEUED.toString())) |
| 161 | + { |
| 162 | + timeout = false; |
| 163 | + break; |
| 164 | + } |
| 165 | + Thread.sleep(10000); |
| 166 | + waitTime = waitTime-10000; |
| 167 | +} |
| 168 | + |
| 169 | +if(timeout) |
| 170 | +{ |
| 171 | + System.out.println("Looks like we timed out for email"); |
| 172 | +} |
| 173 | +``` |
| 174 | + |
| 175 | +| Status Name | Description | |
| 176 | +| ----------- | ------------| |
| 177 | +| Queued | The email has been placed in the queue for delivery. | |
| 178 | +| OutForDelivery | The email is currently en route to its recipient(s). | |
| 179 | +| Dropped | The email message was dropped before the delivery could be successfully completed. | |
| 180 | + |
| 181 | + |
| 182 | + |
| 183 | +## Run the code |
| 184 | + |
| 185 | +1. Navigate to the directory that contains the **pom.xml** file and compile the project by using the `mvn` command. |
| 186 | + |
| 187 | + ```console |
| 188 | + mvn compile |
| 189 | + ``` |
| 190 | + |
| 191 | +1. Build the package. |
| 192 | + |
| 193 | + ```console |
| 194 | + mvn package |
| 195 | + ``` |
| 196 | + |
| 197 | +1. Run the following `mvn` command to execute the app. |
| 198 | + |
| 199 | + ```console |
| 200 | + mvn exec:java -Dexec.mainClass="com.communication.quickstart.App" -Dexec.cleanupDaemonThreads=false |
| 201 | + ``` |
0 commit comments