
JavaMail API é uma biblioteca que permite de forma fácil realizar envios de e-mails. O respositório maven onde se encontra a biblioteca é esse aqui: http://mvnrepository.com/artifact/javax.mail/mail/1.5.0-b01
Passo-a-passo
Dentro dessa API há a classe java.util.Properties onde iremos informar as configurações necessários ao protocolo SMTP como no exemplo abaixo.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Properties props = new Properties(); | |
props.put("mail.smtp.host", "smtp.gmail.com"); | |
props.put("mail.smtp.socketFactory.port", "465"); | |
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); | |
props.put("mail.smtp.auth", "true"); | |
props.put("mail.smtp.port", "465"); |
Com essas configurações em mãos, podemos criar uma sessão(classe javax.mail.Session) contendo a autenticação (javax.mail.Authenticator e javax.mail.PasswordAuthentication) da conta de email que será usado para disparar os emails.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Session session = Session.getInstance(props, new Authenticator() { | |
protected PasswordAuthentication getPasswordAuthentication() { | |
return new PasswordAuthentication("emailrementente@servidor.com", "senha"); | |
} | |
}); |
Autenticado, utilizaremos essa sessão para criar uma mensagem de email através das classes javax.mail.internet.MimeMessage, javax.mail.internet.InternetAddress, javax.mail.Address e javax.mail.Message como demonstrado abaixo:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MimeMessage message = new MimeMessage (session); | |
message.setFrom(new InternetAddress("emailrementente@servidor.com")); | |
Address[] toUser = InternetAddress.parse("emaildestinatario@servidor.com"); | |
message.setRecipients(Message.RecipientType.TO, toUser); | |
message.setSubject("Assunto"); | |
message.setText("Texto", "utf-8", "html"); |
Feito isso, podemos realizar o envio através da classe javax.mail.Transport:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Transport.send(message); |
Exemplo completo
SendEmail.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SendEmail { | |
public static void main(String[] args) { | |
Properties props = new Properties(); | |
props.put("mail.smtp.host", "smtp.gmail.com"); | |
props.put("mail.smtp.socketFactory.port", "465"); | |
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); | |
props.put("mail.smtp.auth", "true"); | |
props.put("mail.smtp.port", "465"); | |
Session session = Session.getDefaultInstance(props, new Authenticator() { | |
protected PasswordAuthentication getPasswordAuthentication() { | |
return new PasswordAuthentication("emailrementente@servidor.com", "senha"); | |
} | |
}); | |
MimeMessage message = new MimeMessage (session); | |
message.setFrom(new InternetAddress("emailrementente@servidor.com")); | |
Address[] toUser = InternetAddress | |
.parse("emaildestinatario@servidor.com"); | |
message.setRecipients(Message.RecipientType.TO, toUser); | |
message.setSubject("Assunto"); | |
message.setText("Texto", "utf-8", "html"); | |
Transport.send(message); | |
} | |
} |