登录站点

用户名

密码

最新:facebook脸书下载地址


福利:怎么注册一个可以看明星名人福利的facebook账号?


推荐:facebook脸谱邀请码(最新免费)

JAVA邮件发送

已有 297 次阅读   2010-11-23 19:11   标签JAVA  邮件 
package com.fangrn.email;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.activation.URLDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
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 com.fangrn.util.DateUtils;
import com.fangrn.util.Tools;
import com.sun.mail.imap.IMAPStore;
/**
* @author Fangrn
* 邮件发送类
*/
public class SendBean {
public SendBean() {
}
/**
  * 绝版邮件发送函数
  * 支持本地和远程附件,文本和HTML编码格式主体数据
  * @throws EmailException 
  * @throws MessagingException 
  * @throws AddressException 
  * @throws Exception 
  */
public synchronized void send(Email email,boolean record) throws EmailException, AddressException, MessagingException{
  Properties props = new Properties();
  props.put("mail.smtp.host",email.getMailHost());
  MyAuthenticator auth=new MyAuthenticator(email.getFromname(),email.getPassword());
  System.out.println(email.getFromname()+"------\t"+email.getPassword());
  Session session=null;
  if (session == null) {
   props.put( "mail.smtp.auth", "true" );
   session = Session.getDefaultInstance(props, auth);
  }
  Message mesg=new MimeMessage(session);
  
  if (!email.isComplete()){
   throw new EmailException("邮件设置错误!");
  }
  System.out.println("邮件通过验证!");
  
//  打开调试
  if (email.isVerbose()){
   session.setDebug(true);
  }
  //发件人
  if(email.getFrom()==null)
   throw new EmailException("请设置发件地址!");
  mesg.setFrom(new InternetAddress(email.getFrom()));
  System.out.println("设置发件人");
  
  //收件人,收件人地址
  if(email.getToList()==null||email.getToList().isEmpty())
   throw new EmailException("请设置收件人");
  InternetAddress[] addresses=new InternetAddress[email.getToList().size()];
  for (int i = 0; i < addresses.length; i++) {
   if(email.getToList().get(i).equals(null)||email.getToList().get(i).equals(""))
   {
    continue;
   }
   System.out.println("*********test**********"+email.getToList().get(i).toString());
   addresses = new InternetAddress(email.getToList().get(i).toString());
   System.out.println(addresses); 
  }
  mesg.setRecipients(Message.RecipientType.TO, addresses);
  System.out.println("设置收件人");
  
  
  //抄送
  if(email.getRecipientsA()!=null&&!email.getRecipientsA().isEmpty()){
   addresses = new InternetAddress[email.getRecipientsA().size()];
   System.out.println(addresses.length);
   for (int i = 0; i < addresses.length; i++) {
    if(email.getRecipientsA().get(i).equals(null)||email.getRecipientsA().get(i).equals(""))
    {
     continue;
    }
    System.out.println((String) email.getRecipientsA().get(i));
    addresses = new InternetAddress((String) email.getRecipientsA().get(i));
   }
   mesg.setRecipients(Message.RecipientType.CC, addresses);
   System.out.println("设置设置抄送");
  }else{
   System.out.println("没有抄送目标");
  }
  //密送
  if(email.getRecipientsB()!=null&&!email.getRecipientsB().isEmpty()){
   addresses = new InternetAddress[email.getRecipientsB().size()];
   for (int i = 0; i < addresses.length; i++) {
    if(email.getRecipientsB().get(i).equals(null)||email.getRecipientsB().get(i).equals(""))
    {
     continue;
    }
    addresses = new InternetAddress( (String) email.getRecipientsB().get(i));
   }
   mesg.setRecipients(Message.RecipientType.BCC, addresses);
   System.out.println("设置密送");
  }else{
   System.out.println("没有密送目标");
  }
  
  //邮件主题
  if(email.getSubject()!=null){
   mesg.setSubject(email.getSubject());
   mesg.setSentDate(DateUtils.getCurrentDateTime());
   System.out.println("设置邮件主题");
  }else{
   mesg.setSubject("[没有主题]");
   mesg.setSentDate(DateUtils.getCurrentDateTime());
   System.out.println("没有邮件主题");
  }
  
  //邮件主体
  Multipart mp = new MimeMultipart();
  MimeBodyPart mbp = new MimeBodyPart();
  if(email.getBody()!=null){
   if (email.getBodycharset()!=""){
    mbp.setText(email.getBody(),email.getBodycharset());//给主体部分设置文件编码格式---"test/html;charset=gb2312"
   }else{
    mbp.setText(email.getBody());
   }
   mp.addBodyPart(mbp);
   System.out.println("设置邮件主体");
  }else{
   if (email.getBodycharset()!=""){
    mbp.setText("",email.getBodycharset());//给主体部分设置文件编码格式---"test/html;charset=gb2312"
   }else{
    mbp.setText("");
   }
   mp.addBodyPart(mbp);
   System.out.println("设置邮件主体");
  }
  //邮件附件
  if (email.getAccessorieList()!=null&&!email.getAccessorieList().isEmpty()){
   int accessoriesCount = email.getAccessorieList().size();
   File f;
   DataSource ds;
   String uf;
   int j;
   for (int i = 0; i < accessoriesCount; i++) {
    mbp = new MimeBodyPart();
    
    try {
     //远程文件
     URLDataSource ud=new URLDataSource(new URL(email.getAccessorieList().get(i).toString()));
     mbp.setDataHandler(new DataHandler(ud));
     j = ud.getName().lastIndexOf(File.separator);
     uf = ud.getName().substring(j + 1);
     
     System.out.println("附件名:"+Tools.encode(uf,"gb2312"));
     mbp.setFileName(Tools.encode(uf,"gb2312"));//设置保存文件名
     mp.addBodyPart(mbp);
     
    } catch (MalformedURLException e) {
     //本地文件
     f = new File(email.getAccessorieList().get(i).toString());
     ds = new FileDataSource(f);
     mbp.setDataHandler(new DataHandler(ds));
     j = f.getName().lastIndexOf(File.separator);
     uf = f.getName().substring(j + 1);
     try {
      uf=Tools.encode(uf,"gb2312");
     } catch (Exception ex) {
      // TODO Auto-generated catch block
      ex.printStackTrace();
     }
     System.out.println("附件名:"+Tools.encode(uf,"gb2312"));
     mbp.setFileName(Tools.encode(uf,"gb2312"));//设置保存文件名
     mp.addBodyPart(mbp);
    }
   }
   mesg.setContent(mp);
   System.out.println("设置邮件附件");
  }else{
   mesg.setContent(mp);
   System.out.println("没有附件!");
   
  }
  //发送邮件
  Transport.send(mesg);
  System.out.println("邮件发送成功!");
  props=null;
  session=null;
}
/**
  * 回复消息
  */
// public boolean replyEmail(String messageID,String from,boolean rAll) throws Exception{
//  String[] ids={messageID};
//  Message[] msg=this.getMessage(ids);
//  MimeMessage rep=(MimeMessage)msg[0].reply(rAll);
//  rep.setFrom(new InternetAddress(from));
//  rep.setText(arg0)
// }
}
更多