quinta-feira, 7 de fevereiro de 2013

exemplo envio de email utilizando javamail

Arquivo com as propriedades de Email (mail.properties)
#Configuracao de Email
mail.transport.protocol=smtp
mail.debug=true
#
mail.smtp.auth=true
mail.smtp.host=smtp.gmail.com
mail.smtp.port=25
#
mail.smtp.from={usuario}@gmail.com
mail.smtp.user={usuario}
mail.smtp.password={senha}
Classe MailAutenticator que estende java.mail.Authenticator e precisa ser utilizada caso o servidor smtp exiga autenticação para o envio de emails.
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

/**
 * Classe de encapsulamento de informações para uso durante o processo de
 * autenticação no servidor de e-mail.
 */
public class MailAuthenticator extends Authenticator {

 private String username, password;

 public MailAuthenticator(String user, String pwd) {
  username = user;
  password = pwd;
 }

 /**
  * Retorna um elemento de autenticação para e-mail
  * a partir de usuário e senha pré-configurados
  */
 @Override
 public PasswordAuthentication getPasswordAuthentication() {
  return new PasswordAuthentication(username, password);
 }

}


Classe MailSender que é  responsável por estabelecer comunicação com o servidor e enviar os email.
import java.io.FileInputStream;
import java.util.Properties;

import javax.activation.CommandMap;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.activation.MailcapCommandMap;
import javax.mail.Message;
import javax.mail.Multipart;
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;

import android.util.Log;

public class MailSender {

 private static final String TAG = MailSender.class.getName();
 
 public MailSender() {
  
  MailcapCommandMap mc = (MailcapCommandMap)CommandMap.getDefaultCommandMap();
  mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
  mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
  mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
  mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
  mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
  CommandMap.setDefaultCommandMap(mc);
 }

 /**
  * 
  * @param from
  * @param to
  * @param subject
  * @param message
  * @param file
  * @throws Exception
  */
 public boolean sendMail(String from, String[] to, String subject, String message) throws Exception {
  return sendMail(from, to, subject, message, new String[0]);
 }
 
 /**
  * 
  * @param from
  * @param to
  * @param subject
  * @param message
  * @param file
  * @throws Exception
  */
 public boolean sendMail(String from, String[] to, String subject, String message, String file) throws Exception {
  return sendMail(from, to, subject, message, new String[] { file });
 }

 /**
  * 
  * @param from
  * @param to
  * @param subject
  * @param message
  * @param files
  * @return 
  * @throws Exception
  */
 public boolean sendMail(String from, String[] to, String subject, String message, String[] files) throws Exception {

  Properties properties = new Properties();
  properties.load(new FileInputStream("mail.properties"));

  String user = properties.getProperty("mail.smtp.user");
  String pass = properties.getProperty("mail.smtp.password");

  Session session = Session.getInstance(properties, new MailAuthenticator(user, pass));
  session.setDebug(true);

  Message msg = new MimeMessage(session);

  try {

   // Setando o destinatário
   msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to[0]));
   for (int count = 1; count < to.length; count++) {
    msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to[count]));
   }   
   
   // Setando a origem do email
   if (from == null || from.isEmpty()) {
    from = properties.getProperty("mail.smtp.from");
   }
   //
   msg.setFrom(new InternetAddress(from));

   // Setando o assunto
   msg.setSubject(subject);

   if (files == null || files.length == 0) {

    /* Setando o content/type do email */
    msg.setContent(message, "text/html");

   } else {

    // Cria a primeira parte da mensagem
    MimeBodyPart mineBodyPart = new MimeBodyPart();
    mineBodyPart.setContent(message, "text/html");

    // Cria a Multipart
    Multipart multiPart = new MimeMultipart();
    multiPart.addBodyPart(mineBodyPart);

    for (String file : files) {

     // Cria partes dos arquivos em anexo
     MimeBodyPart mineBodyPartFiles = new MimeBodyPart();

     // Anexa o arquivo na mensagem
     FileDataSource fileDataSource = new FileDataSource(file);
     mineBodyPartFiles.setDataHandler(new DataHandler(fileDataSource));
     mineBodyPartFiles.setFileName(fileDataSource.getName());
     multiPart.addBodyPart(mineBodyPartFiles);
    }

    // adiciona a Multipart na mensagem
    msg.setContent(multiPart);
   }

  } catch (Exception e) {

   Log.e(TAG, e.getLocalizedMessage());
   
   throw new Exception(e.getLocalizedMessage(), e);
  }

  try {
   
   Transport transport = session.getTransport();
   transport.connect();

   transport.sendMessage(msg, msg.getAllRecipients());
   transport.close();
   
   return true;
   
  } catch (Exception e) {

   Log.e(TAG, e.getLocalizedMessage());
  }
  
  return false;
 }
}

Nenhum comentário:

Postar um comentário