Código Fuente:
Clase sencilla para el envío de emails con formato texto y HTML , y tomando el contenido desde archivos. Ejemplo de uso: EmailSender es = new EmailSender();
es.setSmtpHost("mail.smtp.com");
es.setFromEmail(" java@cricava.com ");
es.setToEmail(" alguien@email.com ");
es.setSubject("Prueba de email");
es.setTextFile("mi/path/archivo.txt");
es.setHtmlFile("mi/path/archivo.html");
es.send();
Código fuente completo de la clase:
import java.util.*;
import javax.mail.*;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.File;
import java.io.IOException;
import javax.mail.internet.*;
import javax.activation.*;
public class EmailSender {
private String smtpHost;
private String fromEmail;
private String toEmail;
private String subject;
private String textFile;
private String htmlFile;
public EmailSender() {
}
public void setSmtpHost(String smtpHost) {
this.smtpHost = smtpHost;
}
public void getSmtpHost() {
return this.smtpHost;
}
public void setFromEmail(String fromEmail) {
this.fromEmail = fromEmail;
}
public void getFromEmail() {
return this.fromEmail;
}
public void setToEmail(String toEmail) {
this.toEmail = toEmail;
}
public void getToEmail() {
return this.toEmail;
}
public void setSubject(String subject) {
this.subject = subject;
}
public void getSubject() {
return this.subject;
}
public void setTextFile(String textFile) {
this.textFile = textFile;
}
public void getTextFile() {
return this.textFile;
}
public void setHtmlFile(String htmlFile) {
this.htmlFile = htmlFile;
}
public void getHtmlFile() {
return this.htmlFile;
}
public void send() {
try {
Properties properties = System.getProperties();
properties.put("mail.transport.protocol", "SMTP");
properties.put("mail.smtp.host", this.smtpHost);
Session session = Session.getInstance(properties, null);
MimeMessage msg = new MimeMessage(session);
InternetAddress from = new InternetAddress(this.fromEmail);
InternetAddress to =new InternetAddress(this.toEmail);
msg.setFrom(from);
msg.addRecipient(Message.RecipientType.TO, to);
msg.setSubject(this.subject);
Multipart mp = new MimeMultipart("alternative");
BodyPart bp1 = getFileBodyPart(this.textFile, "text/plain");
mp.addBodyPart(bp1);
BodyPart bp2 = getFileBodyPart(this.htmlFile, "text/html");
mp.addBodyPart(bp2);
msg.setContent(mp);
Transport.send(msg);
} catch (Exception e) {
System.err.println(e);
}
}
private BodyPart getFileBodyPart(String filename, String contentType)
throws javax.mail.MessagingException {
BodyPart bp = new MimeBodyPart();
bp.setDataHandler(new DataHandler(new FileDataSource(filename)));
return bp;
}
}
.
Usuarios que han visto este tema también han visto...
- Obtener información de clases Java en aplicaciones web
- Tomcat, Java y PostgreSQL
- Creación de nuestro propio servidor web
- Conexión a una base de datos en Java
- Trabajando con SocketChannels en J2SDK 1.4 en Java
Información legal | Política de Privacidad | Contacte con nosotros
Otro proyecto de Factoría de Internet. Copyright© 2003-2011 Factoría de Internet S.L.. Todos los derechos reservados.