Package anvil.util

Source Code of anvil.util.MailUtils

/*
* $Id: MailUtils.java,v 1.10 2002/09/16 08:05:07 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.util;

import java.io.File;
import java.util.Date;
import java.util.Hashtable;
import java.util.Properties;
import java.util.Vector;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.activation.MimetypesFileTypeMap;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;

/**
* MailUtils
*
* @author: Jaripekka Salminen
*/
public class MailUtils
{

  static String charset = "iso-8859-1";
  static Hashtable mimeTypes;

  static {
    mimeTypes = new Hashtable();
    mimeTypes.put("","application/octet-stream");
    mimeTypes.put("txt","text/plain");
    mimeTypes.put("gif","image/gif");
    mimeTypes.put("jpg","image/jpeg");
    mimeTypes.put("jpeg","image/jpeg");
  }

  /**
   * Sends an email message.
   *
   * @param from    sender
   * @param to      comma separated list of recipients
   * @param cc      comma separated list of Cc recipients
   * @param subject email subject
   * @param body    body of the message
   */
  public static void sendMail(String host, String from, String to, String cc,
        String subject, String body) throws MessagingException {
   
    sendMail(host, from, to, cc, subject, body, null);
  }

  /**
   * Sends an email message.
   *
   * @param from    sender
   * @param to      comma separated list of recipients
   * @param cc      comma separated list of Cc recipients
   * @param subject email subject
   * @param body    body of the message
   * @param attachments a vector of File attachments
   */
  public static void sendMail(String host, String from, String to, String cc,
        String subject, String body, Vector attachments) throws MessagingException
  {
    if (from == null || from.length() == 0) {
      throw new MessagingException("No 'From' address specified");
    }
    if (to == null || to.length() == 0) {
      throw new MessagingException("No 'To' address specified");
    }

    Properties properties = System.getProperties();
    properties.put("mail.smtp.host", host);
    javax.mail.Session session = Session.getDefaultInstance(properties,null);

    MimeMessage msg = new MimeMessage(session);
    msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));

    if (cc != null && cc.length() > 0) {
      msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false));
    }
    msg.setSentDate(new Date());
    if (subject != null) {
      msg.setSubject(subject, charset);
    }
    msg.setFrom(new InternetAddress(from));

    if (attachments == null || attachments.size() == 0) {
      msg.setText(body, charset);

    } else {
      MimeMultipart mimeMultipart = new MimeMultipart();
      MimeBodyPart mimeBodyPart = new MimeBodyPart();
      if (body != null) {
          mimeBodyPart.setText(body);
          mimeMultipart.addBodyPart(mimeBodyPart);
      }

      for (int i=0; i<attachments.size(); i++) {
        Object obj = attachments.elementAt(i);
        if (obj instanceof File) {
          File file = (File)obj;
          if (file.isFile() && file.exists() && file.length() > 0) {
            mimeBodyPart = new MimeBodyPart();
            FileDataSource fds = new FileDataSource(file);
            MimetypesFileTypeMap map = new MimetypesFileTypeMap();
            map.addMimeTypes(getTypeEntry(file));
            fds.setFileTypeMap(map);
            mimeBodyPart.setDataHandler(new DataHandler(fds));
            mimeBodyPart.setFileName(file.getName());
            mimeBodyPart.setDisposition(getDisposition(file));
            mimeMultipart.addBodyPart(mimeBodyPart);
          }
        }
      }
      msg.setContent(mimeMultipart);
    }
    Transport.send(msg);
  }

  private static String getTypeEntry(File file) {
    String ext = getExtension(file);
    String mimeType = (String)mimeTypes.get(ext);
    if (mimeType != null) {
      return mimeType+" "+ext;
    } else {
      return "application/octet-stream x";
    }
  }

  private static String getDisposition(File file) {
    String ext = getExtension(file);
    if (ext.equals("gif") ||
        ext.equals("jpeg") ||
        ext.equals("jpg")) {
      return "inline";
    } else {
      return "attachment";
    }
  }

  private static String getExtension(File file) {
    String name = file.getName();
    int dot = name.lastIndexOf(".");
    if (dot >= 0 && dot < (name.length()-1)) {
      return name.substring(dot+1).toLowerCase();
    } else {
      return "";
    }
  }
}
TOP

Related Classes of anvil.util.MailUtils

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.