Artículo
Fecha: 2010-03-04 12:07:14
Example of Sending an e-mail with attachment in java using JavaMail
package test.utils;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Send E-mails
*
*
* @author jmbataller
* @version 1.0
*/
public class Mail {
private static Log logger = LogFactory.getLog(Mail.class);
/** Multipart */
private Multipart mp;
/** MimeMessage object */
MimeMessage message;
/**
* Default constructor - Creates basic e-mail
* @param to
* @param from
* @param host
* @param subject
* @param text
* @throws MessagingException
* @throws AddressException
*/
public Mail(String host) throws AddressException, MessagingException
{
Properties mailProperties = System.getProperties();
mp = new MimeMultipart();
// Setup mail server
mailProperties.setProperty("mail.smtp.host", host);
// Get the default Session object.
Session session = Session.getDefaultInstance(mailProperties);
// Create a default MimeMessage object.
message = new MimeMessage(session);
}
/**
* Creates e-mail
* @param host
* @param from
* @param to
* @param subject
* @param body
* @throws AddressException
* @throws MessagingException
*/
public Mail(String host, String from, String to, String subject, String body) throws AddressException, MessagingException
{
Mail m = new Mail(host);
m.setFrom(from);
m.setTo(to);
m.setSubject(subject);
m.addBodyPart(body);
}
/**
* Sets sender e-mail address
* @param from
* @throws AddressException
* @throws MessagingException
*/
public void setFrom(String from) throws AddressException, MessagingException
{
message.setFrom(new InternetAddress(from));
logger.info("Sender added");
}
/**
* Sets recipient e-mail address
* @throws AddressException
* @throws MessagingException
*/
public void setTo(String to) throws AddressException, MessagingException
{
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
logger.info("Recipient added");
}
/**
* Adds recipient e-mail address
* @param recipient
* @throws AddressException
* @throws MessagingException
*/
public void addRecipient(String recipient) throws AddressException, MessagingException
{
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
logger.info("Recipient added");
}
/**
* Sets subject
* @param subject
* @throws MessagingException
*/
public void setSubject(String subject) throws MessagingException
{
message.setSubject(subject);
logger.info("Subject added");
}
/**
* Sends e-mail
* @throws MessagingException
* @throws AddressException
*/
public void sendMail() throws MessagingException {
// Now set the actual message
//message.setText("message");
// add the Multipart to the message
message.setContent(mp);
// Send message
Transport.send(message);
logger.info("Sent message successfully.");
}
/**
* Adds body part
* @param text
* @throws MessagingException
*/
public void addBodyPart(String text) throws MessagingException
{
MimeBodyPart bodyPart = new MimeBodyPart();
bodyPart.setText(text);
mp.addBodyPart(bodyPart);
logger.info("Body part added");
}
/**
* Adds an attachment to the email
* @param file
* @throws MessagingException
*/
public void addAttachment(String file) throws MessagingException
{
// create a new message part
MimeBodyPart msgPart = new MimeBodyPart();
// attach the file to the message
FileDataSource fds = new FileDataSource(file);
msgPart.setDataHandler(new DataHandler(fds));
msgPart.setFileName(fds.getName());
// Adds new message part
mp.addBodyPart(msgPart);
logger.info("File " + file + " attached.");
}
public static void main(String[] args)
{
String to = "xxx@mail.com";
String from = "no-reply@mail.com";
String host = "smtp.mail.com";
String subject = "email subject";
String body = "text in the body of the email";
try
{
Mail m = new Mail(host);
m.setFrom(from);
m.setTo(to);
m.setSubject(subject);
m.addBodyPart(body);
m.addBodyPart("nnFile: ");
m.addAttachment("file1.pdf");
m.sendMail();
}
catch(Exception e)
{
logger.error(e.getMessage());
e.printStackTrace();
}
}
}
|
Autor:Jose Miguel Bataller
Buscador
Últimas entradas
Preseleccionar un option de un element select html con jQuery »
Testing for Empty elements in XSL »
Java Keytool Commands for Managing security certificates »
Execute servlet filter before calling Axis web service »
Set trustStore for SSL connection in Java »
Example of a client-wsdd.config »
Convert Axis Java Object to XML »