jsp 郵件群發 程式碼

來源:互聯網
上載者:User

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);

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.