Giter VIP home page Giter VIP logo

mailersend-java's Introduction

MailerSend Java SDK

MIT licensed

Table of Contents

Installation

Using Maven:

<dependency>
  <groupId>com.mailersend</groupId>
  <artifactId>java-sdk</artifactId>
  <version>1.0.1</version>
</dependency>

Using Gradle:

implementation 'com.mailersend:java-sdk:1.0.0'

Usage

Email

The SDK provides a simple interface to send an email through MailerSend. Check the examples below for various use cases.

The SDK returns a MailerSendResponse object on successful send or throws a MailerSendException on a failed one.

Through the MailerSendResponse object you can get the ID of the sent message, while the MailerSendException contains the response code and all errors that occured. Check the respective code and errors properties.

Send an email

import com.mailersend.sdk.Email;
import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.MailerSendResponse;
import com.mailersend.sdk.exceptions.MailerSendException;

public void sendEmail() {

    Email email = new Email();

    email.setFrom("name", "your email");
    email.addRecipient("name", "[email protected]");

    // you can also add multiple recipients by calling addRecipient again
    email.addRecipient("name 2", "[email protected]");

    // there's also a recipient object you can use
    Recipient recipient = new Recipient("name", "[email protected]");
    email.addRecipient(recipient);
    
    email.setSubject("Email subject");

    email.setPlain("This is the text content");
    email.setHtml("<p>This is the HTML content</p>");

    MailerSend ms = new MailerSend();

    ms.setToken("Your API token");

    try {
    
        MailerSendResponse response = ms.send(email);
        System.out.println(response.messageId);
    } catch (MailerSendException e) {

        e.printStackTrace();
    }
}

Add CC, BCC recipients

import com.mailersend.sdk.emails.Email;
import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.MailerSendResponse;
import com.mailersend.sdk.exceptions.MailerSendException;

public void sendEmail() {

    Email email = new Email();

    email.setFrom("name", "your email");
    email.addRecipient("name", "[email protected]");

    email.addCc("name", "[email protected]");

    // add a second cc recipient
    email.addCc("name 2", "[email protected]");

    // same for a bcc recipient
    email.addBcc("bcc name", "[email protected]");
    
    email.setSubject("Email subject");

    email.setPlain("This is the text content");
    email.setHtml("<p>This is the HTML content</p>");

    MailerSend ms = new MailerSend();

    ms.setToken("Your API token");

    try {
    
        MailerSendResponse response = ms.emails().send(email);
        System.out.println(response.messageId);
    } catch (MailerSendException e) {

        e.printStackTrace();
    }
}

Send a template-based email

import com.mailersend.sdk.emails.Email;
import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.MailerSendResponse;
import com.mailersend.sdk.exceptions.MailerSendException;

public void sendEmail() {

    Email email = new Email();

    email.setFrom("name", "your email");

    Recipient recipient = new Recipient("name", "[email protected]");
    
    email.addRecipient(recipient);

    email.setTemplateId("Your MailerSend template ID");

    MailerSend ms = new MailerSend();

    ms.setToken("Your API token");

    try {
    
        MailerSendResponse response = ms.emails().send(email);
        System.out.println(response.messageId);
    } catch (MailerSendException e) {

        e.printStackTrace();
    }
}

Personalization

import com.mailersend.sdk.emails.Email;
import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.MailerSendResponse;
import com.mailersend.sdk.exceptions.MailerSendException;

public void sendEmail() {

    Email email = new Email();

    email.setFrom("name", "your email");

    Recipient recipient = new Recipient("name", "[email protected]");
    
    email.addRecipient(recipient);

    email.setSubject("Subject {{ var }}");
    email.setPlain("This is the text version with a {{ var }}.");
    email.setHtml("<p>This is the HTML version with a {{ var }}.</p>");

    // you can add personalization for all recipients
    email.addPersonalization("var name", "personalization value");

    // you can add personalization for each recipient separately
    email.addPersonalization(recipient, "var2 name", "personalization value");

    // you can also add POJOs as personalization provided they can be serialized to JSON via Gson and do not have any object properties
    MyPojo obj = new MyPojo();
    obj.property1 = "property 1 value";
    obj.array1 = {1, 2, 3, 4};

    email.addPersonalization("pojo", obj);

    MailerSend ms = new MailerSend();

    ms.setToken("Your API token");

    try {
    
        MailerSendResponse response = ms.emails().send(email);
        System.out.println(response.messageId);
    } catch (MailerSendException e) {

        e.printStackTrace();
    }
}

Send email with attachment

import com.mailersend.sdk.emails.Email;
import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.MailerSendResponse;
import com.mailersend.sdk.exceptions.MailerSendException;

public void sendEmail() {

    Email email = new Email();

    email.setFrom("name", "your email");
    email.addRecipient("name", "[email protected]");
   
    email.setSubject("Email subject");

    email.setPlain("This is the text content");
    email.setHtml("<p>This is the HTML content</p>");

    // attach a file to the email
    email.attachFile("LICENSE");

    // if you already have a file object, you can attach that to the email
    File file = new File("LICENSE");
    email.attachFile(file);

    MailerSend ms = new MailerSend();

    ms.setToken("Your API token");

    try {
    
        MailerSendResponse response = ms.emails().send(email);
        System.out.println(response.messageId);
    } catch (MailerSendException e) {

        e.printStackTrace();
    }
}

Schedule an email

import com.mailersend.sdk.emails.Email;
import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.MailerSendResponse;
import com.mailersend.sdk.exceptions.MailerSendException;
import java.util.Calendar;
import java.util.Date;

public void scheduleEmail() {

    Email email = new Email();

    email.setFrom("name", "your email");
    email.addRecipient("name", "[email protected]");
   
    email.setSubject("Email subject");

    email.setPlain("This is the text content");
    email.setHtml("<p>This is the HTML content</p>");

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    calendar.add(Calendar.DATE, 1);

    email.setSendAt(calendar.getTime());

    MailerSend ms = new MailerSend();

    ms.setToken("Your API token");

    try {
    
        MailerSendResponse response = ms.emails().send(email);
        System.out.println(response.messageId);
    } catch (MailerSendException e) {

        e.printStackTrace();
    }
}

Send bulk emails

import com.mailersend.sdk.emails.Email;
import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.MailerSendResponse;
import com.mailersend.sdk.exceptions.MailerSendException;

public void sendBulkEmails() {

    Email email1 = new Email();

    email1.setFrom("name", "your email");
    email1.addRecipient("name", "[email protected]");
   
    email1.setSubject("Email subject 1");

    email1.setPlain("This is the text content for the first email");
    email1.setHtml("<p>This is the HTML content for the first email</p>");

    Email email2 = new Email();

    email2.setFrom("name", "your email");
    email2.addRecipient("name", "[email protected]");
   
    email2.setSubject("Email subject 2");

    email2.setPlain("This is the text content for the second email");
    email2.setHtml("<p>This is the HTML content for the second email</p>");

    MailerSend ms = new MailerSend();

    ms.setToken("Your API token");

    try {
    
        String bulkSendId = ms.emails().bulkSend(new Email[] { email1, email2 });

        // you can use the bulkSendId to get the status of the emails
        System.out.println(bulkSendId);

    } catch (MailerSendException e) {

        e.printStackTrace();
    }
}

Get bulk request status

import com.mailersend.sdk.emails.Email;
import com.mailersend.sdk.emails.BulkSendStatus;
import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.MailerSendResponse;
import com.mailersend.sdk.exceptions.MailerSendException;

public void getBulkEmailsStatus() {

    MailerSend ms = new MailerSend();

    ms.setToken("Your API token");

    try {
    
        BulkSendStatus status = ms.emails().bulkSendStatus("bulk send id");

        System.out.println(status.state);

    } catch (MailerSendException e) {

        e.printStackTrace();
    }
}

Inbound routes

Get a list of inbound routes

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.inboundroutes.InboundRoute;
import com.mailersend.sdk.inboundroutes.InboundRoutesList;

public void getInboundRoutes() {
    
    MailerSend ms = new MailerSend();

    ms.setToken("Your API token");

    try {
    
        InboundRoutesList routes = ms.inboundRoutes().getRoutes();

        for (InboundRoute route : routes.routes) {
            System.out.println(route.id);
        }

    } catch (MailerSendException e) {

        e.printStackTrace();
    }
}

Get an inbound route

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.inboundroutes.InboundRoute;

public void getInboundRoute() {
    
    MailerSend ms = new MailerSend();

    ms.setToken("Your API token");

    try {
    
        InboundRoute route = ms.inboundRoutes().getRoute("inbound route id");

        System.out.println(route.id);
        System.out.println(route.name);

    } catch (MailerSendException e) {

        e.printStackTrace();
    }
}

Create an inbound route

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.inboundroutes.Forward;

public void createInboundRoute() {
    
    MailerSend ms = new MailerSend();

    ms.setToken("Your API token");

    try {

        Forward forward = new Forward();
        forward.type = "webhook";
        forward.value = "https://example-domain.com";
        forward.secret = "asdfgh";
    
        ms.inboundRoutes().builder()
            .domainId("domain id")
            .name("Test inbound name")
            .domainEnabled(false)
            .matchFilter("match_all")
            .forwards(new Forward[] { forward })
            .addRoute();

    } catch (MailerSendException e) {

        e.printStackTrace();
    }
}

Update an inbound route

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.inboundroutes.Forward;
import com.mailersend.sdk.inboundroutes.InboundRoute;

public void updateInboundRoute() {
    
    MailerSend ms = new MailerSend();

    ms.setToken("Your API token");

    try {

        Forward forward = new Forward();
        forward.type = "webhook";
        forward.value = "https://example-domain.com";
        forward.secret = "asdfgh";
    
        InboundRoute route = ms.inboundRoutes().builder()
            .domainId("domain id")
            .name("Updated route name")
            .domainEnabled(false)
            .matchFilter("match_all")
            .forwards(new Forward[] { forward })
            .updateRoute("inbound route id");

        System.out.println(route.id);
        System.out.println(route.name);

    } catch (MailerSendException e) {

        e.printStackTrace();
    }
}

Delete an inbound route

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;

public void deleteInboundRoute() {
    
    MailerSend ms = new MailerSend();

    ms.setToken("Your API token");

    try {

        boolean result = ms.inboundRoutes().deleteRoute("inbound route id");

        System.out.println(result);

    } catch (MailerSendException e) {

        e.printStackTrace();
    }
}

Activities

The SDK provides a simple interface to retrieve a list of activities for a domain.

The SDK returns an Activities object on successful send or throws a MailerSendException on a failed one.

Through the Activities object you can get the list of activities, get the next page of results, convert an activity into an email for resend, etc.

Get a list of activities

import com.mailersend.sdk.ActivitiesList;
import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.MailerSendResponse;
import com.mailersend.sdk.exceptions.MailerSendException;

public void getActivities() {

    MailerSend ms = new MailerSend();

    ms.setToken("Your API token");

    try {
    
        ActivitiesList activities = ms.activities().getActivities("domain id");

        for (Activity activity : activities.activities) {

            System.out.println(activity.id);
            System.out.println(activity.createdAt.toString());

            System.out.println(activity.email.from);
            System.out.println(activity.email.subject);
            System.out.println(activitiy.email.recipient.email);
        }

        
    } catch (MailerSendException e) {

        e.printStackTrace();
    }
}

Activities filters

import com.mailersend.sdk.ActivitiesList;
import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.MailerSendResponse;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.util.EventTypes;

