如何利用JMAILAPI发送EMAIL

这个示例是带验证的发送程序
重要提示:   除了使用JMail, 还要当一个jaf-1_0_2-upd.zip包, 以后下载软件前, 一定要注意阅读文档, 免得浪费时间

Mail.java
/*
 * Created on 2004/08/25
 */

package com.csk.dm;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.SendFailedException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * @author 仇驚雷
 */
public class Mail {
 /**
  * smtp username
  */
 public static String userName = null;
 /**
  * smtp password
  */
 public static String password = null;

 /**
  * smtp host
  */
 public static String host = null;

 /**
  * send a mail with specilied parameters
  *
  * @param from
  *            sender
  * @param to
  *            receiver
  * @param host
  *            smtp host
  * @param subject
  *            subject of mail
  * @param body
  *            content of mail
  * @throws SendFailedException
  */
 public void sendMail(String from, String to, String subject, String body)
   throws SendFailedException {
  sendMail(from, from, to, subject, body);
 }

 public void sendMail(String from, String to, String[] cc, String subject,
   String body) throws SendFailedException {
  sendMail(from, from, to, cc, subject, body);
 }

 public void sendMail(String from, String friendName, String to,
   String subject, String body) throws SendFailedException {
  sendMail_internal(from, friendName, to, null, subject, body);
 }

 public void sendMail(String from, String friendName, String to,
   String[] cc, String subject, String body)
   throws SendFailedException {
  sendMail_internal(from, friendName, to, cc, subject, body);
 }

 private void sendMail_internal(String from, String friendName, String to,
   String cc[], String subject, String body)
   throws SendFailedException {
  String[] toArr = new String[1];
  toArr[0] = to;
  sendMail_internal(from, friendName, toArr, cc, subject, body);
 }

 /**
  * send a mail with specilied parameters
  *
  * @param from
  *            sender
  * @param friendName
  *            friend name of sender
  * @param to
  *            receiver
  * @param host
  *            smtp host
  * @param subject
  *            subject of mail
  * @param body
  *            content of mail
  * @throws SendFailedException
  */
 private void sendMail_internal(String from, String friendName, String[] to,
   String cc[], String subject, String body)
   throws SendFailedException {
  Properties props = new Properties();

  Session session = null;
  Message msg = null;

  try {
   SmtpAuth sa = new SmtpAuth();
   props.put("mail.smtp.auth", "true");
   props.put("mail.smtp.host", host);
   props.put("mail.smtp.port", "25");
   sa.setUserinfo(userName, password);
   session = Session.getInstance(props, sa);
   session.setDebug(true);

   msg = new MimeMessage(session);
   msg.setFrom(new InternetAddress(from, friendName));
   InternetAddress[] toAddress = new InternetAddress[to.length];
   for (int i = 0; i < to.length; i++) {
    toAddress[i] = new InternetAddress(to[i]);
    toAddress[i].setAddress(to[i]);
   }
   msg.setRecipients(Message.RecipientType.TO, toAddress);

   if ((cc != null) && (cc.length != 0)) {
    InternetAddress[] ccAddress = new InternetAddress[cc.length];
    for (int i = 0; i < cc.length; i++) {
     ccAddress[i] = new InternetAddress(cc[i]);
     ccAddress[i].setAddress(cc[i]);
    }
    msg.setRecipients(Message.RecipientType.CC, ccAddress);
   }

   msg.setSubject(subject);
   msg.setSentDate(new Date());
   msg.setText(body);
   Transport.send(msg);
  } catch (AddressException e) {
   e.printStackTrace();
  } catch (SendFailedException se) {
   throw se;
  } catch (MessagingException e) {
   e.printStackTrace();
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  }
 }
}

SmtpAuth .java
/*
 * Created on 2004/08/26
 */
package com.csk.dm;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

/**
 * Email身? Validator
 * @author 仇驚雷
 */
public class SmtpAuth extends Authenticator {
 private String user, password;
 
 /**
  * @param getuser
  * @param getpassword
  */
 public void setUserinfo(String getuser, String getpassword) {
  user = getuser;
  password = getpassword;
 }

 protected PasswordAuthentication getPasswordAuthentication() {
  return new PasswordAuthentication(user, password);
 }
}

posted on 2004-08-27 11:03  muddle  阅读(1216)  评论(1编辑  收藏  举报

导航