jmail群邮件且带附件


package com.dg.xxg.util;

import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
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 sun.misc.BASE64Encoder;

public class SmUtil {
// private static Logger logger = Logger.getLogger(ThingAction.class);

public static InternetAddress fromAddress;
public static Session sendMailSession;

public SmUtil() {

}

public PAuthenticator getPopAuthen(){
  PAuthenticator popA = new PAuthenticator();//邮件安全认证。
  popA.performCheck("*******", "*********"); // 填写用户名(去掉@以及后面的)及密码
  return popA;
}
//send method
public void SendMailContent(String title,String[] email,String fname,String name,String units,String phone,String mail) {
  try {
   Properties props = new Properties();
   Transport transport;
   props.put("mail.smtp.auth", "true");
   props.put("mail.smtp.host", "smtp.126.com"); //smtp主机名
   props.put("mail.smtp.connectiontimeout", "10000");
   //props.put("mail.smtp.timeout", "10000");  
   PAuthenticator popA = getPopAuthen();
   sendMailSession = Session.getInstance(props, popA);
   Message newMessage = new MimeMessage(sendMailSession);
   if(fromAddress==null){
    try {
     fromAddress = new InternetAddress("[email protected]");
    } catch (Exception e) {
     fromAddress = new InternetAddress("[email protected]");
    }
   }
   newMessage.setFrom(fromAddress);
   newMessage.setRecipient(Message.RecipientType.TO,new InternetAddress(email[0])); // 接收方邮件地址
   for(int i=1;i<email.length;i++){
   newMessage.addRecipient(Message.RecipientType.TO,new InternetAddress(email[i]);
   }
   newMessage.setSubject(title);
   newMessage.setSentDate(new Date());
   //后面的BodyPart将加入到此处创建的Multipart中
   Multipart mp = new MimeMultipart();
   //用于保存发送附件的文件名的集合
   Vector file = new Vector();
   file.addElement(fname);
   //利用枚举器方便的遍历集合
   Enumeration efile=file.elements();
   //检查序列中是否还有更多的对象
   while(efile.hasMoreElements()){
   MimeBodyPart mbp=new MimeBodyPart();
  
   String filename="";
   //选择出每一个附件名
   filename=efile.nextElement().toString();
   //得到数据源
   FileDataSource fds=new FileDataSource(filename);
   //得到附件本身并至入BodyPart
   mbp.setDataHandler(new DataHandler(fds));
   //解决附件中带有中文名字问题
   BASE64Encoder enc = new BASE64Encoder();
   mbp.setFileName("=?GBK?B?"+enc.encode(fds.getName().getBytes())+"?=");
   //邮件内容部分
   MimeBodyPart part=new MimeBodyPart();
   String content="作者姓名:"+name+"<p> 所属单位./科室:"+units+"<p>联系电话:"+phone+"<p>电子邮箱:"+mail+"<p>";
   part.setContent(content, "text/html;charset=utf-8");
   //得到文件名同样至入BodyPart
   mp.addBodyPart(mbp);
   mp.addBodyPart(part);
   //移走集合中的所有元素
   file.removeAllElements();
   //Multipart加入到信件
          newMessage.setContent(mp);//添加文本至邮件中
          newMessage.saveChanges();//保存修改     
   }
   transport = sendMailSession.getTransport("smtp");
   transport.send(newMessage);
   transport.close();
   //System.out.println("成功!");
  } catch (MessagingException ex) {
   //System.out.println("失败!");
   ex.printStackTrace();
  }
}

public class PAuthenticator extends Authenticator {
  String username = null;
  String password = null;
  public PAuthenticator() {
  }

  public PasswordAuthentication performCheck(String user, String pass) {
   username = user;
   password = pass;
   return getPasswordAuthentication();
  }

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

public static void main(String[] args) {
  SmUtil sml = new SmUtil();
  String title="会议征文";
  String[] email={"[email protected]",[email protected]};
  String filepath="d:/新建文件夹/文档.doc";
  String name="姓名";
  String units="河北医大";
  String phone="0311-84512364";
  String mail="[email protected]";
  sml.SendMailContent(title,email,filepath,name,units,phone,mail);
 
}

}

你可能感兴趣的:(html,Yahoo,sun)