Malle Modules

Malle modules

License

License

Categories

Categories

Net
GroupId

GroupId

net.emphased.malle
ArtifactId

ArtifactId

modules
Last Version

Last Version

0.1.0
Release Date

Release Date

Type

Type

pom
Description

Description

Malle Modules
Malle modules

Download modules

Filename Size
modules-0.1.0.pom 846 bytes
Browse

How to add to project

<!-- https://jarcasting.com/artifacts/net.emphased.malle/modules/ -->
<dependency>
    <groupId>net.emphased.malle</groupId>
    <artifactId>modules</artifactId>
    <version>0.1.0</version>
    <type>pom</type>
</dependency>
// https://jarcasting.com/artifacts/net.emphased.malle/modules/
implementation 'net.emphased.malle:modules:0.1.0'
// https://jarcasting.com/artifacts/net.emphased.malle/modules/
implementation ("net.emphased.malle:modules:0.1.0")
'net.emphased.malle:modules:pom:0.1.0'
<dependency org="net.emphased.malle" name="modules" rev="0.1.0">
  <artifact name="modules" type="pom" />
</dependency>
@Grapes(
@Grab(group='net.emphased.malle', module='modules', version='0.1.0')
)
libraryDependencies += "net.emphased.malle" % "modules" % "0.1.0"
[net.emphased.malle/modules "0.1.0"]

Dependencies

compile (1)

Group / Artifact Type Version
com.google.code.findbugs : jsr305 Optional jar 3.0.0

test (3)

Group / Artifact Type Version
junit : junit jar 4.12
org.assertj : assertj-core jar 2.1.0
org.codehaus.groovy : groovy-all jar 2.4.3

Project Modules

  • core
  • javamail
  • freemarker
  • servlet
  • servlet-test
  • test

travis-ci

Malle is a small Java (1.7 or higher) library that aims to provide consistent and fluent interface for sending mail messages. Implemented on top of standard JavaMail with FreeMarker and Java Server Pages for templating support.

Documentation

Getting started

Add to your project

Maven:

<dependency>
    <groupId>net.emphased.malle</groupId>
    <artifactId>malle-javamail</artifactId>
    <version>0.1.0</version>
</dependency>

If FreeMarker template support is needed:

<dependency>
    <groupId>net.emphased.malle</groupId>
    <artifactId>malle-freemarker</artifactId>
    <version>0.1.0</version>
</dependency>

Use

Assuming the JavaMail is properly configured with system properties, sending a mail is as simple as:

new Javamail()
    .mail()
    .from("[email protected]", "Alice")
    .to("[email protected]", "Bob")
    .subject("A mail from Alice")
    .html("<b>Hello Bob!</b>")
    .send();

If not, it's easy to configure it:

MailSystem mailSystem = new Javamail()
    .withProperty("mail.smtp.auth", "true")
    .withProperty("mail.smtp.starttls.enable", "true")
    .withProperty("mail.smtp.host", "smtp.gmail.com")
    .withProperty("mail.smtp.port", "587")
    .withProperty("mail.user", "[email protected]")
    .withProperty(Javamail.PASSWORD_PROP, "<your app password>");

mailSystem.mail()
    .from("[email protected]", "Alice")
    .to("[email protected]", "Bob")
    .subject("A mail from Alice")
    .html("<b>Hello Bob!</b>")
    .send();

Sending some attachments:

String catUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/0/04/So_happy_smiling_cat.jpg/411px-So_happy_smiling_cat.jpg";
String horseUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/f/f3/Funny_Cide.jpg/444px-Funny_Cide.jpg";
mailSystem.mail()
    .from("[email protected]", "Alice")
    .to("[email protected]", "Bob")
    .subject("A mail with some pics from Alice")
    .html("Hey Bob, check out these funny pics ;)")
    .attachment(InputStreamSuppliers.url(catUrl), "cat.jpg")
    .attachment(InputStreamSuppliers.url(horseUrl), "horse.jpg")
    .send();

