title: sending mail in groovy
body: <p><em>I have written this snippet of code in my program and it is successfully sending mail when I am passing file path in the attachment</em>(Ex:attachment = "/home/Aman/file.txt")* but it is throwing <strong>IOException while sending mail when I don't have to attach any file</strong>.* I have tried by applying condition before <em>messageBodyPart.attachFile(attachment)</em> but it is also throwing same exception.</p>
  
  <pre><code>def sendMail(String message, String attachment, String subject) {
      Properties properties = System.getProperties()
      properties.setProperty("mail.smtp.host", eMailSMTPHost)
      properties.setProperty("mail.smtp.port", eMailSMTPIPPort)
      Session session = Session.getDefaultInstance(properties)
      try{
          // Create a default MimeMessage object.
          MimeMessage msg = new MimeMessage(session)
          msg.setFrom(new InternetAddress(eMailSendFrom))
          eMailSendTo.split(',').each(){ item -&gt;      msg.addRecipient(Message.RecipientType.TO,
              new InternetAddress(item)    )
          }
          eMailSendCc.split(',').each(){ item -&gt; msg.addRecipient(Message.RecipientType.CC,
              new InternetAddress(item)    )
          }
          msg.setSubject(subject)
          BodyPart messageBodyPart = new MimeBodyPart()
          messageBodyPart.setContent(message,"text/html")
          Multipart multipart = new MimeMultipart()
          multipart.addBodyPart(messageBodyPart)
  
          messageBodyPart = new MimeBodyPart()
          messageBodyPart.attachFile(attachment)  
          multipart.addBodyPart(messageBodyPart)
  
          // Send the complete message parts
          msg.setContent(multipart)
          Transport.send(msg)     
          System.exit(0)
      } catch(RuntimeException e) {
          println e.getMessage()
      }
  }
  </code></pre>