public void getActivities() {

    MailerSend ms = new MailerSend();

    ms.setToken("Your API token");

    try {
    
        int page = 1;
        int limit = 25; // also the default limit is 25
        Date dateFrom = DateUtils.addDays(new Date(), -30); // you'll need apache-commons for this
        Date dateTo = new Date();

        String events[] = {EventTypes.OPENED, EventTypes.SENT}; // check com.mailsersend.sdk.util.EventTypes for a full list of events

        ActivitiesList activities = ms.activities().getActivities("domain id", page, limit, dateFrom, dateTo, events);

        for (Activity activity : activities.activities) {

            System.out.println(activity.id);
        }
        
    } catch (MailerSendException e) {

        e.printStackTrace();
    }
}

Activities pagination

import com.mailersend.sdk.ActivitiesList;
import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.MailerSendResponse;
import com.mailersend.sdk.exceptions.MailerSendException;

public void getActivities() {

    MailerSend ms = new MailerSend();

    ms.setToken("Your API token");

    try {
    
        // without any filters, the default limit is 25
        ActivitiesList activities = ms.activities().getActivities("domain id");

        System.out.println(activities.getCurrentPage());

        for (Activity activity : activities.activities) {

            System.out.println(activity.id);
        }


        // get the next page
        ActivitiesList nextPage = activities.nextPage();

        System.out.println(nextPage.getCurrentPage());

        for (Activity activity : nextPage.activities) {

            System.out.println(activity.id);
        }


        // you can also get the previous page
        ActivitiesList previousPage = nextPage.previousPage();

        System.out.println(previousPage.getCurrentPage());

        for (Activity activity : previousPage.activities) {

            System.out.println(activity.id);
        }

    } catch (MailerSendException e) {

        e.printStackTrace();
    }
}

Activity email for resend

import com.mailersend.sdk.ActivitiesList;
import com.mailersend.sdk.Email;
import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.MailerSendResponse;
import com.mailersend.sdk.exceptions.MailerSendException;

public void getActivities() {

    MailerSend ms = new MailerSend();

    ms.setToken("Your API token");

    try {
    
        // without any filters, the default limit is 25
        ActivitiesList activities = ms.activities().getActivities("domain id");

        Activity activity = ms.activities().activities[0];

        Email email = activity.email.toEmail();

        // you can change the email contents or add a template id and send it with `ms.send(email)`

    } catch (MailerSendException e) {

        e.printStackTrace();
    }
}

Analytics

Analytics retrieval follows the builder pattern and is accessible from the MailerSend.analytics object.

Activity data by date

import java.util.Date;
import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.analytics.AnalyticsByDate;
import com.mailersend.sdk.analytics.AnalyticsByDateList;
import com.mailersend.sdk.analytics.AnalyticsList;
import com.mailersend.sdk.analytics.AnalyticsStatistic;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.util.EventTypes;

