Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package br.com.lima;
- import java.io.File;
- import java.util.Date;
- import java.util.List;
- import java.util.Properties;
- import javax.activation.DataHandler;
- import javax.activation.FileDataSource;
- import javax.mail.Authenticator;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.Multipart;
- import javax.mail.Part;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeBodyPart;
- import javax.mail.internet.MimeMessage;
- import javax.mail.internet.MimeMultipart;
- /**
- * class that encapsulates the sending of messages through the javax.mail.
- *
- * @author Mauricio Lima
- * @date 18/07/2011
- */
- public class SendMail {
- private Session session;
- private String user;
- private String content = "text/html";
- public SendMail(boolean requiresAuthentication,
- boolean requiresSSLConection, String host, String port,
- final String user, final String password) {
- super();
- this.user = user;
- Properties props = new Properties();
- props.put("mail.smtp.auth", requiresAuthentication + "");
- props.put("mail.smtp.port", port);
- props.put("mail.host", host);
- if (requiresSSLConection) {
- props.put("mail.smtp.socketFactory.class",
- "javax.net.ssl.SSLSocketFactory");
- props.put("mail.smtp.socketFactory.fallback", "false");
- }
- Authenticator authenticator = new Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(user, password);
- }
- };
- this.session = Session.getInstance(props, authenticator);
- }
- /**
- * Sends a simple e-mail
- * @param subject
- * @param body
- * @param recipientTO
- * @throws MessagingException
- */
- public void sendMail(String subject, String body, String recipientTO) throws MessagingException{
- sendMail(subject, body, new String[]{ recipientTO }, null, null, null, this.content);
- }
- public void sendMail(String subject, String body, String recipientTO, File attachment) throws MessagingException{
- if(attachment != null){
- sendMail(subject, body, new String[]{ recipientTO }, null, null, new File[]{ attachment }, this.content);
- } else {
- sendMail(subject, body, new String[]{ recipientTO }, null, null, null, this.content);
- }
- }
- public void sendMail(String subject, String body, String recipientTO, File attachment, String[] recipientsBCC) throws MessagingException{
- if(attachment != null){
- sendMail(subject, body, new String[]{ recipientTO }, null, null, new File[] {attachment}, this.content);
- } else {
- sendMail(subject, body, new String[]{ recipientTO }, null, null, null, this.content);
- }
- }
- public void sendMail(String subject, String body, File attachment, List<String> recipientsBCC) throws MessagingException{
- String[] recs = new String[recipientsBCC.size()];
- for(int i = 0; i < recs.length; i++){
- recs[i] = recipientsBCC.get(i);
- }
- if(attachment != null){
- sendMail(subject, body, new String[]{ recs[0] }, null, recs, new File[] {attachment}, this.content);
- } else {
- sendMail(subject, body, new String[]{ recs[0] }, null, recs, null, this.content);
- }
- }
- /**
- * Sends a complete e-mail
- * @param subject
- * @param body
- * @param recipientsTO
- * @param recipientsCC
- * @param recipientsBCC
- * @param attachments
- * @param content
- * @throws MessagingException
- */
- public void sendMail(String subject, String body, String[] recipientsTO,
- String[] recipientsCC, String[] recipientsBCC, File[] attachments, String content)
- throws MessagingException {
- this.content = content;
- // mount e-mail
- MimeMessage message = new MimeMessage(session);
- message.setFrom(new InternetAddress(this.user));
- message.setSentDate(new Date());
- message.setSubject(subject);
- Multipart mp = new MimeMultipart();
- // mount body
- MimeBodyPart mbp1 = new MimeBodyPart();
- mbp1.setContent(body, this.content);
- mp.addBodyPart(mbp1);
- // add attachments
- if (attachments != null && attachments.length > 0) {
- for (File file : attachments) {
- MimeBodyPart mime = new MimeBodyPart();
- FileDataSource fds = new FileDataSource(file);
- mime.setDisposition(Part.ATTACHMENT);
- mime.setDataHandler(new DataHandler(fds));
- mime.setFileName(fds.getName());
- mp.addBodyPart(mime);
- }
- }
- // add addresses TO
- if (recipientsTO != null && recipientsTO.length > 0) {
- InternetAddress[] destinationAddressesTO = new InternetAddress[recipientsTO.length];
- for (int i = 0; i < destinationAddressesTO.length; i++) {
- destinationAddressesTO[i] = new InternetAddress();
- destinationAddressesTO[i].setAddress(recipientsTO[i]);
- }
- message.setRecipients(Message.RecipientType.TO,
- destinationAddressesTO);
- }
- // add addresses CC
- if (recipientsCC != null && recipientsCC.length > 0) {
- InternetAddress[] destinationAddressesCC = new InternetAddress[recipientsCC.length];
- for (int i = 0; i < destinationAddressesCC.length; i++) {
- destinationAddressesCC[i] = new InternetAddress();
- destinationAddressesCC[i].setAddress(recipientsCC[i]);
- }
- message.setRecipients(Message.RecipientType.CC,
- destinationAddressesCC);
- }
- // add addresses BCC
- if (recipientsBCC != null && recipientsBCC.length > 0) {
- InternetAddress[] destinationAddressesBCC = new InternetAddress[recipientsBCC.length];
- for (int i = 0; i < destinationAddressesBCC.length; i++) {
- destinationAddressesBCC[i] = new InternetAddress();
- destinationAddressesBCC[i].setAddress(recipientsBCC[i]);
- }
- message.setRecipients(Message.RecipientType.BCC,
- destinationAddressesBCC);
- }
- // add content and complete part
- message.setContent(mp);
- // send the message
- Transport.send(message);
- }
- public String getContent() {
- return content;
- }
- public void setContent(String content) {
- this.content = content;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment