Package br.com.future.model

Source Code of br.com.future.model.EnviaEmail

package br.com.future.model;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
* CLASSE PARA ENVIAR O EMAIL NO GMAIL.
*
* @author future
*
*/
public class EnviaEmail {
   
    public static void main(String[] args) {
        new EnviaEmail().enviar();
    }
   
    //INFORMA��ES REFERENTE A COBRAN�A   

   
    //INFORMA��ES REFERENTE AO EMAIL
    private String emailRemetente = "adercioreinan@gmail.com";
    private String senhaRemetente = "aurenaide";
   
    private String emailDestinatario = "adercioreinan@yahoo.com.br";
    private String assunto = "Processo";
    private String mensagem = "Sobre o Processo";

    public EnviaEmail(){
       
    }

    public void enviar() {
        try {

            //CONFIG. DO GMAIL
            Properties mailProps = new Properties();
            mailProps.put("mail.transport.protocol", "smtp");
            mailProps.put("mail.smtp.starttls.enable","true");
            mailProps.put("mail.smtp.host", "smtp.gmail.com");
            mailProps.put("mail.smtp.auth", "true");
            mailProps.put("mail.smtp.user", emailRemetente);
            mailProps.put("mail.debug", "true");
            mailProps.put("mail.smtp.port", "465");
            mailProps.put("mail.smtp.socketFactory.port", "465");
            mailProps.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            mailProps.put("mail.smtp.socketFactory.fallback", "false");

            //EH NECESSARIO AUTENTICAR
            Session mailSession = Session.getInstance(mailProps, new Authenticator() {                 
                public PasswordAuthentication getPasswordAuthentication(){             
                    return new PasswordAuthentication(emailRemetente, senhaRemetente);
                }
            });
            mailSession.setDebug(false);

            //CONFIG. DA MENSAGEM
            Message mailMessage = new MimeMessage(mailSession);

            //REMETENTE
            mailMessage.setFrom(new InternetAddress(emailRemetente));

            //DESTINATARIO
            mailMessage.setRecipients(Message.RecipientType.TO, InternetAddress.parse(emailDestinatario));       

            //MENSAGEM QUE VAI NO CORPO DO EMAIL
            MimeBodyPart mbpMensagem = new MimeBodyPart();
            mbpMensagem.setText(mensagem);

            //PARTES DO EMAIL
            Multipart mp = new MimeMultipart();
            mp.addBodyPart(mbpMensagem);
           

            //ASSUNTO DO EMAIL
            mailMessage.setSubject(assunto);
           
            //SELECIONA O CONTEUDO
            mailMessage.setContent(mp);

            //ENVIA O EMAIL
            Transport.send(mailMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
TOP

Related Classes of br.com.future.model.EnviaEmail

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.