Scala SDK Samples - Send Mail Operations
Get Email Addresses
package com.zoho.crm.sample.sendmail
import com.zoho.crm.api.inventorytemplates.InventoryTemplate
import java.util
import com.zoho.crm.api.sendmail.APIException
import com.zoho.crm.api.sendmail.ActionWrapper
import com.zoho.crm.api.sendmail.BodyWrapper
import com.zoho.crm.api.sendmail.Mail
import com.zoho.crm.api.sendmail.ResponseWrapper
import com.zoho.crm.api.sendmail.SendMailOperations
import com.zoho.crm.api.sendmail.SuccessResponse
import com.zoho.crm.api.sendmail.UserAddress
import scala.collection.mutable.ArrayBuffer
object SendMail {
/*
* get Email Addresses
* This method is used to get Email Addresses
* @throws Exception
*/
@throws[Exception]
def getEmailAddresses(): Unit = {
//Get instance of SendMailOperations Class
val sendMailsOperations = new SendMailOperations()
//Call getEmailAddresses method
val responseOption = sendMailsOperations.getEmailAddresses()
if (responseOption.isDefined) { //check response
val response = responseOption.get
println("Status Code: " + response.getStatusCode)
if (util.Arrays.asList(204, 304).contains(response.getStatusCode)) {
println(if (response.getStatusCode == 204) "No Content"
else "Not Modified")
return
}
//Check if expected response is received
if (response.isExpected) { //Get object from response
val responseHandler = response.getObject
responseHandler match {
case responseWrapper: ResponseWrapper => //Get the received ResponseWrapper instance
//Get the list of obtained UserAdress instances
val userAddresses = responseWrapper.getFromAddresses
for (userAddress <- userAddresses) {
//Get the ID of each UserAdress
println("UserAdress ID: " + userAddress.getId())
//Get the Email of each UserAdress
println("UserAdress Email: " + userAddress.getEmail())
//Get the Type of each UserAdress
println("UserAdress Type: " + userAddress.getType())
//Get the UserName of each UserAdress
println("UserAdress UserName: " + userAddress.getUserName())
//Get the Default of each UserAdress
println("UserAdress Default: " + userAddress.getDefault())
}
case _ => //Check if the request returned an exception
responseHandler match {
case exception: APIException => //Get the received APIException instance
//Get the Status
println("Status: " + exception.getStatus.getValue)
//Get the Code
println("Code: " + exception.getCode.getValue)
println("Details: ")
exception.getDetails.foreach(entry => {
println(entry._1 + ": " + entry._2)
})
//Get the Message
println("Message: " + exception.getMessage.getValue)
case _ =>
}
}
}
else { //If response is not as expected
//Get model object from response
val responseObject = response.getModel
//Get the response object's class
val clas = responseObject.getClass
//Get all declared fields of the response class
val fields = clas.getDeclaredFields
for (field <- fields) { //Get each value
println(field.getName + ":" + field.get(responseObject))
}
}
}
}
}
class SendMail {}
Send Mail
package com.zoho.crm.sample.sendmail
import com.zoho.crm.api.inventorytemplates.InventoryTemplate
import java.util
import com.zoho.crm.api.sendmail.APIException
import com.zoho.crm.api.sendmail.ActionWrapper
import com.zoho.crm.api.sendmail.BodyWrapper
import com.zoho.crm.api.sendmail.Mail
import com.zoho.crm.api.sendmail.ResponseWrapper
import com.zoho.crm.api.sendmail.SendMailOperations
import com.zoho.crm.api.sendmail.SuccessResponse
import com.zoho.crm.api.sendmail.UserAddress
import scala.collection.mutable.ArrayBuffer
object SendMail {
/*
* send Mail
* This method is used to send Mail
* @param recordID The id of the record
* @param moduleAPIName The api name of the module
* @throws Exception
*/
@throws[Exception]
def sendMail(recordID: Long, moduleAPIName: String): Unit = {
//Get instance of SendMailOperations Class
val sendMailOperations = new SendMailOperations()
val mails = new ArrayBuffer[Mail]
val mail = new Mail()
val from = new UserAddress()
from.setUserName(Option("user"))
from.setEmail(Option("raja.k@zohocorp.com"))
mail.setFrom(Option(from))
val toAddress = new ArrayBuffer[UserAddress]
val to = new UserAddress()
to.setUserName(Option("user2"))
to.setEmail(Option("rajaprabu26@gmail.com"))
toAddress.addOne(to)
mail.setTo(toAddress)
mail.setSubject(Option("Mail subject"))
mail.setContent(Option("Consent form link REGARDS, AZADMIN "))
// mail.setConsentEmail(true);
mail.setMailFormat(Option("html"))
val template = new InventoryTemplate()
template.setId(Option(3477061000000174003l))
mail.setTemplate(Option(template))
mails.addOne(mail)
val bodyWrapper = new BodyWrapper()
bodyWrapper.setData(mails)
//Call createSendMail method that takes BodyWrapper instance as parameter
val responseOption = sendMailOperations.sendMail(recordID, moduleAPIName, bodyWrapper)
if (responseOption.isDefined) {
val response = responseOption.get
println("Status Code: " + response.getStatusCode)
if (response.isExpected) {
val actionHandler = response.getObject
actionHandler match {
case actionWrapper: ActionWrapper =>
//Get the list of obtained ActionResponse instances
val actionResponses = actionWrapper.getData()
for (actionResponse <- actionResponses) {
actionResponse match {
case successResponse: SuccessResponse => //Get the received SuccessResponse instance
println("Status: " + successResponse.getStatus.getValue)
println("Code: " + successResponse.getCode.getValue)
println("Details: ")
successResponse.getDetails.foreach(entry => {
println(entry._1 + ": " + entry._2)
})
println("Message: " + successResponse.getMessage.getValue)
case exception: APIException =>
println("Status: " + exception.getStatus.getValue)
println("Code: " + exception.getCode.getValue)
println("Details: ")
exception.getDetails.foreach(entry => {
println(entry._1 + ": " + entry._2)
})
println("Message: " + exception.getMessage.getValue)
case _ =>
}
}
case exception: APIException =>
println("Status: " + exception.getStatus.getValue)
println("Code: " + exception.getCode.getValue)
println("Details: ")
exception.getDetails.foreach(entry => {
println(entry._1 + ": " + entry._2)
})
println("Message: " + exception.getMessage.getValue)
case _ =>
}
}
else {
val responseObject = response.getModel
val clas = responseObject.getClass
val fields = clas.getDeclaredFields
for (field <- fields) {
println(field.getName + ":" + field.get(responseObject))
}
}
}
}
}
class SendMail {}