com.cerebro.gorgone.commons.SendConfEmail.java Source code

Java tutorial

Introduction

Here is the source code for com.cerebro.gorgone.commons.SendConfEmail.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cerebro.gorgone.commons;

import com.cerebro.gorgone.MyUI;
import static com.cerebro.gorgone.MyUI.logger;
import com.cerebro.gorgone.costants.ConfigProperties;
import com.vaadin.external.org.slf4j.Logger;
import com.vaadin.server.VaadinServlet;
import java.io.InputStream;
import java.util.Properties;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

/**
 *
 * @author matteo
 */
public class SendConfEmail {

    Logger logger = MyUI.logger;

    private String smtp_host;
    private int smtp_port;
    private String smtp_user;
    private String smtp_pwd;
    private boolean smtp_security;

    public SendConfEmail(String address, String confCode) {

        logger.info("Invio della mail di conferma");
        // Leggo i parametri di invio
        Properties props = new Properties();
        InputStream config = VaadinServlet.getCurrent().getServletContext()
                .getResourceAsStream("/WEB-INF/config.properties");
        if (config != null) {
            try {
                logger.info("Carico il file .properties");
                props.load(config);
            } catch (Exception ex) {
                logger.error("Errore nel caricamento del file .properties: " + ex.getMessage());
            }
        }
        smtp_host = props.getProperty(ConfigProperties.SMTP_HOST);
        smtp_port = Integer.getInteger(props.getProperty(ConfigProperties.SMTP_PORT));
        smtp_user = props.getProperty(ConfigProperties.SMTP_USER);
        smtp_pwd = props.getProperty(ConfigProperties.SMTP_PWD);
        smtp_security = Boolean.parseBoolean(ConfigProperties.SMTP_SECURITY);
        // Creo la mail
        HtmlEmail email = new HtmlEmail();
        try {
            email.setHostName(smtp_host);
            email.setSmtpPort(smtp_port);
            email.setSSLOnConnect(smtp_security);
            email.setAuthentication(smtp_user, smtp_pwd);
            email.setFrom("gioco@gioco.com", "Gioco");
            email.setSubject("Conferma il tuo indirizzo email " + confCode);
            email.addTo(address);
            email.setMsg("Messaggio della mail");
            email.send();
            logger.info("Email inviata");
        } catch (EmailException ex) {
            logger.error("Errore nell'invio della mail");
            logger.error(ex.getMessage());
        }
    }

}