title: log4j Custom Mail Appender to send emails with file attachments
body: <p>I have an overridden log4j SMTPAppender which is responsible for sending emails with file attachments. </p>
  
  <p>I have a private member variable which holds the list of file attachments. And before sending an email, I set this list of file attachments. This works perfectly fine for the first email. The email is nicely formatted and the files are attached to the email. But for the subsequent emails, the attachment are displayed as inline text. Here
  s how my subsequent emails look - </p>
  
  <blockquote>
    <p>Blockquote 
    ------=_Part_2_694066731.1314069356582</p>
    
    <p>22-Aug-2011 22:15:56
    FATAL</p>
    
    <p>This is a test email to check if emailing will work.
    ------=_Part_2_694066731.1314069356582
    Content-Disposition: attachment; filename=Testing.zip</p>
    
    <p>PK<br>
    Blockquote</p>
  </blockquote>
  
  <p>Here's my overridden sendBuffer() - </p>
  
  <pre><code>  @Override 
      protected void sendBuffer() {
      try {
          Multipart mp = new MimeMultipart();                             
          MimeBodyPart messagePart = new MimeBodyPart();
          StringBuffer sbuf = new StringBuffer();
          String t = layout.getHeader();
          if (t != null)
              sbuf.append(t);
          int len = cb.length();
          for (int i = 0; i &lt; len; i++) {
              LoggingEvent event = cb.get();
              sbuf.append(layout.format(event));
              if (layout.ignoresThrowable()) {
                  String[] s = event.getThrowableStrRep();
                  if (s != null) {
                      for (int j = 0; j &lt; s.length; j++) {
                          sbuf.append(s[j]);
                          sbuf.append(Layout.LINE_SEP);
                      }
                  }
              }
          }
          t = layout.getFooter();
          if (t != null)
          sbuf.append(t);
          messagePart.setContent(sbuf.toString(), layout.getContentType());
          messagePart.setDisposition(BodyPart.INLINE);
          mp.addBodyPart(messagePart);
  
          //For each attachment
          for (File file : fileAttachments) {
              // Part two is attachment
              MimeBodyPart attachmentPart = new MimeBodyPart();
              FileDataSource fileDataSource = new FileDataSource(file) {
                  @Override
                  public String getContentType() {
                      return "application/zip";
                  }
              };
  
              attachmentPart.setDataHandler(new DataHandler(fileDataSource));
              attachmentPart.setFileName(file.getName());
              attachmentPart.setDisposition(BodyPart.ATTACHMENT);
              mp.addBodyPart(attachmentPart);
  
          }
  
          msg.setContent(mp);
          msg.setSentDate(new Date());
          msg.setSubject(getSubject());           
          send(msg);
      } catch (Exception e) {
          LogLog.error("Error occured while sending e-mail notification.", e);
      }
  }
  </code></pre>
  
  <p>I am using logj in an enterprise application with jboss as application server. Appreciate any suggestions.</p>