Sending some inline attachments:

String catUrl = ...
String horseUrl = ...
mailSystem.mail()
    .from("[email protected]", "Alice")
    .to("[email protected]", "Bob")
    .subject("A mail with some inline pics from Alice")
    .html("Hey Bob, look at this cat <img src=\"cid:[email protected]\"/> and horse <img src=\"cid:[email protected]\"/>")
    .inline(InputStreamSuppliers.url(catUrl), "[email protected]")
    .inline(InputStreamSuppliers.url(horseUrl), "[email protected]")
    .send();

Using FreeMarker templating engine:

Configuration fc = new Configuration(Configuration.VERSION_2_3_22);
fc.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
fc.setDirectoryForTemplateLoading(new File("."));
fc.setDefaultEncoding("UTF-8");

MailSystem mailSystem = new Javamail()
    ...
    .withTemplateEngine(new FreemarkerTemplateEngine().withConfiguration(fc));

mailSystem.mail()
    .template("mytemplate.ftl",
              "from_address", "[email protected]",
              "from_personal", "Alice",
              "to_address", "[email protected]",
              "to_personal", "Bob")
    .send();

where mytemplate.ftl looks like this:

<@mail cmd='from' address='${from_address}' personal='${from_personal}'/>
<@mail cmd='to' address='${to_address}' personal='${to_personal}'/>

<#-- Leading and trailing whitespace inside the headers gets trimmed. -->
<@mail cmd='subject' value='A message for ${to_personal} from ${from_personal} sent using Malle and Freemarker'/>

<#-- Whitespace in the text/html part gets trimmed too. -->
<@mail cmd='html'><#escape x as x?html>
    <p>Hello ${to_personal},</p>

    <p>
        <#-- Unicode works as expected. -->
        This is a sample mail from ${from_personal} sent to you using Malle ♡ Freemarker.
    </p>
    <p>
        <#-- Inline image, see below. -->
        <img src="cid:cat.jpg"/>
    </p>
</#escape></@mail>

<#-- Only trailing whitespace in the text/plain part gets trimmed. -->
<@mail cmd='plain'>

Hello ${to_personal},

It's sad that you don't have an HTML-capable mail reader :(
</@mail>

<#-- Classpath resources can be easily attached. -->
<@mail cmd='attachment' name='image.png' resource='image.png'/>

<#-- Inline resources are easy to add as well. -->
<@mail cmd='inline' id='cat.jpg'
       url='https://upload.wikimedia.org/wikipedia/commons/thumb/0/04/So_happy_smiling_cat.jpg/411px-So_happy_smiling_cat.jpg'/>

Configuring with Spring XML:

<bean id="mailSystem" class="net.emphased.malle.javamail.Javamail">
    <property name="templateEngine">
        <bean class="net.emphased.malle.template.freemarker.FreemarkerTemplateEngine">
            <property name="configuration">
                <bean class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean"
                      p:defaultEncoding="UTF-8"
                      p:templateLoaderPath="classpath:mail"/>
            </property>
        </bean>
    </property>
    <property name="properties">
        <map>
            <entry key="mail.smtp.host" value="${mail.smtp.host}"/>
            <entry key="mail.smtp.port" value="${mail.smtp.port}"/>
            <entry key="mail.smtp.connectiontimeout" value="${mail.smtp.connectiontimeout}"/>
            <entry key="mail.smtp.timeout" value="${mail.smtp.timeout}"/>
        </map>
    </property>
</bean>

... and using in a usual way:

public class MyBean {

    @Autowired
    private MailSystem mailSystem;

    public void sendMail() {
        mailSystem.mail()
            ...
            .send()
    }

License

The MIT License (MIT)

Copyright (c) 2015 Dmytro Lysai /[email protected]/

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Versions

Version
0.1.0
0.1.0-RC1