Home > Uncategorized > #Docmail #API part 2 #Custom #envelopes

#Docmail #API part 2 #Custom #envelopes


I wrote an earlier post on Docmail, where I demonstrated how to send physical mail via C#, one of the things I found, was that it sends a windowed envelope, and overlays the address on the first page of the document you want to send. If you’ve designed your document around this, it shouldn’t be a problem – and you can always add an extra blank page to the start of your PDF (at an extra cost), however, the correct solution, is to send a custom envelope, with the address on the outside.

    public class DocMailAddress
        public string FirstName;
        public string Surname;
        public string Address1;
        public string Address2;
        public string Address3;
        public string Address4;
        public string FullName {
                return FirstName + " " + Surname;

    public class DocMailHandler

        static string sUsr = "username";

        static string sPwd = "password";

        static string sReturnMessageFormat = "Text";

        static string sAppName = "your app";
        static string sMailingName = "your app";

        static string sMailingDesc = "your app";

        static string sEmailOnError = "info@you.com";
        static string sEmailOnSuccess = "info@you.com";

        public static void SendFile(DocMailAddress Address, string PDF)
            DMWS.DMWSSoapClient oService = new DMWS.DMWSSoapClient();
            Hashtable oResultHash = default(Hashtable);
            Guid gMailingGUID = default(Guid);
            Guid gTemplateGUID = default(Guid);
            // Create a mailing
            oResultHash = ResultHashTable(oService.CreateMailing(sUsr, sPwd, sAppName, "A4Letter", sMailingName, sMailingDesc, false, false, "StandardProofPerAddress", false,
            true, DateTime.Now, "", "Full Name", "", "CustomNoPanel", sReturnMessageFormat));
            //get MailingGUID - required to continue the mailing
            gMailingGUID = GetResultGUID(oResultHash, "MailingGUID");

            // Add envelope
            oResultHash =
                ResultHashTable(oService.AddTemplateFromLibrary(sUsr, sPwd, gMailingGUID, "Outer envelope", 1,

            // Add a template
            oResultHash = ResultHashTable(oService.AddTemplateFile(sUsr, sPwd, gMailingGUID, "Deemtree", "Deemtree", OpenFileAsByteArray(PDF), "A4Letter", true, "Arial 10", "",
            "", false, false, "", "", false, 1, 1, "", false,
            //get templateGUID  - can be passed in to other calls
            gTemplateGUID = GetResultGUID(oResultHash, "TemplateGUID");

            oResultHash = ResultHashTable(oService.AddAddress(sUsr, sPwd, gMailingGUID, Address.Address1, Address.Address2, Address.Address3, Address.Address4, "", "", true, "", Address.FirstName, Address.Surname, Address.FullName, "", "", "", "", "", "", "", "", "", "", "", 0, 0, 0, "", "", "", "", "", "", "", "", "", "", ""));
            //The following code submits a mailing for processing, automatically approving the order (Submit=true, PartialProcess=false)
            oResultHash = ResultHashTable(oService.ProcessMailing(sUsr, sPwd, gMailingGUID, sAppName, true, false, 0, "", "Topup", true,
            sEmailOnError, sEmailOnSuccess, "", "", sReturnMessageFormat));

            //The following code checks the status, looping until the proof is ready and then saves the proof file to disk
                oResultHash = ResultHashTable(oService.GetStatus(sUsr, sPwd, gMailingGUID, sReturnMessageFormat));
                var strStatus = Convert.ToString(oResultHash["Status"]);
                bool blnDone = false;
                switch (strStatus)
                    case "Error in processing":
                        blnDone = true;
                        // Error trapping code add here ...
                    case "Mailing submitted":
                    case "Mailing processed":
                    case "Partial processing complete":
                        // You Save the proof to disk
                        blnDone = true;
                if (blnDone) break;
                // Wait one second between each poll
            } while (true);


        #region "Support Functions"
        //The following function convertes a text result string into a hash table for easy access to the return data: 
        private static Hashtable ResultHashTable(string ResultData)
            Hashtable oHashTable = new Hashtable();
            if (!string.IsNullOrEmpty(ResultData))
                foreach (string sLine in ResultData.Split(new[] { '\n' }))
                    if (!string.IsNullOrEmpty(sLine))
                        int iIndex = sLine.IndexOf(": ");
                        string sKey = sLine.Substring(0, iIndex);
                        string sData = sLine.Substring(iIndex + 2, sLine.Length - iIndex - 2);

                        oHashTable.Add(sKey, sData);
            return oHashTable;

        private static void CheckError(Hashtable oResultHash)
            if (oResultHash.ContainsKey("Error code"))
                throw new Exception(string.Format("Error {0}: {1} - {2}", oResultHash["Error code"], oResultHash["Error code string"], oResultHash["Error message"]));

        private static Guid GetResultGUID(Hashtable oResultHash, string sField)
            Guid functionReturnValue = default(Guid);
            functionReturnValue = new System.Guid(Convert.ToString(oResultHash[sField]));
            if (functionReturnValue == Guid.Empty)
                throw new Exception(sField + " Contained a blank or invalid GUID");
            return functionReturnValue;

        //The following function opens a file as a byte array: 
        private static byte[] OpenFileAsByteArray(string FilePath)
            System.IO.FileStream oFileStream = System.IO.File.OpenRead(FilePath);
            int iBytes = Convert.ToInt32(oFileStream.Length);
            byte[] oByteArray = new byte[iBytes];
            oFileStream.Read(oByteArray, 0, iBytes);
            return oByteArray;

        //The following sub routine saves a byte array to a file: 
        private static void SaveFromByteArray(byte[] FileData, string FilePath)
            using (System.IO.FileStream oFileStream = new System.IO.FileStream(FilePath, System.IO.FileMode.Create))
                oFileStream.Write(FileData, 0, FileData.Length);

Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: