Sie sind auf Seite 1von 3

JavaMail es una expansin de Java que facilita el envo y recepcin de e-mail desde cdigo java.

JavaMail implementa el protocolo SMTP (Simple Mail Transfer Protocol) as como los distintos tipos de conexin con servidores de correo -TLS, SSL, autentificacin con usuario y password, etc [Segn SantaWikipedia] Qu necesitamos? JavaMail 1.4.5 Java y Netbeans 6.9 o superior Una cuenta gmail y otra en hotmail Nivel: Bsico/Intermedio Tiempo: 15 minutos 1. Crea un proyecto en Netbeans (proyecto nombre: JMail ), aade una JFrame (nombre:interfaz.java), ademas crea dos clases (JCMail.java y SMTPAuthenticator.java), la interfaz del proyecto debe tener la siguiente forma:

OJO con los nombres. 2. Agrega las libreras de JavaMail al proyecto. 3. La clase SMTPAuthenticator.java, tiene el siguiente cdigo: package jmail; import javax.mail.Authenticator; import javax.mail.PasswordAuthentication; /** * @web http://www.jc-mouse.net * @author Mouse */ public class SMTPAuthenticator extends Authenticator{ private String SMTP_AUTH_USER = ""; private String SMTP_AUTH_PWD = ""; public SMTPAuthenticator() {} public SMTPAuthenticator(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); } } 4. La clase JCMail.java, encargada del envo del mensaje tiene por cdigo: package jmail; 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://www.jc-mouse.net * @author Mouse */ public class JCMail { private String from = "";//tu_correo@gmail.com private String password = "";//tu password: 123456 // destinatario1@hotmail.com,destinatario2@hotmail.com, destinatario_n@hotmail.com private InternetAddress[] addressTo; private String Subject = "";//titulo del mensaje private String MessageMail = "";//contenido del mensaje public JCMail(){} 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); // SMTPAuthenticator auth = new SMTPAuthenticator( 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. Enjoy!!!"); } 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; } } 5. Para terminar, implementar la clase JCMail en la interfaz. JCMail mail = new JCMail(); 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(); } Ejecuta y manda el mensaje, texto plano o etiquetas HTML, tambien para enviar a varios destinatarios, escribe los correos separados por una coma.

Proyecto Netbeans

Das könnte Ihnen auch gefallen