Announcement

Collapse
No announcement yet.

Reading email and attachement with Mirth

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Reading email and attachement with Mirth

    Hi all,

    I'd like to share a javascript reader with the community. The aim is to read email with attachement using the imap protocol.

    The javascript reader below do :

    - connection with the mail server
    - loading the inbox folder
    - reading messages
    - parsing mail rows (date, from, subject, ...)
    - read the 1st attachement such as an hl7 flat file

    Note :

    - it won't work with encrypted email
    - the content might be different depending on using text mail or MIME mail

    Best regards


    var host = "mailserver";
    var user = "xxx";
    var pwd = "yyy";

    var list = new Packages.java.util.ArrayList();

    var properties = new Packages.java.util.Properties();

    properties.setProperty("mail.imap.debug", "true");
    properties.setProperty("mail.imap.auth.plain.disab le", "true");
    properties.setProperty("mail.imap.port", "143");

    var session = new Packages.javax.mail.Session.getInstance(properties );

    var store = session.getStore("imap");
    store.connect(host, user, pwd);

    var folder = store.getFolder("inbox");
    folder.open(2);

    var ListMail = folder.getMessages();

    for (i = 0 ; i < ListMail.length ; i++)
    {
    var Date = ListMail[i].getSentDate();
    var From = ListMail[i].getFrom()[0];
    var Subject = ListMail[i].getSubject();
    var Content = ListMail[i].getContent();

    var multipart = ListMail[i].getContent();
    var bodyPart = multipart.getBodyPart(1);
    var is = new Packages.java.io.InputStream(bodyPart.getInputStre am());
    var isr = new Packages.java.io.InputStreamReader(is);
    var br = new Packages.java.io.BufferedReader(isr);

    var pj = "";
    var lg = "";
    while ((lg = br.readLine()) != null)
    pj = pj + lg;

    var sMsg = "Date : " + Date + "\nFrom : " + From + "\nSubject : " + Subject + "\nContent : " + Content + "\nFile : " + pj;
    list.add(sMsg);
    }

    folder.close(true);
    store.close();

    return list;

  • #2
    interesting information!!! thank you!!!
    It is not truly handy to invent novel drafts. You shall use much beneficial literature and create your keen work. Still products will be not really progressive. It usually happens mutedly with me. I ordinarily have many tips, amusing thoughts; therefore I could to unveil them in desirable discourse, that’s why frequently I look the support of .

    Comment


    • #3
      hi! tried to use the provided code, but I get the following error message.

      "Error in connector "JavaScript Reader" at Javascript:
      Error on line 32: missing ) after argument list
      (e139bd2e-a606-41bf-9e78-ff0b25a743d5#32)"


      Line 32 is this:

      var is = new Packages.java.io.InputStream(bodyPart.getInputStre am());

      I don't see what's wrong with this. can anyone help me out here?

      Comment


      • #4
        I think it's a forum trouble with the quote. There's a blank in the "getInputStream".

        Comment


        • #5
          Hi, I'm trying to use this code to connect to imap, I can get it to connect but it reads all the emails, not just the unread ones. I've found this code:
          var ft = new Packages.javax.mail.search.FlagTerm(new packages.javax.mail.Flags(Flags.Flag.Seen), false);

          But it gives me the error:
          ReferenceError "Flags" is not defined.

          Can anyone help with this, is there a better way to only get unread messages?

          Comment


          • #6
            I am getting below error any suggestion?

            TypeError: Cannot find function getBodyPart in object <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html dir="" lang="" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

            Comment

            Working...
            X