邮件工具类--EmailUtil

/* Copyright Notice ===================================================*
 * This file contains proprietary information of Trust-Mart Inc which is
 * owned by Trust-mart, Inc.
 * Copying or reproduction without prior written approval is prohibited.
 * Copyright (c) 2004 =================================================*/

/* File:    EmailUtil.java<p>
 * Usage:   send email
 * Created: 2004-03
 */
package com.jk.util;

import java.text.DecimalFormat;
import java.util.*;

import javax.mail.*;
import javax.mail.internet.*;


public class EmailUtil {

  /* ======================================== *
   * Constants
   * ======================================== */

  private static final Properties sy_prop = new Properties();

  // default key/value
  static {
    sy_prop.put("mail.transport.protocol", "smtp");
    sy_prop.put("mail.debug", "true"); // debug info.
    sy_prop.put("mail.smtp.auth", "true"); // 如果server 需要验证
  }

  /* ======================================== *
   * Class Methods
   * ======================================== */

  /** Sends plain/text email.
   *
   * @param session javax.mail.Session object, might got from jboss jndi.
   * @param recipients {to, cc1, cc2, ...}
   * @param subject
   * @param content
   * @throw IllegalArgumentException
   * @throw MessagingException
   */
  public static void sendMail(Session session, String[] recipients,
                              String subject, String content) throws
      MessagingException {

    if (session == null || recipients == null || recipients.length == 0) {
      throw new IllegalArgumentException();
    }

   // Message msg = new MimeMessage(session);
    MimeMessage msg=new MimeMessage(session);
  //  msg.setFrom(new InternetAddress(SupplierMail.from));
    msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients[0]));
    for (int i = 1; i < recipients.length; i++) {
      msg.setRecipient(Message.RecipientType.CC,
                       new InternetAddress(recipients[i]));
    }
    msg.setSubject(subject,"ISO8859_1");
    msg.setText(content,"ISO8859_1");
    msg.setSentDate(new java.util.Date());
    Transport.send(msg);
  }

  /** Sends plain/text email.
   *
   * @param session javax.mail.Session object, might got from jboss jndi.
   * @param recipients {to, cc1, cc2, ...}
   * @param subject
   * @param content
   * @throw IllegalArgumentException
   * @throw MessagingException
   */
  public static void sendMail(Session session, String from, List recipients,
                              String subject, String content) throws
      MessagingException {

    if (session == null || recipients == null || recipients.size() == 0) {
      throw new IllegalArgumentException();
    }

   // Message msg = new MimeMessage(session);
    MimeMessage msg=new MimeMessage(session);
    msg.setFrom(new InternetAddress(from));
    msg.setRecipient(Message.RecipientType.TO, new InternetAddress((String)recipients.get(0)));
    for (int i = 1; i < recipients.size(); i++) {
      if(recipients.get(i)!=null)
      msg.setRecipient(Message.RecipientType.CC,
                       new InternetAddress((String)recipients.get(i)));
    }
    msg.setSubject(subject,"GBK");
    msg.setText(content,"GBK");
    msg.setSentDate(new java.util.Date());
    Transport.send(msg);
  }

  /** User provides info. to
   *
   * get the Session object to send mail. Uses
   * WebApp.getMailSession() instead, if you use jboss.
   *
   * @param prop
   * @param user
   * @param password
   * @return
   * @see javax.mail.Session#getDefaultInstance()
   */
  public static Session getSession(Properties prop, String user,
                                   String password) {
    if (prop == null || user == null || password == null || "".equals(user)) {
      throw new IllegalArgumentException();
    }

    //1. copy
    Properties prop0 = new Properties(sy_prop);
    for (Enumeration enum1 = prop.keys(); enum1.hasMoreElements(); ) {
      String key = (String) enum1.nextElement();
      String val = (String) prop.getProperty(key);
      prop0.setProperty(key, val);
    }

    prop0.put("mail.user", user);
    prop0.put("mail.from", user);
    //2. get
    final String s1 = new String(user), s2 = new String(password);
    Authenticator auth = new Authenticator() {
      protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(s1, s2);
      }
    };
    Session session = Session.getInstance(prop0, auth);
    return session;
  }

  /* ======================================== *
   * Testing Methods
   * ======================================== */

  public static void main(String[] args) throws Exception {
    Properties prop = new Properties();
    prop.put("mail.smtp.host", "10.77.78.10");
    //Session session = Session.getDefaultInstance(prop);
   Session session = Session.getInstance(prop,null);
    String from = "admin@admin.com";
    List recipients = new ArrayList();
        recipients.add("admin@admin.com");
    String subject = "EmailUtil testing--测试";
    String content = "13:40:23,437 INFO  [STDOUT] User_Name:admin  Login_Name:admin  IP_Address:127.0.0.1";

    sendMail(session, from, recipients, subject, content);
    System.out.println("OK");
    int a=00;
    String aa=new DecimalFormat("00").format(a);
    System.out.println(new DecimalFormat("00").format(a));
  }

}

 

posted @ 2013-11-07 11:09  Pioneerlug  阅读(1741)  评论(0编辑  收藏  举报