Sending Emails with MailerSend and Java
Prerequisites
Before you get started, make sure you have the following:
- Java installed on your machine.
- An active MailerSend account with an API key.
Set up the MailerSend Java SDK
You can use the MailerSend Java SDK by adding it to your project using a dependency management tool like Maven or Gradle.
Maven
Add the following to your pom.xml
file:
<dependency>
<groupId>com.mailersend</groupId>
<artifactId>java-sdk</artifactId>
<version>1.0.0</version>
</dependency>
Gradle
Add the following to your build.gradle
file:
implementation 'com.mailersend:java-sdk:1.0.0'
Set up the MailerSend API Key
To use the MailerSend Java SDK, you will need to set up your API key. You can find your API key in the MailerSend dashboard.
MailerSend ms = new MailerSend();
ms.setToken("YOUR_API_KEY_HERE");
Send an Email
You are now ready to send an email using the MailerSend Java SDK. Here is an example of how to send an email:
Email email = new Email();
email.subject = "Hello from MailerSend!";
email.text = "This is a test email from the MailerSend Java SDK";
email.addRecipient("Recipient name", "recipient@example.com");
email.setFrom("Sender name", "sender@example.com");
MailerSend ms = new MailerSend();
ms.setToken("YOUR_API_KEY_HERE");
MailerSendResponse response = ms.emails().send(email);
System.out.println(response.responseStatusCode);
In this example, we are sending a simple email with a plain text body.
Conclusion
You have successfully sent an email using MailerSend Java SDK. With the SDK, you can easily send emails using the MailerSend API in your Java applications.