Advertisement
tadeuespindola

GabrielEmailApp

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