Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package enviando.email;
- import java.io.IOException;
- import java.io.UnsupportedEncodingException;
- import java.util.Properties;
- import javax.mail.Address;
- import javax.mail.Authenticator;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- import org.junit.Test;
- public class AppTest {
- private String username = "seu-email@gmail.com";
- private String senha = "sua-senha";
- @Test
- public void testeEmail() {
- try {
- Properties properties = new Properties();
- properties.put("mail.smtp.ssl.trust", "*");
- properties.put("mail.smtp.auth", "true");
- properties.put("mail.smtp.starttls", "true");
- properties.put("mail.smtp.host", "smtp.gmail.com");
- properties.put("mail.smtp.port", "465");
- properties.put("mail.smtp.socketFactory.port", "465");
- properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
- Session session = Session.getInstance(properties, new Authenticator() {
- @Override
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(username, senha);
- }
- });
- Address[] toUser = InternetAddress.parse("tadeujdev@gmail.com, tadeupalermoti@gmail.com");
- Message message = new MimeMessage(session);
- message.setFrom(new InternetAddress(username, "Tadeu E. Palermo"));
- message.setRecipients(Message.RecipientType.TO, toUser);
- message.setSubject("Chegou e-mail enviado pelo Java!");
- message.setText("Que bom, você recebeu um e-mail enviado pelo Java!");
- Transport.send(message);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement