Advertisement
tadeuespindola

GabrielEmailAppTest

Mar 17th, 2021
1,616
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.79 KB | None | 0 0
  1. package enviando.email;
  2.  
  3. import java.io.IOException;
  4. import java.io.UnsupportedEncodingException;
  5. import java.util.Properties;
  6.  
  7. import javax.mail.Address;
  8. import javax.mail.Authenticator;
  9. import javax.mail.Message;
  10. import javax.mail.MessagingException;
  11. import javax.mail.PasswordAuthentication;
  12. import javax.mail.Session;
  13. import javax.mail.Transport;
  14. import javax.mail.internet.InternetAddress;
  15. import javax.mail.internet.MimeMessage;
  16.  
  17. import org.junit.Test;
  18.  
  19. public class AppTest {
  20.    
  21.     private String username = "seu-email@gmail.com";
  22.     private String senha = "sua-senha";
  23.    
  24.     @Test
  25.     public void testeEmail() {
  26.         try {
  27.             Properties properties = new Properties();
  28.             properties.put("mail.smtp.ssl.trust", "*");
  29.             properties.put("mail.smtp.auth", "true");
  30.             properties.put("mail.smtp.starttls", "true");
  31.             properties.put("mail.smtp.host", "smtp.gmail.com");
  32.             properties.put("mail.smtp.port", "465");
  33.             properties.put("mail.smtp.socketFactory.port", "465");
  34.             properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  35.            
  36.             Session session = Session.getInstance(properties, new Authenticator() {
  37.                 @Override
  38.                 protected PasswordAuthentication getPasswordAuthentication() {
  39.                     return new PasswordAuthentication(username, senha);
  40.                 }
  41.             });
  42.            
  43.             Address[] toUser = InternetAddress.parse("tadeujdev@gmail.com, tadeupalermoti@gmail.com");
  44.             Message message = new MimeMessage(session);
  45.             message.setFrom(new InternetAddress(username, "Tadeu E. Palermo"));
  46.             message.setRecipients(Message.RecipientType.TO, toUser);
  47.             message.setSubject("Chegou e-mail enviado pelo Java!");
  48.             message.setText("Que bom, você recebeu um e-mail enviado pelo Java!");
  49.             Transport.send(message);           
  50.         } catch (Exception e) {
  51.             e.printStackTrace();
  52.         }      
  53.     }  
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement