Java tutorial
/* * 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.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 org.apache.commons.mail.EmailException; import org.apache.commons.mail.HtmlEmail; /** * * @author matteo */ public class ConfigurazioneSMTP extends FormLayout { public ConfigurazioneSMTP() { this.setMargin(true); 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); TextField smtpPwd = new TextField("SMTP Password"); smtpPwd.setRequired(true); TextField pwdConf = new TextField("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("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())) { props.setProperty("smtp_host", smtpHost.getValue()); props.setProperty("smtp_port", smtpPort.getValue()); props.setProperty("smtp_user", smtpUser.getValue()); props.setProperty("smtp_pwd", smtpPwd.getValue()); props.setProperty("smtp_sec", 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()) { try { System.out.println("Invio mail di prova a " + emailTest.getValue()); HtmlEmail email = new HtmlEmail(); email.setHostName(props.getProperty("smtp_host")); email.setSmtpPort(Integer.parseInt(props.getProperty("smtp_port"))); email.setSSLOnConnect(Boolean.parseBoolean(props.getProperty("smtp_sec"))); email.setAuthentication(props.getProperty("smtp_user"), props.getProperty("smtp_pwd")); email.setFrom("prova@prova.it"); email.setSubject("Mail di prova"); email.addTo(emailTest.getValue()); email.setHtmlMsg("This is the message"); email.send(); } catch (EmailException ex) { Logger.getLogger(ConfigurazioneSMTP.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); } }