Anda di halaman 1dari 7

package EnviarEmail; import java.util.Date; import javax.mail.Message; import javax.mail.Session; import java.util.Properties; import javax.mail.Multipart; import javax.mail.

Transport; import javax.swing.JOptionPane; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMultipart; import javax.mail.internet.InternetAddress; import javax.mail.internet.AddressException; /** * @web http://programaciongeneral.tk * @author Jheyson Matta */ public class JheysonMatta_Mail { private String from = "";//tu_correo@gmail.com private String password = "";//tu password: 123456 :) // destinatario1@hotmail.com,destinatario2@hotmail.com, destinatario_n@hotma il.com private InternetAddress[] addressTo; private String Subject = "";//titulo del mensaje private String MessageMail = "";//contenido del mensaje public JheysonMatta_Mail(){} public void SEND() { try { Properties props = new Properties(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.user", "usuario"); props.put("mail.smtp.port", 25); // SMTPAutenticar auth = new SMTPAutenticar( getFrom(), getPassword() ) ; Session session = Session.getDefaultInstance(props, auth); session.setDebug(false); //Se crea destino y origen del mensaje MimeMessage mimemessage = new MimeMessage(session); InternetAddress addressFrom = new InternetAddress( getFrom() ); mimemessage.setFrom(addressFrom); mimemessage.setRecipients(Message.RecipientType.TO, addressTo); mimemessage.setSubject( getSubject() ); // Se crea el contenido del mensaje MimeBodyPart mimebodypart = new MimeBodyPart(); mimebodypart.setText( getMessage() ); mimebodypart.setContent( getMessage() , "text/html"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(mimebodypart); mimemessage.setContent(multipart); mimemessage.setSentDate(new Date()); Transport.send(mimemessage); JOptionPane.showMessageDialog(null, "Correo enviado. Exitosamente!!!

"); } catch (MessagingException ex) { System.out.println(ex); } } //remitente public void setFrom(String mail){ this.from = mail; } public String getFrom(){ return this.from; } //Contrasea public void setPassword(char[] value){ this.password = new String(value); } public String getPassword(){ return this.password; } //destinatarios public void setTo(String mails){ String[] tmp =mails.split(","); addressTo = new InternetAddress[tmp.length]; for (int i = 0; i < tmp.length; i++) { try { addressTo[i] = new InternetAddress(tmp[i]); } catch (AddressException ex) { System.out.println(ex); } } } public InternetAddress[] getTo(){ return this.addressTo; } //titulo correo public void setSubject(String value){ this.Subject = value; } public String getSubject(){ return this.Subject; } //contenido del mensaje public void setMessage(String value){ this.MessageMail = value; } public String getMessage(){ return this.MessageMail; } }

----------------------------------------------------------------------------------------------------------

package EnviarEmail; /** * @web http://programaciongeneral.tk * @author Jheyson Matta */ public class Main { public static void main(String[] args) { new interfaz().setVisible(true); } }

----------------------------------------------------------------------------------------------------------

package EnviarEmail; import javax.mail.Authenticator; import javax.mail.PasswordAuthentication; /** * @web http://programaciongeneral.tk * @author Jheyson Matta */ public class SMTPAutenticar extends Authenticator{ private String SMTP_AUTH_USER = ""; private String SMTP_AUTH_PWD = ""; public SMTPAutenticar() {} public SMTPAutenticar(String user , String pass) { this.SMTP_AUTH_USER = user; this.SMTP_AUTH_PWD = pass; } @Override public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(this.SMTP_AUTH_USER, this.SMTP_AUTH_PWD); } }

----------------------------------------------------------------------------------------------------------

package EnviarEmail; /** * @web http://programaciongeneral.tk * @author Jheyson Matta */ public class interfaz extends javax.swing.JFrame { JheysonMatta_Mail mail = new JheysonMatta_Mail(); /** Creates new form interfaz */ public interfaz() { initComponents(); this.setTitle("Jheyson Matta - Enviar Email por Java"); } /** 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() { java.awt.GridBagConstraints gridBagConstraints; jPanel1 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); txtFROM = new javax.swing.JTextField(); txtTO = new javax.swing.JTextField(); txtSUBJECT = new javax.swing.JTextField(); jLabel4 = new javax.swing.JLabel(); txtPWD = new javax.swing.JPasswordField(); jPanel2 = new javax.swing.JPanel(); jScrollPane1 = new javax.swing.JScrollPane(); txtMESSAGE = new javax.swing.JTextArea(); cmdSEND = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setBackground(new java.awt.Color(204, 204, 204)); jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder()); jPanel1.setLayout(new java.awt.GridBagLayout()); jLabel1.setText("De :"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 0; gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); jPanel1.add(jLabel1, gridBagConstraints); jLabel2.setText("Para :"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); jPanel1.add(jLabel2, gridBagConstraints); jLabel3.setText("Asunto :"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 2; gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5); jPanel1.add(jLabel3, gridBagConstraints); txtFROM.setPreferredSize(new java.awt.Dimension(160, 20)); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 0; gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 10); jPanel1.add(txtFROM, gridBagConstraints); txtTO.setPreferredSize(new java.awt.Dimension(390, 20));

gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 1; gridBagConstraints.gridwidth = 3; gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 5); jPanel1.add(txtTO, gridBagConstraints); txtSUBJECT.setPreferredSize(new java.awt.Dimension(390, 20)); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 2; gridBagConstraints.gridwidth = 3; gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 5); jPanel1.add(txtSUBJECT, gridBagConstraints); jLabel4.setText("Contrasea :"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 2; gridBagConstraints.gridy = 0; gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 5); jPanel1.add(jLabel4, gridBagConstraints); txtPWD.setPreferredSize(new java.awt.Dimension(160, 20)); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 3; gridBagConstraints.gridy = 0; gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; gridBagConstraints.insets = new java.awt.Insets(5, 0, 5, 5); jPanel1.add(txtPWD, gridBagConstraints); jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder()); txtMESSAGE.setColumns(20); txtMESSAGE.setRows(5); jScrollPane1.setViewportView(txtMESSAGE); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPan el2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment. LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addContainerGap() .addComponent(jScrollPane1) .addContainerGap()) ); jPanel2Layout.setVerticalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment. LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addContainerGap() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SI ZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE ) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX _VALUE))

); cmdSEND.setText("Enviar"); cmdSEND.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cmdSENDActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentP ane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING ) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Ali gnment.LEADING) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZ E, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout .createSequentialGroup() .addGap(0, 0, Short.MAX_VALUE) .addComponent(cmdSEND))) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING ) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, j avax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELA TED) .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, j avax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRE LATED) .addComponent(cmdSEND) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX _VALUE)) ); pack(); }// </editor-fold> private void cmdSENDActionPerformed(java.awt.event.ActionEvent evt) { mail.setFrom( this.txtFROM.getText() ); mail.setPassword( this.txtPWD.getPassword() ); mail.setTo( this.txtTO.getText() ); mail.setSubject( this.txtSUBJECT.getText() ); mail.setMessage( this.txtMESSAGE.getText() ); mail.SEND(); }

/** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new interfaz().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton cmdSEND; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextField txtFROM; private javax.swing.JTextArea txtMESSAGE; private javax.swing.JPasswordField txtPWD; private javax.swing.JTextField txtSUBJECT; private javax.swing.JTextField txtTO; // End of variables declaration }