### 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., "Acme "). - **subject** (string) - Required - The subject line of the email. - **html** (string) - Required - The HTML content of the email. Supports Resend template variables like `{{{FIRST_NAME}}}` and `{{{RESEND_UNSUBSCRIBE_URL}}}`. - **name** (string) - Optional - A name for the broadcast campaign. ### Request Example ```json { "segmentId": "78261eea-8f8b-4381-83c6-79fa7120f1cf", "from": "Acme ", "subject": "Hello World", "html": "Hi {{{FIRST_NAME|there}}}, you can unsubscribe here: {{{RESEND_UNSUBSCRIBE_URL}}}", "name": "December Newsletter" } ``` ### Response #### Success Response (200) - **id** (string) - The unique identifier for the created broadcast. #### Response Example ```json { "id": "655b32354097f978a1b05b3a" } ``` ``` -------------------------------- ### Create API Key in Java Source: https://context7.com/resend/resend-java-example/llms.txt Generates a new API key with a specified name. This operation requires the Resend Java SDK. The input is an API key name, and the output is the created API key's ID and token. ```java import com.resend.Resend; import com.resend.core.exception.ResendException; import com.resend.services.apikeys.model.CreateApiKeyOptions; import com.resend.services.apikeys.model.CreateApiKeyResponse; public class CreateApiKeyExample { public static void main(String[] args) { try { Resend resend = new Resend("re_your_api_key_here"); CreateApiKeyOptions options = CreateApiKeyOptions.builder() .name("Production") .build(); CreateApiKeyResponse response = resend.apiKeys().create(options); System.out.println("API Key created:"); System.out.println(" ID: " + response.getId()); System.out.println(" Token: " + response.getToken()); } catch (ResendException e) { System.err.println("Failed to create API key: " + e.getMessage()); } } } ``` -------------------------------- ### Send Broadcast API Source: https://context7.com/resend/resend-java-example/llms.txt This endpoint allows you to send a previously created broadcast campaign. You can choose to send it immediately or schedule it for a specific time. ```APIDOC ## POST /broadcasts/{broadcastId}/send ### Description Sends a broadcast campaign immediately or schedules it to be sent at a specific time. ### Method POST ### Endpoint /broadcasts/{broadcastId}/send ### Parameters #### Path Parameters - **broadcastId** (string) - Required - The ID of the broadcast to send. #### Query Parameters None #### Request Body - **scheduledAt** (string) - Optional - The time to schedule the broadcast for. Can be a specific date-time string or a relative time like "in 1 hour". ### Request Example ```json { "scheduledAt": "in 1 hour" } ``` ### Response #### Success Response (200) - **id** (string) - The unique identifier for the broadcast send operation. #### Response Example ```json { "id": "655b32354097f978a1b05b3a" } ``` ``` -------------------------------- ### Send Email with Remote Attachment using Java Source: https://context7.com/resend/resend-java-example/llms.txt This example demonstrates how to attach a file from a remote URL to an email without downloading it locally. The `path` field in the `Attachment` object is used to specify the URL. 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 RemoteAttachmentExample { public static void main(String[] args) { try { Resend resend = new Resend("re_your_api_key_here"); Attachment attachment = Attachment.builder() .path("https://resend.com/static/sample/invoice.pdf") .fileName("invoice.pdf") .build(); CreateEmailOptions params = CreateEmailOptions.builder() .from("Acme ") .to("delivered@resend.dev") .subject("Receipt for your payment") .html("

Thanks 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. ```xml com.resend resend-java 2.0.0 ``` -------------------------------- ### Email Templates API Source: https://context7.com/resend/resend-java-example/llms.txt This section covers the API endpoints for managing email templates. You can create new templates, list existing ones, and retrieve details of a specific template. ```APIDOC ## POST /templates ### Description Creates a new email template that can be reused across campaigns. Templates support variable placeholders. ### Method POST ### Endpoint /templates ### Parameters #### Query Parameters None #### Request Body - **name** (string) - Required - The name of the template. - **html** (string) - Required - The HTML content of the template. Supports variable placeholders like `{{{FIRST_NAME}}}`. - **subject** (string) - Required - The subject line of the email template. ### Request Example ```json { "name": "order-confirmation", "html": "

