支持邮件群发,多附件发送的javaMail代码

阅读更多

http://hi.baidu.com/yuzhi2217/blog/item/37903ad514df10c650da4b7c.html

 

 

用到的包名

activation.jar

mail.jar

java日志文件包可以不用

log4j-1.2.11.jar

源代码如下:

package test;

import java.awt.List;
import java.net.MalformedURLException;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.dom4j.DocumentException;


/**
* @author 俞晓坚

*update 2007-08-15
*
*/
public class MailSendtest {

String host="smtp.163.com";
String userName="yuxia2217";//可替换为你自己的邮箱用户名
String password="***************************";//不好意思密码不能给你们看
String [email protected] //你要发送的邮箱名称
String to;//发给谁
String subject;//邮件议题
String body;//内容

String fileName;//附件路径(注意绝对路径和相对路径的选择)

// 用于保存发送附件的文件名列表
List arrFileName = new List();

public MailSendtest(String to, String subject, String body)
     throws MalformedURLException, DocumentException {
    //初始化发件人、收件人、主题等

    this.to = to;
    this.subject =subject;
    this.body = body;
}

// 用于收集附件名
public void attachFile(String fileName) {
    this.arrFileName.add(fileName);
}

// 开始发送
public boolean startSend() {

    //创建Properties对象
    Properties props = System.getProperties();
    //创建信件服务器
    props.put("mail.smtp.host", this.host);
//    props.put("mail.smtp.host", "smtp.163.com");
    props.put("mail.smtp.auth", "true"); //通过验证
    //得到默认的对话对象
    Session session = Session.getDefaultInstance(props, null);
    try {
     //创建一个消息,并初始化该消息的各项元素
     MimeMessage msg = new MimeMessage(session);
     msg.setFrom(new InternetAddress(this.from));
     if (this.to.trim().length() > 0) {
      String[] arr = this.to.split(",");
      //int ReceiverCount=1;
      int receiverCount = arr.length;
      if (receiverCount > 0) {
       InternetAddress[] address = new InternetAddress[receiverCount];
       for (int i = 0; i < receiverCount; i++) {
        address[i] = new InternetAddress(arr[i]);
       }
       msg.addRecipients(Message.RecipientType.TO, address);
      } else {
       System.out
         .println("None receiver! The program will be exit!");
       System.exit(0);
      }
     } else {
      System.out.println("None receiver! The program will be exit!");
      System.exit(0);
     }
     msg.setSubject(subject);
     //后面的BodyPart将加入到此处创建的Multipart中
     Multipart mp = new MimeMultipart();      
     //获取附件
     int FileCount = this.arrFileName.getItemCount();
     if (FileCount > 0) {
      for (int i = 0; i < FileCount; i++) {
       MimeBodyPart mbp = new MimeBodyPart();     
       //选择出附件名
       fileName = arrFileName.getItem(i).toString();
       //得到数据源
       FileDataSource fds = new FileDataSource(fileName);
       //得到附件本身并至入BodyPart
       mbp.setDataHandler(new DataHandler(fds));
       //得到文件名同样至入BodyPart
       mbp.setFileName(fds.getName());
       mp.addBodyPart(mbp);
      }
      MimeBodyPart mbp = new MimeBodyPart();
      mbp.setText(this.body);
      mp.addBodyPart(mbp);
      //移走集合中的所有元素
      arrFileName.removeAll();
      //Multipart加入到信件
      msg.setContent(mp);
     } else {
      //设置邮件正文
      msg.setText(this.body);
     }
     //设置信件头的发送日期
     msg.setSentDate(new Date());
     msg.saveChanges();
     //发送信件
     Transport transport = session.getTransport("smtp");
     transport.connect(this.host, this.userName, this.password);
     transport.sendMessage(msg, msg
       .getRecipients(Message.RecipientType.TO));
     transport.close();
    } catch (MessagingException mex) {
     mex.printStackTrace();
     Exception ex = null;
     if ((ex = mex.getNextException()) != null) {
      ex.printStackTrace();
     }
     return false;
    }
    return true;
}

public static void main(String[] args) {

    try {
     MailSendtest sendmail = new MailSendtest("[email protected],[email protected]",
       "邮件主题", "邮件内容");
     sendmail.attachFile("E:\\eclipse\\startup.jar");//附件地址
     sendmail.attachFile("E:\\eclipse\\notice.html");/ /附件2地址;根据需要可继续添加....................
     sendmail.startSend();
     System.out.println("OK");
    } catch (Exception e) {
     e.printStackTrace();
    }

}
}

你可能感兴趣的:(Eclipse,log4j,.net,HTML,Blog)