当前位置: 代码迷 >> J2EE >> 大家快来帮小弟我解决下发送email的有关问题
  详细解决方案

大家快来帮小弟我解决下发送email的有关问题

热度:77   发布时间:2016-04-22 02:03:51.0
大家快来帮我解决下发送email的问题。
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.MessagingException;
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 javax.mail.internet.MimeUtility;

 

 


public class Mail {
/** 发件方式 - 普通发送 */
final public static int TO = 0;
/** 发件方式 - 抄送 */
final public static int CC = 1;
/** 发件方式 - 密件抄送 */
final public static int BCC = 2;

/** 邮件相关信息 - SMTP 服务器 */
private String mailSMTPHost = null;
/** 邮件相关信息 - 邮件用户名 */
private String mailUser = null;
/** 邮件相关信息 - 密码 */
private String mailPassword = null;
/** 邮件相关信息 - 发件人邮件地址 */
private String mailFromAddress = null;
/** 邮件相关信息 - 邮件主题 */
private String mailSubject = "";
/** 邮件相关信息 - 邮件发送地址 */
private Address[] mailTOAddress = null;
/** 邮件相关信息 - 邮件抄送地址 */
private Address[] mailCCAddress = null;
/** 邮件相关信息 - 邮件密件抄送地址 */
private Address[] mailBCCAddress = null;
/** 邮件相关信息 - 邮件正文(复合结构) */
private MimeMultipart mailBody = null;

public Mail() {
  mailBody = new MimeMultipart();
}

/**
  * 设置 SMTP 服务器
  * @param strSMTPHost 邮件服务器名称或 IP
  * @param strUser 邮件用户名
  * @param strPassword 密码
  */
public void setSMTPHost(String strSMTPHost, String strUser,
  String strPassword) {
  this.mailSMTPHost = strSMTPHost;
  this.mailUser = strUser;
  this.mailPassword = strPassword;
}

/**
  * 设置邮件发送地址
  * @param strFromAddress 邮件发送地址
  */
public void setFromAddress(String strFromAddress) {
  this.mailFromAddress = strFromAddress;
}

/**
  * 设置邮件目的地址
  * @param strAddress 邮件目的地址列表, 不同的地址可用;号分隔
  * @param iAddressType 邮件发送方式 (TO 0, CC 1, BCC 2) 常量已在本类定义
  * @throws AddressException
  */
public void setAddress(String strAddress, int iAddressType) throws
  AddressException {
  switch (iAddressType) {
  case Mail.TO: {
   
  String alAddress[] =strAddress.split(";");
  mailTOAddress = new Address[alAddress.length];
  for (int i = 0; i < alAddress.length; i++) {
  mailTOAddress[i] = new InternetAddress( alAddress[i]);
  }
 
/* ArrayList alAddress = StringHelper.split(strAddress, ';');
  mailTOAddress = new Address[alAddress.size()];
  for (int i = 0; i < alAddress.size(); i++) {
  mailTOAddress[i] = new InternetAddress( (String) alAddress.get(i));
  }*/
  break;
  }
  case Mail.CC: {
   
  String alAddress[] =strAddress.split(";");
  mailCCAddress = new Address[alAddress.length];
  for (int i = 0; i < alAddress.length; i++) {
  mailCCAddress[i] = new InternetAddress( alAddress[i]);
  }
   
/* ArrayList alAddress = StringHelper.split(strAddress, ';');
  mailCCAddress = new Address[alAddress.size()];
  for (int i = 0; i < alAddress.size(); i++) {
  相关解决方案