Untitled

 avatar
unknown
plain_text
3 years ago
8.2 kB
3
Indexable
package com.company;


import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.swing.*;





/*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */


    /**
     * @author cano
     */
    public class panel extends javax.swing.JFrame {

        /**
         * Creates new form panel
         */
        public panel() {
            initComponents();
        }

        /**
         * This method is called from within the constructor to initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is always
         * regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {

            txtDestinatario = new javax.swing.JTextField();
            jScrollPane1 = new javax.swing.JScrollPane();
            txtMessage = new javax.swing.JTextArea();
            btn_enviarActionPerformed = new javax.swing.JButton();
            txtAsunto = new javax.swing.JTextField();

            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

            txtDestinatario.setText("jTextField1");
            txtDestinatario.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    txtDestinatarioActionPerformed(evt);
                }
            });

            txtMessage.setColumns(20);
            txtMessage.setRows(5);
            jScrollPane1.setViewportView(txtMessage);

            btn_enviarActionPerformed.setText("Enviar");
            btn_enviarActionPerformed.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    btn_enviarActionPerformedActionPerformed(evt);
                }
            });

            txtAsunto.setText("jTextField2");

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                    .addGap(156, 156, 156)
                                                    .addComponent(btn_enviarActionPerformed)
                                                    .addGap(0, 171, Short.MAX_VALUE))
                                            .addGroup(layout.createSequentialGroup()
                                                    .addContainerGap()
                                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                            .addComponent(txtAsunto, javax.swing.GroupLayout.Alignment.TRAILING)
                                                            .addComponent(txtDestinatario)
                                                            .addComponent(jScrollPane1))))
                                    .addContainerGap())
            );
            layout.setVerticalGroup(
                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                    .addContainerGap()
                                    .addComponent(txtDestinatario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(txtAsunto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(22, 22, 22)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 88, Short.MAX_VALUE)
                                    .addComponent(btn_enviarActionPerformed)
                                    .addGap(22, 22, 22))
            );

            pack();
        }

        private void btn_enviarActionPerformedActionPerformed(java.awt.event.ActionEvent evt) {
            Properties props = new Properties();
            props.setProperty("mail.smtp.auth","true");
            props.setProperty("mail.smtp.starttls.enable","true");
            props.setProperty("mail.smtp.host","smtp.gmail.com");
            props.setProperty("mail.smtp.ssl.trust","smtp.gmail.com");
            props.setProperty("mail.smtp.port","587");
    

            Session sesion = Session.getDefaultInstance(props);
            String correoENvia = "emailfalso@gmail.com";
            String contrasena = "////////";
            String destinatario = txtDestinatario.getText();
            String asunto = txtAsunto.getText();
            String msg = txtMessage.getText();

            MimeMessage mail = new MimeMessage(sesion);
            try {
                mail.setFrom(new InternetAddress(correoENvia));

                mail.addRecipient(Message.RecipientType.TO, new InternetAddress(destinatario));
                mail.setSubject(asunto);
                mail.setText(msg);

              /*  Transport trans = sesion.getTransport("smtp");
                trans.connect(correoENvia,contrasena);

                trans.sendMessage(mail, mail.getRecipients(Message.RecipientType.TO));
                trans.close();*/

                Transport tr = sesion.getTransport("smtp");
                tr.connect(correoENvia, contrasena);
                mail.saveChanges();      // don't forget this
                tr.sendMessage(mail, mail.getRecipients(Message.RecipientType.TO));
                tr.close();


                JOptionPane.showMessageDialog(null, "correo enviado");

            } catch (AddressException ex) {
                Logger.getLogger(panel.class.getName()).log(Level.SEVERE, null, ex);
            } catch (MessagingException ex) {
                Logger.getLogger(panel.class.getName()).log(Level.SEVERE, null, ex);
            }


        }

        private void txtDestinatarioActionPerformed(java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
        }

        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /* Set the Nimbus look and feel */
            /* Set the Nimbus look and feel */


            /* Create and display the form */
       java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new panel().setVisible(true);

            }
        });
        }

        // Variables declaration - do not modify
        private javax.swing.JButton btn_enviarActionPerformed;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTextField txtAsunto;
        private javax.swing.JTextField txtDestinatario;
        private javax.swing.JTextArea txtMessage;
        // End of variables declaration
    }

Editor is loading...