title: sending calender invitation as mail attachment in java mail API
body: <p>I am creating icalender invitations in my java code dynamically. Now I want to send this icalender invitation as attachment in a mail without saving it to my server as file.</p>
  
  <p>How can I achieve it?</p>
  
  <p>My Code is:</p>
  
  <hr>
  
  <pre><code>public static void sendNow(String subject, String[] toArray, String[] ccArray, BodyPart bodyPart, BodyPart headerPart, List &lt;MimeMessage&gt; attachments ) {
              MimeMessage message = new MimeMessage(getMimeSession());
              // Add From Email id
              // Add TO receipients
              // Add CC receipients
              // SET Subject
  
              // Create a Multipart
              Multipart multipart = new MimeMultipart();
              if (null != bodyPart) {
                  multipart.addBodyPart(bodyPart);
              }
              if (null != headerPart) {
                  multipart.addBodyPart(headerPart);
              }
               // Attaching MIMEMessage as attachment - IT IS NOT WORKING
                  if(null != attachments) {
  
                  //attach meeting invitation
                  for (MimeMessage attach : attachments) {
                      message.setDataHandler(attach.getDataHandler());
                      message.setFileName(attach.getSubject() + ".ics");
                  }
              }
              // Put parts in message
              message.setContent(multipart);
              // SEND
  </code></pre>
  
  <p>}</p>