public void getAnalyticsByDate() {

    MailerSend ms = new MailerSend();
    ms.setToken(TestHelper.validToken);
    
    try {
        
        Date dateFrom = new Date(); // set your from date normally
        
        AnalyticsByDateList list = ms.analytics()
                .dateFrom(dateFrom)
                .dateTo(new Date())
                .domainId(TestHelper.domainId)
                .getByDate(new String[] {EventTypes.DELIVERED, EventTypes.OPENED, EventTypes.CLICKED});
        
        System.out.println("\n\nAnalytics by date for domain:");
        for (AnalyticsByDate dayStat : list.statistics) {
            
            System.out.println(dayStat.statDate.toString());
            System.out.println(dayStat.delivered);
            System.out.println(dayStat.opened);
            System.out.println(dayStat.clicked);
                            
        }
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Opens by country

import java.util.Date;
import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.analytics.AnalyticsList;
import com.mailersend.sdk.analytics.AnalyticsStatistic;
import com.mailersend.sdk.exceptions.MailerSendException;

public void getOpensByCountry() {

    MailerSend ms = new MailerSend();
    ms.setToken(TestHelper.validToken);
    
    try {
        
        Date dateFrom = new Date(); // set your from date normally
        
        AnalyticsList list = ms.analytics()
                .dateFrom(dateFrom)
                .dateTo(new Date())
                .domainId(TestHelper.domainId)
                .getOpensByCountry();
        
        System.out.println("\n\nOpens by country:");
        
        for (AnalyticsStatistic stat : list.statistics) {
            
            System.out.println(stat.name + " - " + stat.count);
        }
                
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Opens by user agent name

import java.util.Date;
import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.analytics.AnalyticsList;
import com.mailersend.sdk.analytics.AnalyticsStatistic;
import com.mailersend.sdk.exceptions.MailerSendException;

public void getOpensByUserAgentName() {

    MailerSend ms = new MailerSend();
    ms.setToken(TestHelper.validToken);
    
    try {
        
        Date dateFrom = new Date(); // set your from date normally
        
        AnalyticsList list = ms.analytics()
                .dateFrom(dateFrom)
                .dateTo(new Date())
                .getOpensByUserAgent();
        
        System.out.println("\n\nOpens by user agent:");
        
        for (AnalyticsStatistic stat : list.statistics) {
            
            System.out.println(stat.name + " - " + stat.count);
        }
                
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Opens by reading environment

import java.util.Date;
import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.analytics.AnalyticsList;
import com.mailersend.sdk.analytics.AnalyticsStatistic;
import com.mailersend.sdk.exceptions.MailerSendException;

public void getOpensByUserAgentType() {

    MailerSend ms = new MailerSend();
    ms.setToken(TestHelper.validToken);
    
    try {
        
        Date dateFrom = new Date(); // set your from date normally
        
        AnalyticsList list = ms.analytics()
                .dateFrom(dateFrom)
                .dateTo(new Date())
                .getOpensByUserAgenType();
        
        System.out.println("\n\nOpens by user agent type:");
        
        for (AnalyticsStatistic stat : list.statistics) {
            
            System.out.println(stat.name + " - " + stat.count);
        }
                
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Domains

Get a list of domains

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.domains.Domain;
import com.mailersend.sdk.domains.DomainsList;
import com.mailersend.sdk.exceptions.MailerSendException;

public void DomainsList() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("token");
    
    try {
        
        DomainsList list = ms.domains().getDomains();
        
        for (Domain domain : list.domains) {
            
            System.out.println(domain.id);
            System.out.println(domain.name);
        }
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Get a single domain

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.domains.Domain;
import com.mailersend.sdk.exceptions.MailerSendException;

public void SingleDomain() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("api token");
    
    try {
        
        Domain domain = ms.domains().getDomain("domain id");
        
        System.out.println(domain.id);
        System.out.println(domain.name);
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Delete a domain

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;

public void DeleteDomain() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("api token");
    
    try {
        
        boolean domainDeleted = ms.domains().deleteDomain("domain id");
        
        System.out.println("Domain deleted: ".contains(String.valueOf(domainDeleted)));
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Add a domain

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.domains.Domain;
import com.mailersend.sdk.exceptions.MailerSendException;

public void AddDomain() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("api token");
    
    try {
        
        Domain domain = ms.domains().addDomainBuilder().addDomain("domain to add");
        
        System.out.println(domain.id);
        System.out.println(domain.name);
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Get DNS Records

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.domains.Domain;
import com.mailersend.sdk.domains.DomainDnsAttribute;
import com.mailersend.sdk.domains.DomainDnsRecords;
import com.mailersend.sdk.exceptions.MailerSendException;

public void DomainDnsRecords() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("api token");
    
    try {
        
        DomainDnsRecords records = ms.domains().getDomainDnsRecords("domain id");
        
        printDomainDnsAttribute(records.spf);
        printDomainDnsAttribute(records.dkim);
        printDomainDnsAttribute(records.customTracking);
        printDomainDnsAttribute(records.returnPath);
        printDomainDnsAttribute(records.inboundRouting);
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

private void printDomainDnsAttribute(DomainDnsAttribute attribute) {
    
    System.out.println(attribute.hostname);
    System.out.println(attribute.type);
    System.out.println(attribute.value);
}

Verify a Domain

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.domains.DomainVerificationStatus;
import com.mailersend.sdk.exceptions.MailerSendException;

public void VerifyDomain() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("api token");
    
    try {
        
        DomainVerificationStatus status = ms.domains().verifyDomain("domain id");
        
        System.out.println(status.message);
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Get a list of recipients per domain

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.domains.DomainRecipientsList;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.util.ApiRecipient;

public void ReceipientsPerDomain() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("api token");
    
    try {
        
        DomainRecipientsList list = ms.domains().getDomainRecipients("domaion id");
        
        for (ApiRecipient recipient : list.recipients) {
            
            System.out.println(recipient.email);
        }
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Update domain settings

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.domains.Domain;
import com.mailersend.sdk.exceptions.MailerSendException;

public void UpdateDomainSettings() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("api token");
    
    try {
        
        Domain domain = ms.domains().updateDomainSettingsBuilder()
            .customnTrackingEnabled(true)
            .sendPaused(false)
            .updateDomain("domain id");
        
        System.out.println(domain.domainSettings.customTrackingEnabled);
        System.out.println(domain.domainSettings.sendPaused);
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Messages

Get a list of messages

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.messages.Message;
import com.mailersend.sdk.messages.MessagesList;

public void MessagesList() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("token");
    
    try {
        
        MessagesList list = ms.messages().getMessages();
        
        for (MessageListItem message : list.messages) {
            
            System.out.println(message.id);
            System.out.println(message.createdAt.toString());
        }
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Get a single message

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.messages.Message;
import com.mailersend.sdk.messages.MessagesListItem;

public void SingleMessage() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("token");
    
    try {
        
        Message message = ms.messages().getMessage("message id");
        
        System.out.println(message.id);
        System.out.println(message.createdAt.toString());
        System.out.println(message.domain.name);
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Scheduled messages

Get a list of scheduled messages

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.scheduledmessages.ScheduledMessagesList;
import com.mailersend.sdk.scheduledmessages.ScheduledMessage;

public void getScheduledMessages() {
    
    MailerSend ms = new MailerSend();

    ms.setToken("Your API token");

    try {
    
        ScheduledMessagesList messages = ms.scheduledmessages().getScheduledMessages();

        for (ScheduledMessage message : messages.scheduledMessages) {
            System.out.println(message.id);
            System.out.println(message.subject);
        }

    } catch (MailerSendException e) {

        e.printStackTrace();
    }
}

Get a scheduled message

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.scheduledmessages.ScheduledMessage;

public void getScheduledMessage() {
    
    MailerSend ms = new MailerSend();

    ms.setToken("Your API token");

    try {
    
        ScheduledMessage message = ms.scheduledmessages().getScheduledMessage("message id");

        System.out.println(message.id);
        System.out.println(message.subject);

    } catch (MailerSendException e) {

        e.printStackTrace();
    }
}

Delete a scheduled message

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.scheduledmessages.ScheduledMessage;

public void deleteScheduledMessage() {
    
    MailerSend ms = new MailerSend();

    ms.setToken("Your API token");

    try {
    
        boolean result = ms.scheduledmessages().deleteScheduledMessage("message id");

        System.out.println(result);

    } catch (MailerSendException e) {

        e.printStackTrace();
    }
}

Tokens

Create a token

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.tokens.Token;
import com.mailersend.sdk.tokens.TokenAdd;
import com.mailersend.sdk.tokens.TokenScopes;

public void CreateToken() {
    
    MailerSend ms = new MailerSend();
    ms.setToken(TestHelper.validToken);
    
    try {
        
        TokenAdd token = ms.tokens().addBuilder()
            .name("Test token")
            .domainId("domain id")
            .addScope(TokenScopes.activityFull)
            .addScope(TokenScopes.analyticsFull)
            .addToken();
        
        System.out.println(token.id);
        System.out.println(token.name);
        System.out.println(token.accessToken);
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Update token

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.tokens.Token;

public void CreateToken() {
    
    MailerSend ms = new MailerSend();
    ms.setToken(TestHelper.validToken);
    
    try {
        
    MailerSend ms = new MailerSend();
    ms.setToken(TestHelper.validToken);
    
    try {

        // true to pause it, false to unpause it
        Token token = ms.tokens().updateToken(T"token id", true);
        
        System.out.println(token.name);
        System.out.println(token.status);
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
        fail();
    }
}

Delete token

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.tokens.Token;

public void CreateToken() {
    
    MailerSend ms = new MailerSend();
    ms.setToken(TestHelper.validToken);
    
    try {
        MailerSendResponse response = ms.tokens().deleteToken("token to delete");
        
        System.out.println(response.responseStatusCode);
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Recipients

Get a list of recipients

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.util.ApiRecipient;
import com.mailersend.sdk.util.ApiRecipientsList;

public void GetRecipients() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        ApiRecipientsList list = ms.recipients().getRecipients();
        
        for (ApiRecipient recipient : list.recipients) {
            
            System.out.println(recipient.id);
            System.out.println(recipient.email);
        }

    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Get a single recipient

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.recipients.Recipient;

public void GetSingleRecipient() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        Recipient recipient = ms.recipients().getRecipient("recipient id");
        
        System.out.println(recipient.email);

    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Delete a recipient

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.MailerSendResponse;

public void DeleteRecipient() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        MailerSendResponse response = ms.recipients().deleteRecipient("recipient id");
        
        System.out.println(response.responseStatusCode);

    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Get recpients from a suppression list

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.MailerSendResponse;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.recipients.BlocklistItem;
import com.mailersend.sdk.recipients.BlocklistListResponse;
import com.mailersend.sdk.recipients.SuppressionItem;
import com.mailersend.sdk.recipients.SuppressionList;

public void GetRecipientsFromSuppressionList() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        BlocklistListResponse blocklist = ms.recipients().suppressions().getBlocklist();
        
        for (BlocklistItem item : blocklist.items) {
            
            System.out.println(item.id);
            System.out.println(item.pattern);
            System.out.println(item.type);
        }
        
        SuppressionList hardBounces = ms.recipients().suppressions().getHardBounces();
        
        for (SuppressionItem item : hardBounces.items) {
            
            System.out.println(item.id);
            System.out.println(item.recipient.email);
        }
        
        SuppressionList spamComplaints = ms.recipients().suppressions().getSpamComplaints();
        
        for (SuppressionItem item : spamComplaints.items) {
            
            System.out.println(item.id);
            System.out.println(item.recipient.email);
        }
        
        SuppressionList unsubscribes = ms.recipients().suppressions().getUnsubscribes();
        
        for (SuppressionItem item : unsubscribes.items) {
            
            System.out.println(item.id);
            System.out.println(item.recipient.email);
        }

    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Add recpients to a suppression list

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.MailerSendResponse;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.recipients.BlocklistItem;
import com.mailersend.sdk.recipients.BlocklistListResponse;
import com.mailersend.sdk.recipients.SuppressionItem;
import com.mailersend.sdk.recipients.SuppressionList;

public void AddRecipientsToSuppressionList() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        // blocklist
        ms.recipients().suppressions().addBuilder().pattern(".*@example.com");
        ms.recipients().suppressions().addBuilder().recipient("[email protected]");
        BlocklistItem[] items = ms.recipients().suppressions().addBuilder().addToBlocklist();
        
        for (BlocklistItem item : items) {
            
            System.out.println(item.id);
        }
        
        
        // hard bounces
        ms.recipients().suppressions().addBuilder().recipient("[email protected]");
        ms.recipients().suppressions().addBuilder().domainId(TestHelper.domainId);
        SuppressionList list = ms.recipients().suppressions().addBuilder().addRecipientsToHardBounces();
        
        for (SuppressionItem item : list.items) {
            
            System.out.println(item.id);
        }
        
        
        // spam complaints
        ms.recipients().suppressions().addBuilder().recipient("[email protected]");
        ms.recipients().suppressions().addBuilder().domainId(TestHelper.domainId);
        list = ms.recipients().suppressions().addBuilder().addRecipientsToSpamComplaints();
        
        for (SuppressionItem item : list.items) {
            
            System.out.println(item.id);
        }
        
        
        // unsubscribes
        ms.recipients().suppressions().addBuilder().recipient("[email protected]");
        ms.recipients().suppressions().addBuilder().domainId(TestHelper.domainId);
        list = ms.recipients().suppressions().addBuilder().addRecipientsToUnsubscribes();
        
        for (SuppressionItem item : list.items) {
            
            System.out.println(item.id);
        }

    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Delete recipients from a suppression list

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.MailerSendResponse;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.recipients.BlocklistItem;
import com.mailersend.sdk.recipients.BlocklistListResponse;
import com.mailersend.sdk.recipients.SuppressionItem;
import com.mailersend.sdk.recipients.SuppressionList;

public void DeleteRecipientsFromSuppressionList () {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        // delete from blocklist
        BlocklistListResponse blocklist = ms.recipients().suppressions().getBlocklist();
        
        if (blocklist.items.length == 0) {
            
            fail();
        }
        
        String itemId = blocklist.items[0].id;
        
        MailerSendResponse response = ms.recipients().suppressions().deleteBlocklistItems(new String[] { itemId });
        
        System.out.println(response.responseStatusCode);
        
        
        // delete from hard bounces
        SuppressionList hardBounces = ms.recipients().suppressions().getHardBounces();
        
        if (hardBounces.items.length == 0) {
            
            fail();
        }
        
        itemId = hardBounces.items[0].id;
        
        response = ms.recipients().suppressions().deleteHardBouncesItems(new String[] { itemId });
        
        System.out.println(response.responseStatusCode);
        
        
        // delete from spam complaints
        SuppressionList spamComplaints = ms.recipients().suppressions().getSpamComplaints();
        
        if (spamComplaints.items.length == 0) {
            
            fail();
        }
        
        itemId = spamComplaints.items[0].id;
        
        response = ms.recipients().suppressions().deleteSpamComplaintsItems(new String[] { itemId });
        
        System.out.println(response.responseStatusCode);
        

        // delete from unsubscribes
        SuppressionList unsubscribes = ms.recipients().suppressions().getUnsubscribes();
        
        if (unsubscribes.items.length == 0) {
            
            fail();
        }
        
        itemId = unsubscribes.items[0].id;
        
        response = ms.recipients().suppressions().deleteUnsubscribesItems(new String[] { itemId });
        
        System.out.println(response.responseStatusCode);

    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Webhooks

Get a list of webhooks

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.webhooks.Webhook;
import com.mailersend.sdk.webhooks.WebhooksList;

public void GetWebhooks() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        WebhooksList list = ms.webhooks().getWebhooks("domain id");
        
        for (Webhook webhook : list.webhooks) {
            
            System.out.println(webhook.name);
        }
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Get a single webhook

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.webhooks.Webhook;

public void GetSingleWebhook() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        Webhook webhook = ms.webhooks().getWebhook("webhook id");
        
        System.out.println(webhook.name);
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Create a webhook

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.webhooks.Webhook;

public void CreateWebhook() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        Webhook webhook = ms.webhooks().builder()
            .name("Webhook name")
            .url("Webhook url")
            .addEvent(WebhookEvents.ACTIVITY_OPENED)
            .addEvent(WebhookEvents.ACTIVITY_CLICKED)
            .createWebhook("domain id");
        
        System.out.println(webhook.name);
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Update a webhook

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.webhooks.Webhook;

public void UpdateWebhook() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        Webhook webhook = ms.webhooks()
                .builder()
                .name("Updated webhook name")
                .updateWebhook("webhook id");
                    
        System.out.println(webhook.name);
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Delete a webhook

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.MailerSendResponse;
import com.mailersend.sdk.exceptions.MailerSendException;

public void DeleteWebhook() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        MailerSendResponse response = ms.webhooks().deleteWebhook("webhook id");
            
        System.out.println(response.responseStatusCode);
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Templates

Get a list of templates

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.templates.TemplateItem;
import com.mailersend.sdk.templates.TemplatesList;

public void getTemplatesList() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
            TemplatesList list = ms.templates().getTemplates();
            
            for (TemplateItem item : list.templates) {
                
                System.out.println(item.id);
                System.out.println(item.name);
            }
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Get a single template

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.MailerSendResponse;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.templates.Template;
import com.mailersend.sdk.templates.TemplateItem;
import com.mailersend.sdk.templates.TemplatesList;

public void getTemplate() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
            Template template = ms.templates().getTemplate("template id");
            
            System.out.println(template.id);
            System.out.println(template.name);
            System.out.println(template.imagePath);
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Delete a template

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.MailerSendResponse;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.templates.Template;
import com.mailersend.sdk.templates.TemplateItem;
import com.mailersend.sdk.templates.TemplatesList;

public void deleteTemplate() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
            MailerSendResponse response = ms.templates().deleteTemplate("template id");
            
            System.out.println(response.responseStatusCode);
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Email verification

Get all email verification lists

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailsend.sdk.emailverification.EmailVerificationList;
import com.mailsend.sdk.emailverification.EmailVerificationLists;

public void getLists() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        EmailVerificationLists lists = ms.emailVerification().getLists();
            
        for (EmailVerificationList list : lists.lists) {
            System.out.println(list.id);
            System.out.println(list.name);
        }
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Get an email verification list

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailsend.sdk.emailverification.EmailVerificationList;

public void getList() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        EmailVerificationList list = ms.emailVerification().getList("list id");

        System.out.println(list.name);
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Create an email verification list

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailsend.sdk.emailverification.EmailVerificationList;

public void createList() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        EmailVerificationList list = ms.emailVerification().builder()
			.name("Test email verification")
			.addEmail("[email protected]")
			.addEmail("[email protected]")
			.addEmail("[email protected]")
			.create();

        System.out.println(list.id);
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Verify an email list

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailsend.sdk.emailverification.EmailVerificationList;

public void verifyList() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        EmailVerificationList list = ms.emailVerification().verifyList("list id");

        System.out.println(list.status.name);
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Get email verification list results

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailsend.sdk.emailverification.ListVerificationResults;
import com.mailsend.sdk.emailverification.VerificationResult;

public void verifyList() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        ListVerificationResults results = ms.emailVerification().verificationResults("list id");

        for (VerificationResult result : results.results) {
            System.out.println(result.address);
            System.out.println(result.result);
        }
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

SMS activity

Get a list of SMS activities

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailsend.sdk.sms.activities.SmsActivityList;
import com.mailsend.sdk.sms.activities.SmsActivity;

public void getActivities() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        SmsActivityList list = ms.sms().activities().getActivities();
            
        for (SmsActivity activity : lists.smsActivities) {
            System.out.println(activity.id);
            System.out.println(activity.content);
        }
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Get activity of a single message

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailsend.sdk.sms.activities.SmsActivity;

public void getActivity() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        SmsActivity activity = ms.sms().activities().getMessageActivity("message id");
            
        System.out.println(activity.id);
        System.out.println(activity.content);
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

SMS messages

Get a list of SMS messages

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailsend.sdk.sms.messages.SmsMessageList;
import com.mailsend.sdk.sms.messages.SmsMessage;

public void getSmsMessages() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        SmsMessageList list = ms.sms().messages().getMessages();
            
        for (SmsMessage message : list.messages) {
            System.out.println(message.id);
            System.out.println(message.text);
        }
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Get info on a SMS message

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailsend.sdk.sms.messages.SmsMessage;

public void getSmsMessage() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        SmsMessage message = ms.sms().messages().getMessage("message id");

        System.out.println(message.id);
        System.out.println(message.text);
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

SMS Phone numbers

Get a list of SMS phone numbers

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.sms.phonenumbers.PhoneNumber;
import com.mailersend.sdk.sms.phonenumbers.PhoneNumberList;

public void getSmsPhoneNumbers() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        PhoneNumberList numbers = ms.sms().phoneNumbers().getPhoneNumbers();
        
        for (PhoneNumber number : numbers.phoneNumbers) {
            
            System.out.println(number.id);
        }
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Get an SMS phone number

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.sms.phonenumbers.PhoneNumber;

public void getSmsPhoneNumber() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
                
        PhoneNumber number = ms.sms().phoneNumbers().getPhoneNumber("phone number id");

        System.out.println(number.id);
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Update a single SMS phone number

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.sms.phonenumbers.PhoneNumber;

public void updateSmsPhoneNumber() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        boolean pausePhoneNumber = false;
        PhoneNumber number = ms.sms().phoneNumbers().updatePhoneNumber("phone number id", pausePhoneNumber);
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Delete an SMS phone number

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;

public void deleteSmsPhoneNumber() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        boolean result = ms.sms().phoneNumbers().deletePhoneNumber("phone number id");

        System.out.println(result);
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

SMS recipients

Get a list of SMS recipients

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.sms.recipients.SmsRecipient;
import com.mailersend.sdk.sms.recipients.SmsRecipientList;

public void getSmsRecipients() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        SmsRecipientList list = ms.sms().recipients().getRecipients();
        
        for (SmsRecipient recipient : list.recipients) {
            
            System.out.println(recipient.id);
        }
        
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Get an SMS recipient

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.sms.recipients.SmsRecipient;

public void getSmsRecipient() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        SmsRecipient recipient = ms.sms().recipients().getRecipient("recipient id");
        
        System.out.println(recipient.id);
        
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Update a single SMS recipient

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.sms.recipients.SmsRecipient;

public void updateSmsRecipient() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        String status = "opt_out";

        SmsRecipient recipient = ms.sms().recipients().updateRecipient("recipient id", status);
        
        System.out.println(recipient.status);
        
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

SMS inbounds

Get a list of SMS inbound routes

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.sms.inboundroutes.SmsInboundRoute;
import com.mailersend.sdk.sms.inboundroutes.SmsInboundRouteList;

public void getSmsInboundRoutes() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        SmsInboundRouteList routes = ms.sms().inboundRoutes().getSmsInboundRoutes();
        
        for (SmsInboundRoute route : routes.routes) {
            
            System.out.println(route.id);
        }
        
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Get a single SMS inbound route

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.sms.inboundroutes.SmsInboundRoute;

public void getSmsInboundRoute() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        SmsInboundRoute route = ms.sms().inboundRoutes().getSmsInboundRoute("route id");
        
        System.out.println(route.id);
        
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Add an SMS inbound route

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.sms.inboundroutes.SmsInboundRoute;

public void addSmsInboundRoute() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        SmsInboundRoute route = ms.sms().inboundRoutes().builder()
                .smsNumberId("sms number id")
                .name("Test inbound route")
                .enabled(false)
                .forwardUrl("https://example.com")
                .filter("equal", "START")
                .addSmsInboundRoute();
        
        System.out.println(route.id);
        
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Update an inbound route

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.sms.inboundroutes.SmsInboundRoute;

public void updateSmsInboundRoute() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        SmsInboundRoute route = ms.sms().inboundRoutes().builder()
                .smsNumberId("sms number id")
                .name("Test inbound route updated")
                .enabled(false)
                .forwardUrl("https://example.com")
                .filter("equal", "START")
                .updateSmsInboundRoute("route id");
        
        System.out.println(route.name);
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Delete an SMS inbound route

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sdk.sms.inboundroutes.SmsInboundRoute;

public void deleteSmsInboundRoute() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        boolean result = ms.sms().inboundRoutes().deleteSmsInboundRoute("route id");
        
        System.out.println(result);
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

SMS webhooks

Get a list of SMS webhooks

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sms.webhooks.SmsWebhook;
import com.mailersend.sms.webhooks.SmsWebhookList;

public void getSmsWebhooks() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        SmsWebhookList list = ms.sms().webhooks().getWebhooks("phone number id");
        
        for (SmsWebhook webhook : list.webhooks) {
            
            System.out.println(webhook.id);
        }
        
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Get an SMS webhook

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sms.webhooks.SmsWebhook;

public void getSmsWebhook() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        SmsWebhook webhook = ms.sms().webhooks().getWebhook("webhook id");
        
        System.out.println(webhook.id);
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Create an SMS webhook

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sms.webhooks.SmsWebhook;

public void createSmsWebhook() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        SmsWebhook webhook = ms.sms().webhooks().builder()
            .addEvent("sms.sent")
            .name("sms webhook")
            .url("https://example.com")
            .createWebhook("sms phone number id");
        
        System.out.print(webhook.id);
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Update an SMS webhook

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;
import com.mailersend.sms.webhooks.SmsWebhook;

public void updateSmsWebhook() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        SmsWebhook webhook = ms.sms().webhooks().builder()
        	.name("sms updated webhook")
        	.updateWebhook("webhook id");
        	
        System.out.print(webhook.name);
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Delete an SMS webhook

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;

public void deleteSmsWebhook() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        boolean result = ms.sms().webhooks().deleteWebhook("webhook id");
        	
        System.out.print(result);
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

SMS

Send an SMS with personalization

import com.mailersend.sdk.MailerSend;
import com.mailersend.sdk.exceptions.MailerSendException;

public void sendSms() {
    
    MailerSend ms = new MailerSend();
    ms.setToken("mailersend token");
    
    try {
        
        String messageId = ms.sms().builder().from("from phone number")
        .addRecipient("to phone number")
        .text("test sms {{name}}")
        .addPersonalization("to phone number", "name", "name personalization")
        .send();
        
        System.out.println(messageId);
        
    } catch (MailerSendException e) {
        
        e.printStackTrace();
    }
}

Testing

Change the properties in the TestHelper class of the com.mailersend.sdk.tests package to correspond to your account details, then simply run

mvn test

Support and Feedback

In case you find any bugs, submit an issue directly here in GitHub.

You are welcome to create an SDK for any other programming language.

If you have any troubles using our API or SDK free to contact our support by email [email protected]

The official documentation is at https://developers.mailersend.com

License

The MIT License (MIT)

mailersend-java's People

Contributors

dagnelies avatar dependabot[bot] avatar fosron avatar johnkelesidis avatar nachots-codefuente avatar nklmilojevic avatar robgordon89 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

mailersend-java's Issues

Inbound routes

  • Get a list of inbound routes
  • Get an inbound route
  • Create an inbound route
  • Update an inbound route
  • Delete an inbound route

Camelcase methods should start with lowercase

By convention in Java methods start with lowercase. They are documented as such in the readme, but wrong in the code (1.0.0).

For instance AddBccshould be addBcc, also many others...


    public void AddRecipient(Recipient recipient) {
        this.recipients.add(recipient);
    }

    public void AddRecipients(Recipient[] recipients) {
        this.recipients.addAll(Arrays.asList(recipients));
    }

    public void AddCc(String name, String email) {
        Recipient recipient = new Recipient(name, email);
        this.cc.add(recipient);
    }

    public void AddCc(Recipient recipient) {
        this.cc.add(recipient);
    }

    public void AddBcc(String name, String email) {
        Recipient recipient = new Recipient(name, email);
        this.bcc.add(recipient);
    }

    public void AddBcc(Recipient recipient) {
        this.bcc.add(recipient);
    }

    public void AddReplyTo(Recipient replyTo) {
        this.replyTo = replyTo;
    }

    public void AddReplyTo(String name, String email) {
        this.replyTo = new Recipient(name, email);
    }

Variables are not being filled

Fix bug when sending email with template, variables are not being filled

The fix is quite simple, after line 341 in the Email.java file add the code:

this.templateVariables.add(var);

Exception swallowed in MailerSendApi.java line 288

The exception in the catch block is swallowed making debugging production issues very difficult.The exception should be added to the thrown one as a cause. I would suggest other catch blocks be checked for this it makes things very tricky to debug.

variables are not being filled

Fix bug when sending email with template, variables are not being filled

The fix is quite simple, after line 292 in the Email.java file add the code:

this.templateVariables.add(var);

Enable webhook

Please add a possibility to enable the webhook from the sdk(api)

Scheduled messages

  • Schedule an email
  • Get a list of scheduled messages
  • Get a scheduled message
  • Delete a scheduled message

Add the priority parameter to the inbound route endpoints

We have a new parameter called "inbound_priority" on our Inbound Routes. This parameter allows our users to create multiple inbound routes per inbound subdomain. Each one of the routes will be processed in a descending order. Values can be between 0 and 100. 0 has more importance than 100.

Create the Email verification endpoints

  • Get all email verification lists
  • Get an email verification list
  • Create an email verification list
  • Verify an email list
  • Get email verification list results

version 1.0.0

Hello, I would like to ask why the 1.1.1 version I imported does not exist import com.mailersend.sdk.Email
image
image

Add missing parameters to Domains endpoints

We added two parameters for our domains: ignore duplicated recipients and add a precedence bulk header. Both of these parameters have to be added to our related Domains endpoints.

  • Add the ignore_duplicated_recipients and the precedence_bulk parameters to the Get a list of domains endpoint.
  • Add the ignore_duplicated_recipients and the precedence_bulk parameters to the Get a single domain endpoint.
  • Add the ignore_duplicated_recipients and the precedence_bulk parameters to the Add a domain endpoint.
  • Add the ignore_duplicated_recipients and the precedence_bulk parameters to the Update domain settings endpoint.

Suggest to add token as .env variable instead of explicit declaration

To better enforce security on our SDKs and avoid incidents, please avoid suggesting our users set explicit declaration of the API/SMTP token in the code and instead suggest setting the API key as an environment variable.

  • Remove all mentions of the token added to any endpoint "ms.setToken("Your API token");"
  • Suggest users to add the token as a .env variable. Name the variable MAILERSEND_API_KEY.
  • Make it optional.

See Slack convo

Update the list of all webhook events

Please update the list of all webhook events in the documentation or any place where they are mentioned:
SOFT_BOUNCED = "activity.soft_bounced",
HARD_BOUNCED = "activity.hard_bounced",
OPENED = "activity.opened",
OPENED_UNIQUE = "activity.opened_unique",
CLICKED = "activity.clicked",
CLICKED_UNIQUE = "activity.clicked_unique",
UNSUBSCRIBED = "activity.unsubscribed",
SPAM_COMPLIANT = "activity.spam_complaint",
SURVEY_OPENED = "activity.survey_opened",
SURVEY_SUBMITTED = "activity.survey_submitted",
IDENTITY_VERIFIED = "sender_identity.verified",
MAINTENANCE_START = "maintenance.start",
MAINTENANCE_END = "maintenance.end",

v1.0.1 is not available in Maven repo

Hi there,

Your documentation says to grab this dependency:

<dependency>
  <groupId>com.mailersend</groupId>
  <artifactId>java-sdk</artifactId>
  <version>1.0.1</version>
</dependency>

Only one I find to be available is:

<dependency>
  <groupId>com.mailersend</groupId>
  <artifactId>java-sdk</artifactId>
  <version>1.0.0</version>
</dependency>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.