使用Commom-email发送电子邮件

import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;

import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.SimpleEmail;


public class DMail {

 /**
  * 发送简单的email,不能添加附件
  */
 public static final int SIMPLEEMAIL = 1;
 /**
  * 文本邮件,可以添加多个附件
  */
 public static final int MULTIPARTEMAIL = 2;
 /**
  * HTML格式邮件,同时具有MultiPartEmail类所有“功能”
  */
 public static final int HTMLEMAIL = 3;
 
 /**
  * 发件人的地址
  */
 private String from_address = "";
 /**
  * 邮件标题
  */
 private String title = "";
 /**
  * 邮件内容
  */
 private String text = "";
 /**
  * 发送邮件服务器的信息
  */
 private String mail_smtp_host = "";
 /**
  * 发件人用户名
  */
 private String username = "";
 /**
  * 发件人密码
  */
 private String passwod = "";
 /**
  * mail发送方式
  */
 private int mail_type = 1;
 /**
  * 邮件附件列表
  */
 private ArrayList accessoriesList = new ArrayList();
 /**
  * 发送地址列表
  */
 private ArrayList to_addressList = new ArrayList();
 /**
  * 抄送地址列表
  */
 private ArrayList cc_to_addressList = new ArrayList();
 /**
  * 密送地址列表
  */
 private ArrayList bcc_to_addressList = new ArrayList();
 
 /**
  * 获取发件人的地址
  * @return
  */
 private String getFrom_address() {
  return from_address;
 }
 /**
  * 设置发件人的地址
  * @param from_address
  */
 public void setFrom_address(String from_address) {
  this.from_address = from_address;
 }
 /**
  * 获取smtp host主机地址
  * @return
  */
 private String getMail_smtp_host() {
  return mail_smtp_host;
 }
 /**
  * 设置smtp host主机地址
  * @param mail_smtp_host
  */
 public void setMail_smtp_host(String mail_smtp_host) {
  this.mail_smtp_host = mail_smtp_host;
 }
 /**
  * 获取发件人密码
  * @return
  */
 private String getPasswod() {
  return passwod;
 }
 /**
  * 设置发件人密码
  * @param passwod
  */
 public void setPasswod(String passwod) {
  this.passwod = passwod;
 }
 /**
  * 获取邮件内容
  * @return
  */
 private String getText() {
  return text;
 }
 /**
  * 设置邮件内容(支持HTML)
  * @param text
  */
 public void setText(String text) {
  this.text = text;
 }
 /**
  * 获取邮件标题
  * @return
  */
 private String getTitle() {
  return title;
 }
 /**
  * 设置邮件标题(主题)
  * @param title
  */
 public void setTitle(String title) {
  this.title = title;
 }
 /**
  * 获取发件人用户名
  * @return
  */
 private String getUsername() {
  return username;
 }
 /**
  * 设置发件人用户名
  * @param username
  */
 public void setUsername(String username) {
  this.username = username;
 }
 /**
  * 添加附件
  * @param path
  */
 public void AddAccessories(String path){
  EmailAttachment attachment = new EmailAttachment();
  attachment.setPath(path);
  attachment.setDisposition(EmailAttachment.ATTACHMENT);
  //文件描述
  attachment.setDescription("venustech");
  try {
   attachment.setName(MimeUtility.encodeText(path.substring(path.lastIndexOf("/")+1)));//解决中文字符问题
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  }
  this.accessoriesList.add(attachment);
 }
 
 /**
  * 添加发送地址
  * @param path
  */
 public void Add_To_addressList(String path){
  String[] p = path.split(";");
  for(int i = 0 ; i < p.length;i++){
   if(p[i].equals("")){
    continue;
   }
   to_addressList.add(p[i]);
  }
 }
 /**
  * 添加抄送地址
  * @param path
  */
 public void Add_Cc_To_addressList(String path){
  String[] p = path.split(";");
  for(int i = 0 ; i < p.length;i++){
   if(p[i].equals("")){
    continue;
   }
   cc_to_addressList.add(p[i]);
  }
 }
 /**
  * 添加密送地址
  * @param path
  */
 public void Add_Bcc_To_addressList(String path){
  String[] p = path.split(";");
  for(int i = 0 ; i < p.length;i++){
   if(p[i].equals("")){
    continue;
   }
   bcc_to_addressList.add(p[i]);
  }
 }
 
