2



atm I'm implementing a custom solution in our SP 2010 farm to receive emails on an specific custom list.
With the following code it's possible to add one entry, with an email-body contains one line with values, seperated by ';' - i.e. (test;entry;in;our;sharepoint)
But now it's not cool, related to performance sending hundreds/thousands of mails in a minute to our sharepoint and insert every single item.
So I implemented in the same solution sth. to receive an email with multiple lines in a body (lines currently seperated by '~' and the single line still seperated by ';').

But there I can find the following error in the SharePoint log:

07/07/2015 12:02:56.85  OWSTIMER.EXE (0x13A4)                       0x07F0  SharePoint Foundation           E-Mail                          6873    Warning     An error occurred while processing the incoming e-mail file C:\inetpub\mailroot\Drop\15ed460901d0b89c00000008.eml. The error was: Object reference not set to an instance of an object..    ca05878c-e64b-4a32-bdf0-1916a8e8464a

And here's the code of my solution:

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace incomail_proxylist.incomail
{
    public class incomail : SPEmailEventReceiver
    {
        public override void EmailReceived(SPList list, SPEmailMessage emailMessage, String receiverData)
        {
            base.EmailReceived(list, emailMessage, receiverData);
            string str = emailMessage.PlainTextBody;
            string booli = emailMessage.Headers["subject"];

            if (booli.Contains("COMPLETE_LIST"))
            {    
                string[] entries = str.Split('~'); //new string[] { "\r\n", "\n" }, StringSplitOptions.None
                for (int i = 0; i <= entries.Length; i++)
                {
                    SPListItem newItem = list.Items.Add();
                    string[] arr = entries[i].Split(';');

                    newItem["Title"] = arr[0];
                    newItem["Host"] = arr[1];
                    newItem["Internal_IP"] = arr[2];
                    newItem["External_IP"] = arr[3];
                    newItem["Incoming_URL"] = arr[4];
                    newItem["Forwarding_URL"] = arr[5];
                    newItem["SSL"] = arr[6];
                    newItem["Status"] = arr[7];
                    newItem["ConfigPath"] = arr[8];

                    newItem.Update();
                }
            }
            else
            {
                SPListItem newItem = list.Items.Add();
                string[] arr = str.Split(';');

                newItem["Title"] = emailMessage.Headers["subject"];
                newItem["Host"] = arr[0];
                newItem["Internal_IP"] = arr[1];
                newItem["External_IP"] = arr[2];
                newItem["Incoming_URL"] = arr[3];
                newItem["Forwarding_URL"] = arr[4];
                newItem["SSL"] = arr[5];
                newItem["Status"] = arr[6];
                newItem["ConfigPath"] = arr[7];

                newItem.Update();
            }
        }
    }
}

Edit: Adding an email like requested:

x-sender: server-a@domain.com
x-receiver: server-b@domain.com
Received: from exchange.domain.com ([ip of server]) by sharepoint.domain.com over TLS secured channel with Microsoft SMTPSVC(7.5.7600.16385);
     Tue, 7 Jul 2015 10:53:37 +0200
Received: from exchange2.domain.com (ip of server) by
 exchange.domain.com (ip of server) with Microsoft SMTP Server (TLS) id
 14.3.224.2; Tue, 7 Jul 2015 10:53:32 +0200
Received: from server-a.domain.com (ip of server) by
 exchange2.domain.com (ip of server) with Microsoft SMTP Server id
 14.3.224.2; Tue, 7 Jul 2015 10:53:32 +0200
Message-ID: <686044.912410097-sendEmail@server-a>
From: Server-A <server-a@domain.com>
To: "server-b@domain.com" <server-b@domain.com>
Subject: COMPLETE_LIST
Date: Tue, 7 Jul 2015 08:53:32 +0000
X-Mailer: sendEmail-1.56
MIME-Version: 1.0
Content-Type: multipart/related;
    boundary="----MIME delimiter for sendEmail-943467.83247693"
Return-Path: server-a@domain.com
X-OriginalArrivalTime: 07 Jul 2015 08:53:37.0598 (UTC) FILETIME=[69F27DE0:01D0B892]

------MIME delimiter for sendEmail-943467.83247693
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

title;server-a.domain.com;ip1;ip2;incomingURL;forwardingURL;active;/path/to/config-file~
title;server-a.domain.com;ip1;ip2;incomingURL;forwardingURL;active;/path/to/config-file~
title;server-a.domain.com;ip1;ip2;incomingURL;forwardingURL;active;/path/to/config-file~
title;server-a.domain.com;ip1;ip2;incomingURL;forwardingURL;active;/path/to/config-file~
title;server-a.domain.com;ip1;ip2;incomingURL;forwardingURL;active;/path/to/config-file~
title;server-a.domain.com;ip1;ip2;incomingURL;forwardingURL;active;/path/to/config-file~
title;server-a.domain.com;ip1;ip2;incomingURL;forwardingURL;active;/path/to/config-file~
title;server-a.domain.com;ip1;ip2;incomingURL;forwardingURL;active;/path/to/config-file~

------MIME delimiter for sendEmail-943467.83247693--

This is an example for an email like our exchange receives it.
I hided our internal information like IPs and hostnames.
But the structure is exactly the same!
Thanks in advance.

  • Please provide one of your emails. – theChrisKent Jul 7 '15 at 12:15
  • eMail code is provided above.. Just hided internal information like ips, hostnames, URLs - the strucutre is exactly the same! – Flaash Jul 8 '15 at 9:29
1

Based on your sample email, you are missing the "SSL" portion within your input.

When you are splitting the entries into arr you are only getting back 8 items rather than 9. Attempting to access arr[8] when it doesn't exist will cause Object reference not set to an instance of an object.

You should add some validation regarding length on the split lines. Something like

for (int i = 0; i < entries.Length; i++)
{
    string[] arr = entries[i].Split(';');
    if(arr.Length == 9)
    {
        SPListItem newItem = list.Items.Add();

        newItem["Title"] = arr[0];
        newItem["Host"] = arr[1];
        newItem["Internal_IP"] = arr[2];
        newItem["External_IP"] = arr[3];
        newItem["Incoming_URL"] = arr[4];
        newItem["Forwarding_URL"] = arr[5];
        newItem["SSL"] = arr[6];
        newItem["Status"] = arr[7];
        newItem["ConfigPath"] = arr[8];

        newItem.Update();
    }
}
  • I edited my code but before I can query the if(arr.Lenght == 9) I have to create this arr :p So now in my for loop at first the arr will be created and then the if query will be done. But this doesn't solve my issue - in SP log there's still the error which i provided above.. – Flaash Jul 8 '15 at 13:23
  • @Flaash whoops! I've edited my code above. Were you able to confirm if you are missing the proper number of fields in the multiline emails? – theChrisKent Jul 8 '15 at 13:27
  • Also, I just realized you are doing an <= in the for loop. Change this to just < (see above) and things should start working (assuming you send the right number of fields in your email). – theChrisKent Jul 8 '15 at 13:29
  • damn. I was so fixed, the problem should be in my custom solution, so I haven't done a look in my bash script :D Now I adjusted the script and writes the "SSL-varable" in my email and now it works. Thanks for the help! In future I should read with open eyes :D Have a nice day :) – Flaash Jul 8 '15 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.