发送邮件

来源:互联网 发布:免费网校系统php源码 编辑:程序博客网 时间:2024/06/10 10:14

public class SendMail extends LEAPLogic
{
 private MimeMessage mimeMsg; //MIME邮件对象
 private Session session; //邮件会话对象
 private Properties props; //系统属性
 private String username = ""; //smtp认证用户名和密码
 private String password = "";
 private Multipart mp; //Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象
 
 private void sendMail(String smtp)
 {
  setSmtpHost(smtp);
  createMimeMessage();
 }
 
 private void setSmtpHost(String hostName)
 {
  System.out.println("设置系统属性:mail.smtp.host = "+hostName);
  if(props == null)props = System.getProperties(); //获得系统属性对象
  props.put("mail.smtp.host",hostName); //设置SMTP主机
 }
 
 private boolean createMimeMessage()
 {
  try
  {
   System.out.println("准备获取邮件会话对象!");
   session = Session.getDefaultInstance(props,null); //获得邮件会话对象
  }
  catch(Exception e)
  {
   System.err.println("获取邮件会话对象时发生错误!"+e);
   return false;
  }
  System.out.println("准备创建MIME邮件对象!");
  try
  {
   mimeMsg = new MimeMessage(session); //创建MIME邮件对象
   mp = new MimeMultipart();
   return true;
  }
  catch(Exception e)
  {
   System.err.println("创建MIME邮件对象失败!"+e);
   return false;
  }
 }
 
 private void setNeedAuth(boolean need)
 {
  System.out.println("设置smtp身份认证:mail.smtp.auth = "+need);
  if(props == null)props = System.getProperties();
  if(need)
  {
   props.put("mail.smtp.auth","true");
  }
  else
  {
   props.put("mail.smtp.auth","false");
  }
 }
 
 private void setNamePass(String name,String pass)
 {
  username = name;
  password = pass;
 }
 
 private boolean setSubject(String mailSubject)
 {
  System.out.println("设置邮件主题!");
  try
  {
   mimeMsg.setSubject(MimeUtility.encodeText(mailSubject));
   return true;
  }
  catch(Exception e)
  {
   System.err.println("设置邮件主题发生错误!");
   return false;
  }
 }
 
 private boolean setBody(String mailBody)
 {
  try{
   BodyPart bp = new MimeBodyPart();
   bp.setContent(""+mailBody,"text/html;charset=GB2312");
   mp.addBodyPart(bp);
   return true;
  }
  catch(Exception e){
   System.err.println("设置邮件正文时发生错误!"+e);
   return false;
  }
 }
 
 private boolean addFileAffix(String filename)
 {
  System.out.println("增加邮件附件:"+filename);
  try{
   BodyPart bp = new MimeBodyPart();
   FileDataSource fileds = new FileDataSource(filename);
   bp.setDataHandler(new DataHandler(fileds));
   bp.setFileName(MimeUtility.encodeWord(fileds.getName()));
   mp.addBodyPart(bp);
   return true;
   }
   catch(Exception e)
   {
    System.err.println("增加邮件附件:"+filename+"发生错误!"+e);
    return false;
   }
 }
 
 private boolean setFrom(String from)
 {
  System.out.println("设置发信人!");
  try{
  mimeMsg.setFrom(new InternetAddress(from)); //设置发信人
  return true;
  }
  catch(Exception e)
  {
  return false;
  }
 }
 
 private boolean setTo(String to)
 {
  if(to == null)return false;
  try{
   mimeMsg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));
   return true;
  }
  catch(Exception e)
  {
   return false;
  }
 }
 
 private boolean setCopyTo(String copyto)
 {
  if(copyto == null)return false;
  try{
   mimeMsg.setRecipients(Message.RecipientType.CC,(Address[])InternetAddress.parse(copyto));
   return true;
  }
  catch(Exception e)
  {
   return false;
  }
 }
 
 private boolean sendout()
 {
  try
  {
   mimeMsg.setContent(mp);
   mimeMsg.saveChanges();
   System.out.println("正在发送邮件....");
   Session mailSession = Session.getInstance(props,null);
   Transport transport = mailSession.getTransport("smtp");
 
   transport.connect((String)props.get("mail.smtp.host"),username,password);
   transport.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.TO));
   //transport.send(mimeMsg);
   System.out.println("发送邮件成功!");
   transport.close();
   return true;
  }
  catch(Exception e)
  {
   System.err.println("邮件发送失败!"+e);
   return false;
  }
 }

 public boolean SendmailMain(SendMailParameters smps)
 {
  try
  {
   String name = "";
   String pwd = "";
   String smtpHost = smps.getSmtpHost();
   if (smtpHost == null || smtpHost.length() == 0) { smtpHost = "public.longrise.com.cn"; }
   sendMail(smtpHost);
   
   Boolean needAuth = false;
   int _needAuth = smps.getNeedAuth();
   if (_needAuth == 0)
   { needAuth = false; }
   else
   { needAuth = true; }
   
   needAuth = true;
   
   setNeedAuth(needAuth);
 
   String fromMailName = smps.getFromMailName();
   if (fromMailName == null || fromMailName.length() == 0)
   {
    fromMailName = "longrise@longrise.com.cn";
    name = "longrise";
    pwd  = "l1o2n3g4";
   }
   else
   {
    fromMailName = "longrise@longrise.com.cn";
    name = smps.getSendMailName();
    pwd = smps.getSendMailPassWord();
   }
   
   if(setFrom(fromMailName) == false) return false;
   
   if(setSubject(smps.getSubject()) == false) return false;
   if(setBody(smps.getMailBody()) == false) return false;
   if(setTo(smps.getToMailName()) == false) return false;
   
   //电子投保单
//   String fileAffixPath =  smps.getFileAffixPath();
//   if (fileAffixPath.length() > 0)
//   {
//    String[] fileAffixPathArry = null;
//    fileAffixPathArry = fileAffixPath.split(";");
//    if(fileAffixPathArry.length > 0){
//     for(int i = 0;i < fileAffixPathArry.length; i ++){     
//      String _tempfileAffixPath = Global.getInstance().getRootPath() + "/ExcelReports/" + fileAffixPathArry[i];
//      if (new File(_tempfileAffixPath).exists())
//       addFileAffix(_tempfileAffixPath);
//      //if(addFileAffix(_tempfileAffixPath) == false) return false;
//     }
//    }
//   }
   //增加附件
   String fileAttachmentPath =  smps.getFileAttachmentPath();
   if (fileAttachmentPath.length() > 0)
   {
    String[] fileAttachmentPathArry = null;
    fileAttachmentPathArry = fileAttachmentPath.split(";");
    if(fileAttachmentPathArry.length > 0){
     for(int i = 0;i < fileAttachmentPathArry.length; i ++){
      String _tempAttachmentPath = NameedPathUtil.getPath ("default") + fileAttachmentPathArry[i];
      if (new File(_tempAttachmentPath).exists())
       addFileAffix(_tempAttachmentPath);
      //if(addFileAffix(_tempAttachmentPath) == false) return false;
     }
    }
   }

   setNamePass(name,pwd);
   if(sendout() == false) return false;
   return true;
  }
     catch (Exception e)
     {
         Global.getInstance().LogError(this, e);
         return false;
     }
 }
}

原创粉丝点击