jsp 邮件群发 程序代码

作者:袖梨 2022-07-02

jsp 邮件群发 程序代码
public static boolean sendMail(String subject,String body,String to,String content,Address[] ValidUnsentAddresses,boolean isReSend) {
  logger.info("始发送邮件*");
  boolean result=false;
  try {
   Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

   // Get a Properties object
   Properties props = System.getProperties();
   props.setProperty("mail.smtp.host", STMT_SERVER);//
   props.setProperty("mail.transport.protocol", "smtp");// -
   // props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
   props.setProperty("mail.smtp.socketFactory.fallback", "false");
   props.setProperty("mail.smtp.port", PORT);/
   props.setProperty("mail.smtp.socketFactory.port", PORT);
   props.put("mail.smtp.auth", "true");
  
   final String username = USERNAME;
   final String password = PASSWORD;
  
   Session session = Session.getDefaultInstance(props,
     new Authenticator() {
      protected PasswordAuthentication getPasswordAuthentication() {
       return new PasswordAuthentication(username,
         password);
      }
     });
 
   session.setDebug(true);
   Transport transport = session.getTransport("smtp");
   Message msg = new MimeMessage(session);
 
   if(ValidUnsentAddresses==null){
    List list=new ArrayList();;
   
    String[] gods=to.split(",");
    for (int i = 0; i < gods.length; i++) {
     try {
      if(gods[i]!=null && !"".equals(gods[i])&&SendMailManager.checkEmail(gods[i])){
       list.add(new InternetAddress(gods[i]));
      }    
     } catch (AddressException e) {
      logger.error(SendMailManager.class.getName()+"  :"+e.getMessage());  
      e.printStackTrace();
     }
    }
    // ----------
    InternetAddress[] address = (InternetAddress[])list.toArray(new InternetAddress[list.size()]);
    msg.setRecipients(Message.RecipientType.BCC, address);
   }else{
    msg.setRecipients(Message.RecipientType.BCC, ValidUnsentAddresses);
   }
  
  
   msg.setFrom(new InternetAddress(USER_EMAIL));
  
   msg.setSubject(subject);
  
   msg.setSentDate(new Date());
  
  
   if(body!=null&&!"".equals(body)){
    MimeMultipart mp = new MimeMultipart();
    MimeBodyPart bp = new MimeBodyPart();
    bp.setContent(body, "text/html;charset=utf-8");
    mp.addBodyPart(bp);
    msg.setContent(mp);
   }else if(!"".equals(content)){
    msg.setText(content);
           }
                            msg.saveChanges();
   transport.connect((String) props.get("mail.smtp.host"),USER_EMAIL, password);
   transport.sendMessage(msg, msg.getRecipients(Message.RecipientType.BCC));
   transport.close();
   result=true;
   System.out.println("邮件已发送成功!");
  
  } catch (SendFailedException e) {
   try {
    Thread.currentThread().sleep(10000);
   } catch (InterruptedException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   }
   if(e.getValidUnsentAddresses()!=null && isReSend){
   
    sendMail(subject,body,"",content,e.getValidUnsentAddresses(),false);

   }
  
  }catch (MessagingException e) {
   result=false;
   System.out.println("邮件已发失败!");
   logger.error(SendMailManager.class.getName()+"  :"+e.getMessage());
   e.printStackTrace();
  }finally{
   //System.out.println(new Date().toLocaleString()+"连接smtp一次");
  }
  return result;
 }
外部js 调用方法

sendMail(subject,body,to,content,null,true);

相关文章

精彩推荐