I recently had a customer who had a requirement to get their email via POP3 through a MID Server rather than allowing the ServiceNow instance network access to their email server.

It actually turned out to be pretty easy. All I needed was a MID Server Script Include:

Name: PopMail

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
var PopMail = Class.create();

PopMail.prototype = {
    initialize: function() {
  var Properties = Packages.java.util.Properties;

  var MailFolder = Packages.javax.mail.Folder;
  var MailMessage = Packages.javax.mail.Message;
  var MailSession = Packages.javax.mail.Session;
  var MailStore = Packages.javax.mail.Store;
  var result = '';

  var props = new Properties();

  var host = "mail.mydomain.com";
  var username = "me@mydomain.com";
  var password = "mySecurePassword";
  var provider = "pop3";

  var session = MailSession.getDefaultInstance(props, null);
  var store = session.getStore(provider);
  store.connect(host, username, password);

  var inbox = store.getFolder("INBOX");
  if (inbox == null) {
    gs.print("No INBOX");
  } else {
    inbox.open(MailFolder.READ_ONLY);

    var messages = inbox.getMessages(1,2);
    for (var i = 0; i < messages.length; i++) {
      result += "Message " + (i + 1);
      result += "\nMessage #: " + messages[i].getMessageNumber();    
      result += "\nFROM: " + getFromString(messages[i].getFrom());
      result += "\nTO: " + getRecipientsString(messages[i].getAllRecipients());
      result += "\nReceived: " + messages[i].getReceivedDate();
      result += "\nSent: " + messages[i].getSentDate();
      result += "\nSubject: " + messages[i].getSubject();
      result += "\nContent Type: " + messages[i].getContentType();
      result += "\nContent: " + messages[i].content;
      result += "\nContent: " + getContentText(messages[i].content, messages[i].getContentType());
      result += "\n========================================";
    }
    probe.setParameter("Output", result);
    inbox.close(false);
    store.close();
  }

  function getContentText(content, contentType){
    var retString = "";
    if(/.*multipart\/alternative.*/.test(contentType)){
      var count = content.getCount();
      retString += "\nNumber of Parts: " + count;
      for(var i=0; i<count; i++){
        var bodyPart = content.getBodyPart(i);
        retString += "\nPart " + i + ": " + escape(bodyPart.content);
      }
    }
  }
  function getRecipientsString(recipients){
    var retString = "";
    for(var i=0;i<recipients.length; i++){
      retString += recipients[i].toString() + ";";
    }
    return retString;
  }

  function getFromString(from){
    var retString = "";
    for(var i=0;i<from.length; i++){
      retString += from[i].toString() + ";";
    }
    return retString;  
  }

}
}

Then you just post the following message to the ECC Queue:

Agent: mid.server.YOURMIDSERVERNAME
Topic: JavascriptProbe
Name: PopMail
Queue: Output
State: ready
Payload:

1
2
3
4
5
<?xml version="1.0" encoding="UTF-8"?>
<parameters>
  <parameter name="script" value="var req = new PopMail();">
  </parameter>
</parameters>

The resulting ECC Queue record is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8"?>
<results probe_time="2781">
  <result>
    <output>org.mozilla.javascript.Undefined@19067e5</output>
  </result>
  <parameters>
    <parameter name="topic" value="JavascriptProbe" />
    <parameter name="queue" value="output" />
    <parameter name="sys_id" value="ce8a78160a0a3ca700cbee4af5f7dbd8" />
    <parameter name="state" value="ready" />
    <parameter name="Output"
      value="Message 1\nMessage #: 1\nFROM: Majon International &lt;majon@majonpromotion.com&gt;;\nTO: Mark Masterson &lt;advertising@mydomain.com&gt;;\nReceived: null\nSent: Sat Jan 22 06:33:27 MST 2011\nSubject: Your Blog Strategy\nContent Type: multipart/alternative; boundary=&quot;_----------=_1295703207141407065216&quot;\nContent: javax.mail.internet.MimeMultipart@a34783\nContent: undefined\n========================================Message 2\nMessage #: 2\nFROM: SmallBusinessNewz &lt;smallbusinessnewz@ientrynetwork.net&gt;;\nTO: advertising@mydomain.com;\nReceived: null\nSent: Sun Jan 23 11:35:07 MST 2011\nSubject: Small Business Climate Making Turnaround\nContent Type: multipart/alternative; boundary=4e2e745f2ea51e3\nContent: javax.mail.internet.MimeMultipart@d37b87\nContent: undefined\n========================================" />
    <parameter name="script" value="var req = new PopMail();" />
    <parameter name="agent" value="mid.server.RadRoadTrips.com" />
    <parameter name="ecc_queue" value="ce8a78160a0a3ca700cbee4af5f7dbd8" />
    <parameter name="sequence" value="12dce8a78160000001" />
    <parameter name="name" value="PopMail" />
    <parameter name="table_name" value="ecc_queue" />
  </parameters>
</results>

Of course, code is just a prototype. It will take the first 10 messages from the server. It currently leaves the messages on the server.

You would really want to bust out the results into more complex XML so that you could parse and act upon it.