### Create, List, and Get Segments in Java Source: https://context7.com/resend/resend-java-example/llms.txt Shows how to create new segments for organizing contacts, list all existing segments, and retrieve specific segment details using the Resend Java SDK. Requires a Resend API key. ```java import com.resend.Resend; import com.resend.core.exception.ResendException; import com.resend.services.segments.model.CreateSegmentOptions; import com.resend.services.segments.model.CreateSegmentResponseSuccess; import com.resend.services.segments.model.GetSegmentResponseSuccess; import com.resend.services.segments.model.ListSegmentsResponseSuccess; public class SegmentManagementExample { public static void main(String[] args) { try { Resend resend = new Resend("re_your_api_key_here"); // Create segment CreateSegmentOptions options = CreateSegmentOptions.builder() .name("Registered Users") .build(); CreateSegmentResponseSuccess response = resend.segments().create(options); System.out.println("Segment created:"); System.out.println(" ID: " + response.getId()); System.out.println(" Name: " + response.getName()); // List all segments ListSegmentsResponseSuccess listResponse = resend.segments().list(); System.out.println("\nSegments:"); listResponse.getData().forEach(segment -> System.out.println(" - " + segment.getName() + " (ID: " + segment.getId() + ")") ); // Get segment details GetSegmentResponseSuccess getResponse = resend.segments().get(response.getId()); System.out.println("\nSegment details: " + getResponse.getName()); } catch (ResendException e) { System.err.println("Failed to manage segment: " + e.getMessage()); } } } ``` -------------------------------- ### Create, Get, and Update Contacts in Java Source: https://context7.com/resend/resend-java-example/llms.txt Demonstrates how to add new contacts with detailed information, retrieve existing contact details, and modify contact information using the Resend Java SDK. Requires a Resend API key. ```java import com.resend.Resend; import com.resend.core.exception.ResendException; import com.resend.services.contacts.model.Contact; import com.resend.services.contacts.model.CreateContactOptions; import com.resend.services.contacts.model.CreateContactResponseSuccess; import com.resend.services.contacts.model.UpdateContactOptions; import com.resend.services.contacts.model.UpdateContactResponseSuccess; public class ContactManagementExample { public static void main(String[] args) { try { Resend resend = new Resend("re_your_api_key_here"); // Create contact CreateContactOptions options = CreateContactOptions.builder() .email("steve.wozniak@gmail.com") .firstName("Steve") .lastName("Wozniak") .unsubscribed(false) .build(); CreateContactResponseSuccess createResponse = resend.contacts().create(options); System.out.println("Contact created: " + createResponse.getId()); // Get contact details Contact contact = resend.contacts().get(createResponse.getId()); System.out.println("Contact: " + contact.getFirstName() + " " + contact.getLastName()); // Update contact UpdateContactOptions updateOptions = UpdateContactOptions.builder() .id(createResponse.getId()) .firstName("Stephen") .unsubscribed(true) .build(); UpdateContactResponseSuccess updateResponse = resend.contacts().update(updateOptions); System.out.println("Contact updated: " + updateResponse.getId()); } catch (ResendException e) { System.err.println("Failed to manage contact: " + e.getMessage()); } } } ``` -------------------------------- ### Create and Manage Webhooks with Resend Java SDK Source: https://context7.com/resend/resend-java-example/llms.txt Shows how to create webhooks to receive real-time notifications for various email events like sends, deliveries, bounces, and opens. It includes examples for creating webhooks with single or multiple events and listing all configured webhooks. Requires the Resend Java SDK. ```java import com.resend.Resend; import com.resend.core.exception.ResendException; import com.resend.services.webhooks.model.CreateWebhookOptions; import com.resend.services.webhooks.model.CreateWebhookResponseSuccess; import com.resend.services.webhooks.model.ListWebhooksResponseSuccess; import static com.resend.services.webhooks.model.WebhookEvent.*; public class WebhookManagementExample { public static void main(String[] args) { try { Resend resend = new Resend("re_your_api_key_here"); // Create webhook with single event CreateWebhookOptions options = CreateWebhookOptions.builder() .endpoint("https://example.com/handler") .events(EMAIL_SENT) .build(); CreateWebhookResponseSuccess response = resend.webhooks().create(options); System.out.println("Webhook created:"); System.out.println(" ID: " + response.getId()); System.out.println(" Signing Secret: " + response.getSigningSecret()); // Create webhook with multiple events CreateWebhookOptions multiOptions = CreateWebhookOptions.builder() .endpoint("https://example.com/handler") .events(EMAIL_SENT, EMAIL_DELIVERED, EMAIL_BOUNCED, EMAIL_OPENED, EMAIL_CLICKED) .build(); CreateWebhookResponseSuccess multiResponse = resend.webhooks().create(multiOptions); System.out.println("Multi-event webhook created: " + multiResponse.getId()); // List all webhooks ListWebhooksResponseSuccess listResponse = resend.webhooks().list(); System.out.println("\nWebhooks:"); listResponse.getData().forEach(webhook -> System.out.println(" - " + webhook.getEndpoint() + " (ID: " + webhook.getId() + ")") ); } catch (ResendException e) { System.err.println("Failed to manage webhook: " + e.getMessage()); } } } ``` -------------------------------- ### List and Get Domain Details with Resend Java SDK Source: https://context7.com/resend/resend-java-example/llms.txt This snippet illustrates how to retrieve a list of all domains associated with your Resend account and fetch detailed information for a specific domain using its ID. It requires your Resend API key. The output includes a list of domain names and statuses, followed by the detailed attributes of a selected domain. ```java import com.resend.Resend; import com.resend.core.exception.ResendException; import com.resend.services.domains.model.Domain; import com.resend.services.domains.model.ListDomainsResponse; public class ListDomainsExample { public static void main(String[] args) { try { Resend resend = new Resend("re_your_api_key_here"); // List all domains ListDomainsResponse response = resend.domains().list(); System.out.println("Domains:"); response.getData().forEach(domain -> System.out.println(" - " + domain.getName() + " (Status: " + domain.getStatus() + ")") ); // Get specific domain details String domainId = "4dd369bc-aa82-4ff3-97de-514ae3000ee0"; Domain domain = resend.domains().get(domainId); System.out.println("\nDomain details:"); System.out.println(" ID: " + domain.getId()); System.out.println(" Name: " + domain.getName()); System.out.println(" Status: " + domain.getStatus()); System.out.println(" Created at: " + domain.getCreatedAt()); } catch (ResendException e) { System.err.println("Failed to retrieve domains: " + e.getMessage()); } } } ``` -------------------------------- ### Create and Manage Email Templates with Resend Java Source: https://context7.com/resend/resend-java-example/llms.txt This Java snippet illustrates how to create, list, and retrieve email templates using the Resend API. It requires the Resend API key. The example outputs the ID of the created template and lists all existing templates, followed by details of a specific template. ```java import com.resend.Resend; import com.resend.core.exception.ResendException; import com.resend.services.templates.model.CreateTemplateOptions; import com.resend.services.templates.model.CreateTemplateResponseSuccess; import com.resend.services.templates.model.GetTemplateResponseSuccess; import com.resend.services.templates.model.ListTemplatesResponseSuccess; public class TemplateManagementExample { public static void main(String[] args) { try { Resend resend = new Resend("re_your_api_key_here"); // Create template CreateTemplateOptions options = CreateTemplateOptions.builder() .name("order-confirmation") .html("
Hello {{{FIRST_NAME}}},
Your order {{{ORDER_ID}}} has been confirmed!
") .subject("Order Confirmation") .build(); CreateTemplateResponseSuccess response = resend.templates().create(options); System.out.println("Template created: " + response.getId()); // List all templates ListTemplatesResponseSuccess listResponse = resend.templates().list(); System.out.println("Templates:"); listResponse.getData().forEach(template -> System.out.println(" - " + template.getName() + " (ID: " + template.getId() + ")") ); // Get template details GetTemplateResponseSuccess getResponse = resend.templates().get(response.getId()); System.out.println("Template: " + getResponse.getName()); } catch (ResendException e) { System.err.println("Failed to manage template: " + e.getMessage()); } } } ``` -------------------------------- ### Create Domain with Regional Configuration using Resend Java SDK Source: https://context7.com/resend/resend-java-example/llms.txt This example demonstrates how to create a Resend domain with a specific regional configuration, which is useful for data residency requirements. It requires your Resend API key, the domain name, and the desired region code. The output confirms the domain's creation and its assigned region. ```java import com.resend.Resend; import com.resend.core.exception.ResendException; import com.resend.services.domains.model.CreateDomainOptions; import com.resend.services.domains.model.CreateDomainResponse; public class RegionalDomainExample { public static void main(String[] args) { try { Resend resend = new Resend("re_your_api_key_here"); CreateDomainOptions options = CreateDomainOptions.builder() .name("example.com") .region("eu-west-1") .build(); CreateDomainResponse response = resend.domains().create(options); System.out.println("Domain created in EU region:"); System.out.println(" ID: " + response.getId()); System.out.println(" Region: " + response.getRegion()); } catch (ResendException e) { System.err.println("Failed to create domain: " + e.getMessage()); } } } ``` -------------------------------- ### Schedule Broadcast with Resend Java Source: https://context7.com/resend/resend-java-example/llms.txt This Java example demonstrates how to schedule a broadcast campaign for delivery at a future time. It requires the Resend API key and the broadcast ID. The output confirms the broadcast has been scheduled with its ID. ```java import com.resend.Resend; import com.resend.core.exception.ResendException; import com.resend.services.broadcasts.model.SendBroadcastOptions; import com.resend.services.broadcasts.model.SendBroadcastResponseSuccess; public class ScheduledBroadcastExample { public static void main(String[] args) { try { Resend resend = new Resend("re_your_api_key_here"); String broadcastId = "49a3999c-0ce1-4ea6-ab68-afcd6dc2e794"; SendBroadcastOptions options = SendBroadcastOptions.builder() .scheduledAt("in 1 hour") .build(); SendBroadcastResponseSuccess response = resend.broadcasts().send(options, broadcastId); System.out.println("Broadcast scheduled: " + response.getId()); } catch (ResendException e) { System.err.println("Failed to schedule broadcast: " + e.getMessage()); } } } ``` -------------------------------- ### Create and Manage Topics with Resend Java SDK Source: https://context7.com/resend/resend-java-example/llms.txt Demonstrates creating subscription topics with different default subscription settings (opt-in/opt-out) and adding descriptions. It also shows how to retrieve topic details. This functionality is crucial for managing user preferences and opt-in/opt-out handling. ```java import com.resend.Resend; import com.resend.core.exception.ResendException; import com.resend.services.topics.model.CreateTopicOptions; import com.resend.services.topics.model.CreateTopicResponseSuccess; import com.resend.services.topics.model.GetTopicResponseSuccess; public class TopicManagementExample { public static void main(String[] args) { try { Resend resend = new Resend("re_your_api_key_here"); // Create topic with opt-in CreateTopicOptions options = CreateTopicOptions.builder() .name("Weekly Newsletter") .defaultSubscription("opt_in") .build(); CreateTopicResponseSuccess response = resend.topics().create(options); System.out.println("Topic created: " + response.getId()); // Create topic with opt-out and description CreateTopicOptions optOutOptions = CreateTopicOptions.builder() .name("Product Updates") .defaultSubscription("opt_out") .description("Get notified about new product features and updates") .build(); CreateTopicResponseSuccess optOutResponse = resend.topics().create(optOutOptions); System.out.println("Topic with opt-out created: " + optOutResponse.getId()); // Get topic details GetTopicResponseSuccess getResponse = resend.topics().get(response.getId()); System.out.println("Topic name: " + getResponse.getName()); } catch (ResendException e) { System.err.println("Failed to manage topic: " + e.getMessage()); } } } ``` -------------------------------- ### Add Resend Java Dependency (Gradle) Source: https://github.com/resend/resend-java-example/blob/main/README.md This code snippet shows how to add the Resend Java SDK as a dependency to your project using Gradle. Ensure you have a Java project set up with Gradle for build automation. ```gradle implementation 'com.resend:resend-java:2.0.0' ``` -------------------------------- ### Create and Send Broadcasts API Source: https://context7.com/resend/resend-java-example/llms.txt This endpoint allows you to create broadcast campaigns for sending emails to segments of contacts using templates. It also demonstrates how to send the broadcast immediately after creation. ```APIDOC ## POST /broadcasts ### Description Creates broadcast campaigns for sending emails to entire segments of contacts with template support. The broadcast can be sent immediately after creation. ### Method POST ### Endpoint /broadcasts ### Parameters #### Query Parameters None #### Request Body - **segmentId** (string) - Required - The ID of the segment to send the broadcast to. - **from** (string) - Required - The sender's email address and name (e.g., "AcmeThanks for your payment! Please find your invoice attached.
") .attachments(attachment) .build(); CreateEmailResponse response = resend.emails().send(params); System.out.println("Email with remote attachment sent with ID: " + response.getId()); } catch (ResendException e) { System.err.println("Failed to send email: " + e.getMessage()); } } } ``` -------------------------------- ### Add Resend Java Dependency (Maven) Source: https://github.com/resend/resend-java-example/blob/main/README.md This code snippet demonstrates how to include the Resend Java SDK as a dependency in your Maven project. This is essential for using Resend's Java client library in your application. ```xmlHello {{{FIRST_NAME}}},
Your order {{{ORDER_ID}}} has been confirmed!
", "subject": "Order Confirmation" } ``` ### Response #### Success Response (200) - **id** (string) - The unique identifier for the created template. #### Response Example ```json { "id": "655b32354097f978a1b05b3a" } ``` ## GET /templates ### Description Retrieves a list of all created email templates. ### Method GET ### Endpoint /templates ### Parameters #### Query Parameters None ### Request Example (No request body for GET requests) ### Response #### Success Response (200) - **data** (array) - An array of template objects. - **id** (string) - The template ID. - **name** (string) - The template name. #### Response Example ```json { "data": [ { "id": "655b32354097f978a1b05b3a", "name": "order-confirmation" } ] } ``` ## GET /templates/{templateId} ### Description Retrieves the details of a specific email template by its ID. ### Method GET ### Endpoint /templates/{templateId} ### Parameters #### Path Parameters - **templateId** (string) - Required - The ID of the template to retrieve. #### Query Parameters None ### Request Example (No request body for GET requests) ### Response #### Success Response (200) - **id** (string) - The template ID. - **name** (string) - The template name. - **html** (string) - The HTML content of the template. - **subject** (string) - The subject line of the email template. #### Response Example ```json { "id": "655b32354097f978a1b05b3a", "name": "order-confirmation", "html": "Hello {{{FIRST_NAME}}},
Your order {{{ORDER_ID}}} has been confirmed!
", "subject": "Order Confirmation" } ``` ``` -------------------------------- ### Create and Verify Domain with Resend Java SDK Source: https://context7.com/resend/resend-java-example/llms.txt This snippet shows how to add a custom domain to your Resend account and initiate the verification process for its DNS configuration. It requires your Resend API key and the domain name. The output includes the created domain's ID, name, status, and region, as well as confirmation of the verification initiation. ```java import com.resend.Resend; import com.resend.core.exception.ResendException; import com.resend.services.domains.model.CreateDomainOptions; import com.resend.services.domains.model.CreateDomainResponse; import com.resend.services.domains.model.VerifyDomainResponse; public class DomainManagementExample { public static void main(String[] args) { try { Resend resend = new Resend("re_your_api_key_here"); // Create domain CreateDomainOptions options = CreateDomainOptions.builder() .name("example.com") .build(); CreateDomainResponse response = resend.domains().create(options); System.out.println("Domain created:"); System.out.println(" ID: " + response.getId()); System.out.println(" Name: " + response.getName()); System.out.println(" Status: " + response.getStatus()); System.out.println(" Region: " + response.getRegion()); // Verify domain VerifyDomainResponse verifyResponse = resend.domains().verify(response.getId()); System.out.println("Domain verification initiated: " + verifyResponse.getObject()); } catch (ResendException e) { System.err.println("Failed to manage domain: " + e.getMessage()); } } } ``` -------------------------------- ### Send Batch Emails in Java Source: https://context7.com/resend/resend-java-example/llms.txt Sends multiple emails efficiently using a single API call. Requires the Resend Java SDK. Inputs are a list of email options, and outputs are the status of sent emails. ```java import com.resend.Resend; import com.resend.core.exception.ResendException; import com.resend.services.batch.model.CreateBatchEmailsResponse; import com.resend.services.emails.model.CreateEmailOptions; import java.util.Arrays; import java.util.List; public class BatchEmailsExample { public static void main(String[] args) { try { Resend resend = new Resend("re_your_api_key_here"); CreateEmailOptions firstEmail = CreateEmailOptions.builder() .from("AcmeIt works!
") .build(); ListIt works!
") .build(); CreateEmailResponse response = resend.emails().send(params); System.out.println("Email sent with ID: " + response.getId()); } catch (ResendException e) { System.err.println("Failed to send email: " + e.getMessage()); } } } ``` -------------------------------- ### List and Remove API Keys in Java Source: https://context7.com/resend/resend-java-example/llms.txt Manages API keys by listing all existing keys and removing a specific key by its ID. This function uses the Resend Java SDK. Inputs are implicitly the current API keys, and the output includes a list of keys and confirmation of removal. ```java import com.resend.Resend; import com.resend.core.exception.ResendException; import com.resend.services.apikeys.model.ListApiKeysResponse; public class ManageApiKeysExample { public static void main(String[] args) { try { Resend resend = new Resend("re_your_api_key_here"); // List all API keys ListApiKeysResponse response = resend.apiKeys().list(); System.out.println("API Keys:"); response.getData().forEach(apiKey -> System.out.println(" - " + apiKey.getName() + " (ID: " + apiKey.getId() + ")") ); // Remove a specific API key String apiKeyId = "b6d24b8e-af0b-4c3c-be0c-359bbd97381e"; resend.apiKeys().remove(apiKeyId); System.out.println("API Key removed: " + apiKeyId); } catch (ResendException e) { System.err.println("Failed to manage API keys: " + e.getMessage()); } } } ``` -------------------------------- ### Send Email to Multiple Recipients with Resend Java SDK Source: https://context7.com/resend/resend-java-example/llms.txt Illustrates sending an email to a list of recipients using the Resend Java SDK. It takes sender, a list of 'to' addresses, subject, and HTML content as parameters. ```java import com.resend.Resend; import com.resend.core.exception.ResendException; import com.resend.services.emails.model.CreateEmailOptions; import com.resend.services.emails.model.CreateEmailResponse; import java.util.List; public class MultipleRecipientsExample { public static void main(String[] args) { try { Resend resend = new Resend("re_your_api_key_here"); CreateEmailOptions params = CreateEmailOptions.builder() .from("AcmeThis email was sent to multiple recipients!
") .build(); CreateEmailResponse response = resend.emails().send(params); System.out.println("Email sent with ID: " + response.getId()); } catch (ResendException e) { System.err.println("Failed to send email: " + e.getMessage()); } } } ``` -------------------------------- ### Create and Send Broadcasts with Resend Java Source: https://context7.com/resend/resend-java-example/llms.txt This snippet shows how to create a broadcast campaign for sending emails to a segment of contacts using a template. It requires the Resend API key and segment ID. The output includes the ID of the created and sent broadcast. ```java import com.resend.Resend; import com.resend.core.exception.ResendException; import com.resend.services.broadcasts.model.CreateBroadcastOptions; import com.resend.services.broadcasts.model.CreateBroadcastResponseSuccess; import com.resend.services.broadcasts.model.SendBroadcastOptions; import com.resend.services.broadcasts.model.SendBroadcastResponseSuccess; public class BroadcastExample { public static void main(String[] args) { try { Resend resend = new Resend("re_your_api_key_here"); // Create broadcast CreateBroadcastOptions options = CreateBroadcastOptions.builder() .segmentId("78261eea-8f8b-4381-83c6-79fa7120f1cf") .from("AcmeThanks for your payment! Please find your invoice attached.
") .attachments(attachment) .build(); CreateEmailResponse response = resend.emails().send(params); System.out.println("Email with attachment sent with ID: " + response.getId()); } catch (ResendException | IOException e) { System.err.println("Failed to send email: " + e.getMessage()); } } } ``` -------------------------------- ### Send Email with CC and BCC using Resend Java SDK Source: https://context7.com/resend/resend-java-example/llms.txt Shows how to send an email that includes recipients in both CC (Carbon Copy) and BCC (Blind Carbon Copy) fields using the Resend Java SDK. Requires API key, sender, 'to' address, and CC/BCC addresses. ```java import com.resend.Resend; import com.resend.core.exception.ResendException; import com.resend.services.emails.model.CreateEmailOptions; import com.resend.services.emails.model.CreateEmailResponse; public class CCBCCExample { public static void main(String[] args) { try { Resend resend = new Resend("re_your_api_key_here"); CreateEmailOptions params = CreateEmailOptions.builder() .from("AcmeThis email includes CC and BCC recipients!
") .build(); CreateEmailResponse response = resend.emails().send(params); System.out.println("Email sent with ID: " + response.getId()); } catch (ResendException e) { System.err.println("Failed to send email: " + e.getMessage()); } } } ``` -------------------------------- ### Send Email with Inline Image using Java Source: https://context7.com/resend/resend-java-example/llms.txt This snippet shows how to embed an image directly within the email's HTML content using a content ID. The `path` and `contentId` fields in the `Attachment` object are used for this purpose. This requires the `resend-java-sdk`. The output is the ID of the sent email. ```java import com.resend.Resend; import com.resend.core.exception.ResendException; import com.resend.services.emails.model.Attachment; import com.resend.services.emails.model.CreateEmailOptions; import com.resend.services.emails.model.CreateEmailResponse; public class InlineImageExample { public static void main(String[] args) { try { Resend resend = new Resend("re_your_api_key_here"); Attachment inlineImage = Attachment.builder() .path("https://resend.com/static/sample/logo.png") .fileName("logo.png") .contentId("logo-image") .build(); CreateEmailOptions params = CreateEmailOptions.builder() .from("AcmeHere is our inline logo