Question

I am have sxtracted json resonse from Yahoo mail webservice. After that i parsed using play json library.

Now i am not not able to iterate over it and construct list of mail objects with sender name, subject, ccList etc.

Mail object i am trying to construct is : List(EmailMessage(subject, recvdDate,body1,sender,recipientsList))

I went through the documentation at play website. But had difficulty in understanding them as a learner in scala. Can someone help me how can we achieve this using case classes.

Response i got form web service :

{
    "result": {
        "folder": {
            "folderInfo": {
                "fid": "Sent",
                "name": "Sent"
            },
            "total": 3,
            "unread": 0,
            "size": 943522,
            "isSystem": true
        },
        "total": 3,
        "code": [],
        "message": [
            {
                "mid": "2_0_0_2_3196_ACfai2IAACqmUwNFtgAAAKAUN2U",
                "receivedDate": 1392723382,
                "subject": "test mail",
                "xapparentlyto": "",
                "hasBlockedImages": false,
                "flags": {
                    "isReplied": 0,
                    "isFlagged": 0,
                    "isRead": 1,
                    "isDraft": 0,
                    "isForwarded": 0,
                    "isHam": 0,
                    "isSpam": 0,
                    "hasAttachment": 0,
                    "isRecent": 1,
                    "inAddressBook": 0
                },
                "from": {
                    "email": "rajeevkumar.kallempudi@yahoo.com",
                    "name": "Rajeev Kumar Kallempudi"
                },
                "to": [
                    {
                        "email": "rajeevprasanna@gmail.com",
                        "name": "rajeevprasanna@gmail.com"
                    }
                ],
                "replyto": [
                    {
                        "email": "rajeevkumar.kallempudi@yahoo.com",
                        "name": "Rajeev Kumar Kallempudi"
                    }
                ],
                "cc": [],
                "bcc": [],
                "domainkey": false,
                "messageId": "<1392723382.76715.YahooMailNeo@web160105.mail.bf1.yahoo.com>",
                "inReplyTo": "",
                "references": "",
                "sender": "rajeevkumar.kallempudi@yahoo.com",
                "part": [
                    {
                        "partId": "HEADER",
                        "type": "x-unknown",
                        "subtype": "",
                        "typeParams": "",
                        "disposition": "",
                        "dispParams": "",
                        "encoding": "7bit",
                        "filename": "",
                        "size": 1474,
                        "contentId": "",
                        "isTruncated": false,
                        "hasBlockedImages": false,
                        "referencedInline": false
                    }                     
                ]
            },
            {
                "mid": "2_0_0_2_2463_ACfai2IAAAdpUwM1NwAAAD0sJOA",
                "receivedDate": 1392719159,
                "subject": "passage1",
                "xapparentlyto": "",
                "hasBlockedImages": false,
                "flags": {
                    "isReplied": 0,
                    "isFlagged": 0,
                    "isRead": 1,
                    "isDraft": 0,
                    "isForwarded": 0,
                    "isHam": 0,
                    "isSpam": 0,
                    "hasAttachment": 0,
                    "isRecent": 1,
                    "inAddressBook": 0
                },
                "from": {
                    "email": "rajeevkumar.kallempudi@yahoo.com",
                    "name": "Rajeev Kumar Kallempudi"
                },
                "to": [
                    {
                        "email": "rajeevkumar.kellmpudi@yahoo.com",
                        "name": ""
                    }
                ],
                "replyto": [
                    {
                        "email": "rajeevkumar.kallempudi@yahoo.com",
                        "name": "Rajeev Kumar Kallempudi"
                    }
                ],
                "cc": [],
                "bcc": [],
                "domainkey": false,
                "messageId": "<1392719159.63976.YahooMailNeo@web160106.mail.bf1.yahoo.com>",
                "inReplyTo": "",
                "references": "",
                "sender": "rajeevkumar.kallempudi@yahoo.com",
                "part": [
                    {
                        "partId": "HEADER",
                        "type": "x-unknown",
                        "subtype": "",
                        "typeParams": "",
                        "disposition": "",
                        "dispParams": "",
                        "encoding": "7bit",
                        "filename": "",
                        "size": 1949,
                        "contentId": "",
                        "isTruncated": false,
                        "hasBlockedImages": false,
                        "referencedInline": false
                    },
                    {
                        "partId": "TEXT",
                        "subtype": "alternative",
                        "type": "multipart",
                        "typeParams": "boundary=\"-827237569-990831377-1392719159=:63976\"",
                        "disposition": "",
                        "dispParams": "",
                        "encoding": "7bit",
                        "filename": "",
                        "size": 11623,
                        "contentId": "",
                        "isTruncated": false,
                        "hasBlockedImages": false,
                        "referencedInline": false
                    }                     
                ]
            } 
        ]
    },
    "error": null
}
Was it helpful?

Solution

If you just query Yahoo's messages you won't get the content of emails, so you should use a query that looks more like this. Please note that this will query all of an account's messages, including read or archived ones.

select
  message.subject,
  message.receivedDate,
  message.from,
  message.to,
  message.cc,
  message.bcc,
  message.part.text
from ymail.msgcontent
where (fid,mids) in (
  select
    folder.folderInfo.fid,
    mid
  from ymail.messages
)

Once you have that you just need to use the JSON pick element features described in Play's documentation. It should look something like this:

import java.util._

case class EmailMessage(
  subject      : String,
  receivedDate : Date,
  body         : String,
  sender       : String,
  recipients   : List[String]
)

def yahooToEmailMessage(json:JsValue) = {

  val subject = json / "result" / "message" / "subject"
  val body = json / "result" / "message" / "part" / "text"
  val ts = (json / "result" / "message" / "receivedDate").toLong

  val receivedDate = new Date(ts * 1000L)

  val sender = getRecipient(json / "result" / "message" / "to")
  val to  = getRecipients(json / "result" / "message" / "to")
  val cc  = getRecipients(json / "result" / "message" / "cc")
  val bcc = getRecipients(json / "result" / "message" / "bcc")

  val recipients = to ::: cc ::: bcc

  EmailMessage(
    subject,
    receivedDate,
    body,
    sender,
    recipients
  )
}

def getRecipient(recipient:JsValue):List[String] = {

  val name  = recipient / "name"
  val email = recipient / "email"

  name + " <" + email + ">"
}

def getRecipients(array:JsArray):List[String] = {
  array.foldLeft(List[String]()) {
    (list,recipient) => list ::: List(getRecipient(recipient))
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top