C#: Send A Letter With SendLetter()

Instructions

    1. Create a new Console Application project and add a web reference
    2. Copy the entire code below, delete anything written in your application and paste the code there
    3. Replace the values of the Const lines with your details:
      • Replace MyUsername and MyPassword with your PostalMethods user details.
      • Replace MyDescription with your own text to help you identify this letter in your activity log (optional).
      • Replace MyFileExtension with the extension of the document you are using for this test.
      • Replace MyPathToFile with a path to your test PDF file. Remember, for method SendLetter you must use a document where the recipient’s address already appears so it will show through the envelope window. You can use one of our Sample Letter Documents.
      • MyWorkMode determines if your letter will be sent as Production, Development or will use your user’s default Work Mode setting. Read more about Production and Development Work Modes.
    4. Start your application. It will start, encode your document as Base64 and send a letter to the PostalMethods SendLetter Web Service method. If the response is a positive number, you will be able to see your letter in the PostalMethods Control Panel.

If the response is negative number, check the Web Service Status Codes section.

That’s it – you have successfully sent a letter to PostalMethods. Congratulations.

using System;
using System.IO;
 
namespace ConsoleApplication1
{
 
class Module1
{
 
        public static void Main()
        {
            const string MyUsername = "MyUsername";
            const string MyPassword = "MyPassword";
            const string MyDescription = "Sending a letter using C#";
            const string MyFileExtension = "pdf";
            const string MyPathToFile = "C:\\MyFile.pdf";
            const com.postalmethods.api.WorkMode MyWorkMode = com.postalmethods.api.WorkMode.Default;
 
            com.postalmethods.api.PostalWS objPM = new com.postalmethods.api.PostalWS();
            long lngResult = 0;
            //
            // Read your binary document. In this example: C:\MyFile.pdf
            //
            FileStream objFR = default(FileStream);
            objFR = new FileStream(MyPathToFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            byte[] B = new byte[objFR.Length];
            objFR.Read(B, 0, B.Length);
            objFR.Close();
            objFR = null;
            //
            // Invoke the SendLetter method
            //
            lngResult = objPM.SendLetter(MyUsername, MyPassword, MyDescription, MyFileExtension, B, MyWorkMode);
            //
            // Check response status
            //
            if (lngResult > 0)
            {
                //
                // A positive value means the message was successfully queued for processing.
                // The PostalMethods Letter ID is returned.
                //
                Console.WriteLine("LetterID is: " + lngResult);
            }
            else
            {
                //
                // A negative value means an error occurred.
                // See the PostalMethods Status Codes: http://www.postalmethods.com/statuscodes#webservice.
                //
                Console.WriteLine("Error is:" + lngResult);
            }
 
            // In debug mode, the following lines prevent your console application from closing automatically upon termination
            Console.WriteLine("Hit ENTER to terminate application");
            Console.ReadLine();
        }
    }
}
Send Letters
Securely + Reliably
Send letters securely and reliably from any business application. Use for invoices, quotes, and other important business communications. Enjoy special web-form to postal features and instant notifications.
Simple Process
Easy as 1-2-3
Experience the ease of our streamlined process - developed for efficiency! You submit your document by email or API. We automatically print, collate, insert, and stamp your letter. Your letter is delivered via standard postal service.
Pay As You Go
No Fuss. No Obligations.
Register for free with no obligations! Evaluate the service as long as you need. Once you are ready, set up our user-friendly pay-as-you option to use our service. No fuss. No on-going commitment. Just plain easy!