br.com.recursive.biblioteca.servicos.EmailService.java Source code

Java tutorial

Introduction

Here is the source code for br.com.recursive.biblioteca.servicos.EmailService.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 br.com.recursive.biblioteca.servicos;

import br.com.recursive.biblioteca.entidades.Pessoa;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

/**
 *
 * @author Claudivan Moreira
 */
public class EmailService {

    public void sendHtmlEmail(Pessoa pessoa) throws EmailException {

        Email email = new HtmlEmail();
        email.setAuthenticator(new DefaultAuthenticator("claupwd@gmail.com", "@claupwd2014"));
        email.setHostName("smtp.gmail.com");
        email.setFrom("claupwd@gmail.com");
        email.setSubject("SIB Online - Recuperao de Senha");
        email.setMsg(createMessage(pessoa));
        email.addTo(pessoa.getContato().getEmail());
        email.setSSL(true);
        //Se true, exibe na saida todo o processo do envio do email
        email.setDebug(true);
        email.send();
    }

    public String createMessage(Pessoa pessoa) {
        String html = "<div id=\"body_email\" style=\"font-size: 14px;font-family: sans-serif; text-align: center;\">"
                + "<div id=\"topo_mail\" style=\"width: 100%;background-color: #71C837;color: #ffffff; margin: 0 auto;\">"
                + "<p style=\"text-align: center; padding-bottom: 7px; padding-top: 7px;\">SIB Online | Sistema Bibliotec&aacute;rio - IFPB Campus Cajazeiras </p></div>"
                + "<div id=\"text_mail\"><br/><p>Ol " + pessoa.getNome()
                + " os dados de acesso a sua conta no SIB Online so:</p>"
                + "<b style=\"line-height: 1.5em;\">Login: " + pessoa.getMatricula() + "<br/>Senha: "
                + pessoa.getUsuario().getSenha() + "</b></div></div>";
        return html;
    }

    public static void main(String[] args) throws EmailException {

        //        Pessoa p = new Pessoa();
        //        p.setNome("sdsfsdf");
        //        
        //        Contato c = new Contato();
        //        c.setEmail("oelisiany@gmail.com");
        //        
        //        p.setContato(c);
        //        p.setUsuario(new Usuario());
        //        
        //        EmailService es = new EmailService();
        //        es.sendHtmlEmail(p);

        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("cpf", "312313123");
        map1.put("id", 1);

        for (String chave : map1.keySet()) {
            System.out.println(chave);
        }
    }

}