 /**
  * 发送HTML格式邮件,可以添加多个附件
  * @return
  */
 private boolean sendHtmlEMail(){
  
  try {
   HtmlEmail email = new HtmlEmail();
   //smtp host
   email.setHostName(getMail_smtp_host());
   email.setCharset("UTF-8");
   //登陆邮件服务器的用户名和密码
   email.setAuthentication(getUsername(),getPasswod());
   //接收人
   //email.addTo(getTo_address(),getFrom_nickname());
   for(int i = 0 ; i < to_addressList.size();i++){
    email.addTo(((String)to_addressList.get(i)),((String)to_addressList.get(i)));
   }
   //抄送
   for(int i = 0 ; i < cc_to_addressList.size();i++){
    email.addCc(((String)cc_to_addressList.get(i)),((String)cc_to_addressList.get(i)));
   }
   //密送
   for(int i = 0 ; i < bcc_to_addressList.size();i++){
    email.addBcc(((String)bcc_to_addressList.get(i)),((String)bcc_to_addressList.get(i)));
   }
   //发送人
   email.setFrom(getFrom_address(),getFrom_address());
   //标题
   email.setSubject(getTitle());
   //邮件内容
   email.setHtmlMsg(getText());
   //添加附件(附件,可以定义多个附件对象)
   for(int i = 0; i < this.accessoriesList.size();i++){
    EmailAttachment attachment = (EmailAttachment)this.accessoriesList.get(i);
    email.attach(attachment);
   }
   //发送
   email.send();
  } catch (EmailException e) {
   return false;
  }
  return true;
 }
 /**
  * 发送文本邮件,可以添加多个附件
  * @return
  */
 private boolean sendMultipartEMail(){
  
  try {
   MultiPartEmail email = new MultiPartEmail();
   //smtp host
   email.setHostName(getMail_smtp_host());
   email.setCharset("UTF-8");
   //登陆邮件服务器的用户名和密码
   email.setAuthentication(getUsername(),getPasswod());
   //接收人
   //email.addTo(getTo_address(),getFrom_nickname());
   for(int i = 0 ; i < to_addressList.size();i++){
    email.addTo(((String)to_addressList.get(i)),((String)to_addressList.get(i)));
   }
   //抄送
   for(int i = 0 ; i < cc_to_addressList.size();i++){
    email.addCc(((String)cc_to_addressList.get(i)),((String)cc_to_addressList.get(i)));
   }
   //密送
   for(int i = 0 ; i < bcc_to_addressList.size();i++){
    email.addBcc(((String)bcc_to_addressList.get(i)),((String)bcc_to_addressList.get(i)));
   }
   //发送人
   email.setFrom(getFrom_address(),getFrom_address());
   //标题
   email.setSubject(getTitle());
   //邮件内容
   email.setMsg(getText());
   //添加附件(附件,可以定义多个附件对象)
   for(int i = 0; i < this.accessoriesList.size();i++){
    EmailAttachment attachment = (EmailAttachment)this.accessoriesList.get(i);
    email.attach(attachment);
   }
   //发送
   email.send();
  } catch (EmailException e) {
   return false;
  }
  return true;
 }
 
 
 /**
  * 发送简单邮件
  * @return
  */
 private boolean sendSimpleEMail(){
  SimpleEmail email = new SimpleEmail();
  //smtp host
  email.setHostName(getMail_smtp_host());
  email.setCharset("UTF-8");
  //登陆邮件服务器的用户名和密码
  try {
   email.setAuthentication(getUsername(),getPasswod());
   //接收人
   //email.addTo(getTo_address(),getFrom_nickname());
   for(int i = 0 ; i < to_addressList.size();i++){
    email.addTo(((String)to_addressList.get(i)),((String)to_addressList.get(i)));
   }
   //抄送
   for(int i = 0 ; i < cc_to_addressList.size();i++){
    email.addCc(((String)cc_to_addressList.get(i)),((String)cc_to_addressList.get(i)));
   }
   //密送
   for(int i = 0 ; i < bcc_to_addressList.size();i++){
    email.addBcc(((String)bcc_to_addressList.get(i)),((String)bcc_to_addressList.get(i)));
   }
   //发送人
   email.setFrom(getFrom_address(),getFrom_address());
   //标题
   email.setSubject(getTitle());
   //邮件内容
   email.setMsg(getText());
   //发送
   email.send();
  } catch (EmailException e) {
   e.printStackTrace();
   return false;
  }
  return true;
 }
 
 /**
  * 发送邮件
  * @return
  */
 public boolean sendEMail(){
  
  boolean result = false;
  if(this.getMail_type() == this.SIMPLEEMAIL){
   result = sendSimpleEMail();
  }else if(this.getMail_type() == this.MULTIPARTEMAIL){
   result = sendMultipartEMail();
  }else{
   result = sendHtmlEMail();
  }
  return result;
 }
 /**
  * 获取邮件发送类型
  * @return
  */
 public int getMail_type() {
  return mail_type;
 }
 /**
  * 设置邮件发送类型
  * @param mail_type
  */
 public void setMail_type(int mail_type) {
  this.mail_type = mail_type;
 }
 /**
  * 发送邮件的例子
  * @param args
  */
 public static void main(String [] args){
  
//  DMail dmail = new DMail();
//  /**/
//  dmail.setFrom_address("发件人地址");//完整邮箱
//  dmail.Add_To_addressList("收件人地址(列表)");//收件人邮箱地址(多个地址以;分割)
//  dmail.Add_Cc_To_addressList("抄送地址(列表)");
//  dmail.Add_Bcc_To_addressList("密送地址(列表)");
//  dmail.setTitle("邮件的标题信息");
//  dmail.setText("邮件内容");
//  dmail.setMail_smtp_host("smtp服务器地址");
//  dmail.setUsername("用户名");
//  dmail.setPasswod("密码");
//  /**/
//  
//  //简单邮件发送方式
//  dmail.setMail_type(DMail.SIMPLEEMAIL);
//  if(dmail.sendEMail()){
//   System.out.println("邮件发送成功");
//  }else{
//   System.out.println("邮件发送失败");
//  }
  
  /**发送带附件的邮件**
  dmail.setMail_type(DMail.MULTIPARTEMAIL);
  dmail.AddAccessories("c://response.txt");
  dmail.AddAccessories("c://12.txt");
  if(dmail.sendEMail()){
   System.out.println("邮件发送成功");
  }else{
   System.out.println("邮件发送失败");
  }/**/
 }
}

你可能感兴趣的:(apache,html,sql)