Hello {{{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("Acme ") .to("foo@gmail.com") .subject("Hello World") .html("

It works!

") .build(); CreateEmailOptions secondEmail = CreateEmailOptions.builder() .from("Acme ") .to("bar@outlook.com") .subject("World Hello") .html("

It works!

") .build(); List emails = Arrays.asList(firstEmail, secondEmail); CreateBatchEmailsResponse response = resend.batch().send(emails); System.out.println("Batch emails sent:"); response.getData().forEach(email -> System.out.println(" - Email ID: " + email.getId()) ); } catch (ResendException e) { System.err.println("Failed to send batch emails: " + e.getMessage()); } } } ``` -------------------------------- ### Send Simple Email with Resend Java SDK Source: https://context7.com/resend/resend-java-example/llms.txt Demonstrates how to send a basic HTML email to a single recipient using the Resend Java SDK. Requires an API key and specifies sender, recipient, subject, and HTML content. ```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 EmailExample { public static void main(String[] args) { try { Resend resend = new Resend("re_your_api_key_here"); CreateEmailOptions params = CreateEmailOptions.builder() .from("Acme ") .to("delivered@resend.dev") .subject("Hello World") .html("

It 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("Acme ") .to(List.of("user1@example.com", "user2@example.com", "user3@example.com")) .subject("Hello to multiple recipients") .html("

This 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("Acme ") .subject("Hello World") .html("Hi {{{FIRST_NAME|there}}}, you can unsubscribe here: {{{RESEND_UNSUBSCRIBE_URL}}}") .name("December Newsletter") .build(); CreateBroadcastResponseSuccess createResponse = resend.broadcasts().create(options); System.out.println("Broadcast created: " + createResponse.getId()); // Send broadcast immediately SendBroadcastOptions sendOptions = SendBroadcastOptions.builder().build(); SendBroadcastResponseSuccess sendResponse = resend.broadcasts().send(sendOptions, createResponse.getId()); System.out.println("Broadcast sent: " + sendResponse.getId()); } catch (ResendException e) { System.err.println("Failed to manage broadcast: " + e.getMessage()); } } } ``` -------------------------------- ### Create Sending-Only API Key in Java Source: https://context7.com/resend/resend-java-example/llms.txt Creates a restricted API key with 'sending_access' permission for a specific domain. Utilizes the Resend Java SDK. Requires the API key name, permission type, and domain ID as input, outputting the new key's ID and token. ```java import com.resend.Resend; import com.resend.core.exception.ResendException; import com.resend.services.apikeys.model.CreateApiKeyOptions; import com.resend.services.apikeys.model.CreateApiKeyResponse; public class RestrictedApiKeyExample { public static void main(String[] args) { try { Resend resend = new Resend("re_your_api_key_here"); CreateApiKeyOptions options = CreateApiKeyOptions.builder() .name("Sending Only") .permission("sending_access") .domainId("your-domain-id") .build(); CreateApiKeyResponse response = resend.apiKeys().create(options); System.out.println("API Key with sending access created:"); System.out.println(" ID: " + response.getId()); System.out.println(" Token: " + response.getToken()); } catch (ResendException e) { System.err.println("Failed to create API key: " + e.getMessage()); } } } ``` -------------------------------- ### Send Email with Local Attachment using Java Source: https://context7.com/resend/resend-java-example/llms.txt This snippet shows how to attach a local file to an email. The file is first encoded in Base64 format and then included in the email's attachments. This requires the `resend-java-sdk` and the `FileUtils` utility for encoding. 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; import com.resend.util.FileUtils; import java.io.IOException; public class AttachmentExample { public static void main(String[] args) { try { Resend resend = new Resend("re_your_api_key_here"); // Attach local file String fileContent = FileUtils.encodeFileToBase64("invoice.pdf"); Attachment attachment = Attachment.builder() .fileName("invoice.pdf") .content(fileContent) .build(); CreateEmailOptions params = CreateEmailOptions.builder() .from("Acme ") .to("delivered@resend.dev") .subject("Receipt for your payment") .html("

Thanks 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("Acme ") .to("delivered@resend.dev") .cc("cc@resend.dev") .bcc("bcc@resend.dev") .subject("Hello with CC and BCC") .html("

This 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("Acme ") .to("delivered@resend.dev") .subject("Thank you for contacting us") .html("

Here is our inline logo

") .attachments(inlineImage) .build(); CreateEmailResponse response = resend.emails().send(params); System.out.println("Email with inline image sent with ID: " + response.getId()); } catch (ResendException e) { System.err.println("Failed to send email: " + e.getMessage()); } } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.