fr.mailjet.mail.MailjetEmail.java Source code

Java tutorial

Introduction

Here is the source code for fr.mailjet.mail.MailjetEmail.java

Source

package fr.mailjet.mail;

/*
 * 
 * Mailjet
 * %%
 * Copyright (C) 2012 Pitton Olivier - olivier dot pitton at gmail dot com
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.mail.HtmlEmail;

import fr.mailjet.context.MailjetContext;

/**
 * Un simple {@link HtmlEmail} prconfigur pour Mailjet. Par dfaut, le SSL est
 * configur.
 * 
 * @author Pitton Olivier
 * 
 */
public class MailjetEmail extends HtmlEmail {

    /**
     * Constructeur
     * 
     * @param parContext
     *          {@link MailjetContext} un contexte mailjet
     */
    public MailjetEmail(MailjetContext parContext) {
        this(parContext.getApiKey(), parContext.getSecretKey(), parContext.getSmtpPort(), parContext.isUseSSL());
    }

    /**
     * Constructeur
     * 
     * @param parApiKey
     *          {@link String} la cl publique du compte
     * @param parSecretKey
     *          {@link String} la cl prive du compte
     * @param parSmtpPort
     *          {@code int} le port SMTP
     */
    public MailjetEmail(String parApiKey, String parSecretKey, int parSmtpPort) {
        this(parApiKey, parSecretKey, parSmtpPort, true);
    }

    /**
     * Constructeur
     * 
     * @param parApiKey
     *          {@link String} la cl publique du compte
     * @param parSecretKey
     *          {@link String} la cl prive du compte
     * @param parSmtpPort
     *          {@code int} le port SMTP
     * @param parUseSSL
     *          {@code boolean} true pour activer le SSL
     */
    public MailjetEmail(String parApiKey, String parSecretKey, int parSmtpPort, boolean parUseSSL) {
        this.setAuthentication(parApiKey, parSecretKey);
        int locSmtpPort = parSmtpPort;
        this.setSmtpPort(locSmtpPort);
        this.setSSL(parUseSSL);
        if (parUseSSL) {
            this.setSslSmtpPort(Integer.toString(locSmtpPort));
        }
        this.setHostName(MailjetContext.getHostname());
    }

    /**
     * Ajoute les headers Mailjet dans le mail <b> sans supprimer les headers
     * prcdents</b>. Les potentielles anciennes valeurs associes 
     * {@link EnumMailjetMailHeader} sont crases.
     * 
     * @param parHeaders
     *          {@link Map} une map de headers
     * @see EnumMailjetMailHeader
     */
    @SuppressWarnings("unchecked")
    public void setMailjetHeaders(Map<EnumMailjetMailHeader, Object> parHeaders) {
        Map<String, String> locRealHeaders = new HashMap<String, String>(parHeaders.size());
        locRealHeaders.putAll(this.headers);
        for (Map.Entry<EnumMailjetMailHeader, Object> locEntry : parHeaders.entrySet()) {
            locRealHeaders.put(locEntry.getKey().getConstName(), locEntry.getValue().toString());
        }
        this.setHeaders(locRealHeaders);
    }
}