com.cerebro.provevaadin.smtp.ConfigurazioneSMTPSpring.java Source code

Java tutorial

Introduction

Here is the source code for com.cerebro.provevaadin.smtp.ConfigurazioneSMTPSpring.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.provevaadin.smtp;

import com.vaadin.data.validator.EmailValidator;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Button;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.Notification;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

/**
 *
 * @author matteo
 */
public class ConfigurazioneSMTPSpring extends FormLayout {

    public ConfigurazioneSMTPSpring() {

        TextField smtpHost = new TextField("SMTP Host Server");
        smtpHost.setRequired(true);
        TextField smtpPort = new TextField("SMTP Port");
        smtpPort.setRequired(true);
        TextField smtpUser = new TextField("SMTP Username");
        smtpUser.setRequired(true);
        PasswordField smtpPwd = new PasswordField("SMTP Password");
        smtpPwd.setRequired(true);
        PasswordField pwdConf = new PasswordField("Conferma la Password");
        pwdConf.setRequired(true);
        CheckBox security = new CheckBox("Sicurezza del server");

        Properties props = new Properties();
        InputStream config = VaadinServlet.getCurrent().getServletContext()
                .getResourceAsStream("/WEB-INF/config.properties");
        if (config != null) {
            System.out.println("Carico file di configurazione");
            try {
                props.load(config);
            } catch (IOException ex) {
                Logger.getLogger(ConfigurazioneSMTP.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
        smtpHost.setValue(props.getProperty("mail.smtp.host"));
        smtpUser.setValue(props.getProperty("smtp_user"));
        security.setValue(Boolean.parseBoolean(props.getProperty("smtp_sec")));

        Button salva = new Button("Salva i parametri");
        salva.addClickListener((Button.ClickEvent event) -> {
            System.out.println("Salvo i parametri SMTP");
            if (smtpHost.isValid() && smtpPort.isValid() && smtpUser.isValid() && smtpPwd.isValid()
                    && smtpPwd.getValue().equals(pwdConf.getValue())) {
                System.out.println(smtpHost.getValue() + smtpPort.getValue() + smtpUser.getValue()
                        + smtpPwd.getValue() + security.getValue().toString());
                props.setProperty("mail.smtp.host", smtpHost.getValue());
                props.setProperty("mail.smtp.port", smtpPort.getValue());
                props.setProperty("smtp_user", smtpUser.getValue());
                props.setProperty("smtp_pwd", smtpPwd.getValue());
                props.setProperty("mail.smtp.ssl.enable", security.getValue().toString());
                String webInfPath = VaadinServlet.getCurrent().getServletConfig().getServletContext()
                        .getRealPath("WEB-INF");
                File f = new File(webInfPath + "/config.properties");
                try {
                    OutputStream o = new FileOutputStream(f);
                    try {
                        props.store(o, "Prova");
                    } catch (IOException ex) {
                        Logger.getLogger(ConfigurazioneSMTP.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(ConfigurazioneSMTP.class.getName()).log(Level.SEVERE, null, ex);
                }
                Notification.show("Parametri salvati");
            } else {
                Notification.show("Ricontrolla i parametri");
            }

        });

        TextField emailTest = new TextField("Destinatario Mail di Prova");
        emailTest.setImmediate(true);
        emailTest.addValidator(new EmailValidator("Mail non valida"));

        Button test = new Button("Invia una mail di prova");
        test.addClickListener((Button.ClickEvent event) -> {
            System.out.println("Invio della mail di prova");
            if (emailTest.isValid() && !emailTest.isEmpty()) {
                System.out.println("Invio mail di prova a " + emailTest.getValue());
                JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
                mailSender.setJavaMailProperties(props);
                mailSender.setUsername(props.getProperty("smtp_user"));
                mailSender.setPassword(props.getProperty("smtp_pwd"));
                MimeMessage message = mailSender.createMimeMessage();
                MimeMessageHelper helper = new MimeMessageHelper(message);
                try {
                    helper.setFrom("dottmatteocasagrande@gmail.com");
                    helper.setSubject("Subject");
                    helper.setText("It works!");
                    helper.addTo(emailTest.getValue());
                    mailSender.send(message);
                } catch (MessagingException ex) {
                    Logger.getLogger(ConfigurazioneSMTPSpring.class.getName()).log(Level.SEVERE, null, ex);
                }

            } else {
                Notification.show("Controlla l'indirizzo mail del destinatario");
            }
        });

        this.addComponents(smtpHost, smtpPort, smtpUser, smtpPwd, pwdConf, security, salva, emailTest, test);

    }

}