import javax.mail.*
import javax.mail.internet.*
import groovy.json.JsonSlurper
import groovy.json.JsonOutput
 
 /*
    Request:
    {
      "sender":"info.apinizer",
      "recipients":"info@apinizer.com",
      "infoRecipients":null,
      "secretRecipients":null,
      "subject":"test",
      "content":"test",
      "contentType":"text/html;charset=utf-8"
    }
     
    Response:
    {
      "isTransactionSuccess":true,
      "errorMessage":"",
      "errorCode":""
    }
 */
  
try {
   
    requestHeaderMapFromClient.put('statusCode',0);
 
    def jsonSlurper = new JsonSlurper()
    def jsonMessage = jsonSlurper.parseText(requestBodyTextFromClient)
  
 
    String from=jsonMessage.sender;
    if(from.equals("info.apinizer")){
        from="info@apinizer.com";
    }else if(from.equals("apinizer.error")){
        from="warning@apinizer.com";
    }
    else{
          
        statusCode=400;
        requestErrorMessageFromClient = JsonOutput.toJson([isTransactionSuccess: false, errorMEssage: 'Incorrect sender information', errorCode:'01-ERR-02'])
        return;
    } 
 
 
    String to=jsonMessage.recipients;
    String cc=jsonMessage.infoRecipients;
    String bcc=jsonMessage.secretRecipients;
 
    if(!to?.trim()&& !cc?.trim()&&  !bcc?.trim()){
        requestErrorMessageFromClient = JsonOutput.toJson([isTransactionSuccess: false, errorMEssage: 'At least one recipient must be added', errorCode:'01-ERR-03'])
        statusCode=400;
        return;
    }
 
    String subject=jsonMessage.subject;
    if(subject?.trim()){
        subject=""
    }
 
    String content=jsonMessage.content;
    if(content?.trim()){
        content=""
    }
 
    String mimeType=jsonMessage.contentType;
    if(mimeType?.trim()){
        mimeType="text/html;charset=utf-8"
    }
 
    // Init constants of sender email account.
    String email = "apinizer.error"
    String password = ""
    String host = "mail.apinizer.com"
    String port = "25" //  "465" "587"
 
    // Set up properties.
    Properties props = System.getProperties()
    props.put("mail.smtp.user", email)
    props.put("mail.smtp.host", host)
    props.put("mail.smtp.port", port)
    props.put("mail.smtp.auth", "false")
    props.put("mail.smtp.starttls.enable","false")
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory")
    props.put("mail.smtp.ssl.trust", host) // Change host to "*" if you want to trust all host.
 
    Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(email, password);
            }
        });
         
    // Set up message.
    MimeMessage message = new MimeMessage(session)
    message.setFrom(new InternetAddress(from))
    if(to?.trim()){
        message.addRecipients(Message.RecipientType.TO, new InternetAddress(to))
    }
    if(cc?.trim()){
        message.addRecipients(Message.RecipientType.CC, new InternetAddress(cc))
    }
    if(bcc?.trim()){
        message.addRecipients(Message.RecipientType.BCC, new InternetAddress(bcc))
    }
    message.setSubject(subject)
    message.setContent(content, mimeType)
 
    try {
        // Send mail.
        Transport.send(message )
    } catch (MessagingException e) {
        e.printStackTrace();
        statusCode=500;
        requestErrorMessageFromClient = JsonOutput.toJson([isTransactionSuccess: false, errorMessage: 'Error occured while sending mail:' + e.getMessage(), errorCode:'01-ERR-04'])
        return;
    }
    statusCode=200;
    requestErrorMessageFromClient = JsonOutput.toJson([isTransactionSuccess: true, errorMessage: '', errorCode:''])
 
} catch (MessagingException e) {
    e.printStackTrace();
    statusCode=500;
    requestErrorMessageFromClient = JsonOutput.toJson([isTransactionSuccess: false, errorMessage: 'Error occured while sending mail :' + e.getMessage(), errorCode:'01-ERR-05'])
    return;
}
GROOVY