Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Java library to easily send emails using the Mailgun service

License

NotificationsYou must be signed in to change notification settings

sargue/mailgun

Repository files navigation

JavadocsBuild StatusLicense

PLEASE READ (there's now an official library)

Since I created this library Mailgun has released anofficial Java library. Please check it.You might want to use it instead of this one, as it has more features and ismaintained by the Mailgun team. I haven't used it myself.

Introduction

What is this?

This is a small Java library to ease the sending of email messages using thegreatMailgun service. It uses the RESTful Javaclient libraryJersey.

What isMailgun?

An email sending service with a REST API.

What isJersey?

A RESTful java library. Actually, the reference implementation ofJAX-RS, the standard API for RESTful webservices for Java.

Versions and javax.* / jakarta.* package naming

Version 2.x.x usesjakarta.* package naming.

If you need the legacyjavax.* package naming, please stick with 1.x.x versions.

Installation

Add the dependency to your project:

Gradle

implementation 'net.sargue:mailgun:2.0.0'

Maven

<dependency>    <groupId>net.sargue</groupId>    <artifactId>mailgun</artifactId>    <version>2.0.0</version></dependency>

A note about dependencies

This project depends on the Jersey library (see above). The Jersey libraryis part of the biggerglassfish/Oracle ecosystem which apparentlydoesn't have top-notch compatibility very high on its priority list.

Said so, you may encounter problems with dependencies as there are somelibraries which are repackaged under different Maven coordinates and willleak duplicates on your classpath.

Please, seeissue #1 for detailsand workarounds. Thanks for your understanding.

Usage

The library is pretty straighforward. You just need to remember two classes:

  • Configuration: which usually is a singleton you build once and re-use
  • Mail: the entry point to build and send emails
  • MailContent: an optional helper to build HTML and text message bodys

That were three classes but the last one is optional although very usefulif you want to send some simple messages in HTML.

The library is built to be used as a fluent interface, almost a DSL, so the codeis quite self-explanatory.

Javadocs

You canbrowse the javadocspublished thanks to the great javadoc.io service.

Requirements and dependencies

The runtime requirement is Java 8 or higher.

Depends onJersey 2 client.

Android support

There is not. Android is not officially supported. I have no experience on Android development, so I won't be able to help much on any issue. There are anumber of issues raised which indicate that the librarycan be used on Android but YMMV.

The main issue about using this library on android is the repackaging of some packages done by Jersey, likejavax.inject. If using gradle you could try to add this:

configurations {    all*.excludegroup:'org.glassfish.hk2.external',module:'javax.inject'}

Anyway try it and if you find a problem please report it. I will try to help.

Configuration

First, you need to prepare aConfiguration object used by the library.

Usually you can do this once and keep the same object for all your calls. Itis thread safe.

Configurationconfiguration =newConfiguration()    .domain("somedomain.com")    .apiKey("key-xxxxxxxxxxxxxxxxxxxxxxxxx")    .from("Test account","postmaster@somedomain.com");

A note on region endpoints

The default configuration por the API base URL is the US endpoint. If you areusinga different endpoint you should set the base URL manually on theconfiguration object.

Example to use the EU (European) endpoint:

Configurationconfiguration =newConfiguration()    .domain("somedomain.com")    .apiUrl("https://api.eu.mailgun.net/v3")    .apiKey("key-xxxxxxxxxxxxxxxxxxxxxxxxx")    .from("Test account","postmaster@somedomain.com");

Sending a basic email

Mail.using(configuration)    .to("marty@mcfly.com")    .subject("This is the subject")    .text("Hello world!")    .build()    .send();

Sending an email with an attachment

Mail.using(configuration)    .to("marty@mcfly.com")    .subject("This message has an text attachment")    .text("Please find attached a file.")    .multipart()    .attachment(newFile("/path/to/image.jpg"))    .build()    .send();

More examples

Some fields (more or less the ones that make sense) can be repeated.Liketo() to send to multiple recipients,attachment() to includemore than one attachment and so on.

Mail.using(configuration)    .to("marty@mcfly.com")    .to("george@mcfly.com")    .cc("lorraine@mcfly.com")    .cc("dave@mcfly.com")    .subject("This is the subject")    .text("Hello world!")    .build()    .send();
Mail.using(configuration)    .to("marty@mcfly.com")    .subject("This message has an text attachment")    .text("Please find attached a file.")    .multipart()    .attachment(newFile("/path/to/image.jpg"))    .attachment(newFile("/path/to/report.pdf"))    .build()    .send();
Mail.using(configuration)    .to("marty@mcfly.com")    .subject("Activate your account")    .template("account_activation")    .parameter("v:name","Doc Brown")    .build()    .send();

Advanced content using content helpers

The classes on the packagenet.sargue.mailgun.content are designedto easily build basic HTMLmessages. It's not supposed to be used for building cutting edge responsivemodern HTML messages. It's just for simple cases where you need to send amessage with some basic HTML like tables and some formatting.

Some self-explanatory examples:

Mail.using(configuration)    .body()    .h1("This is a heading")    .p("And this some text")    .mail()    .to("marty@mcfly.com")    .subject("This is the subject")    .build()    .send();
Mail.using(configuration)    .body()    .h3("Monthly report")    .p("Report of the number of time travels this month")    .table()        .row("Marty","5")        .row("Doc","7")        .row("Einstein","0")    .end()    .mail()    .to("marty@mcfly.com")    .subject("Monthly Delorean usage")    .build()    .send();

Of course, you can keep the body content and mail building separated.

Bodybody =Body.builder()                .h1("This is a heading")                .p("And this some text")                .build();Mail.using(configuration)    .to("marty@mcfly.com")    .subject("This is the subject")    .content(body)    .build()    .send();

There is also a very powerful extension mechanism which are thecontentconverters. Check it out with some more information about the theseclassesin the wiki.

Changelog

The changelog is in aseparate page.

Test suite

There is a test suite usingWireMock to mock the MailgunREST API endpoint.

The mail content test suite is a work in progress right now.

Contributing

All contributions are welcome. Use the issues' section to send feature requests.Pull requests are also welcome, just try to stick with the overall code styleand provide some tests if possible.

About

Java library to easily send emails using the Mailgun service

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp