VB.NET: Send A Letter Using A Template

Instructions

  1. First, you need to upload a letter template to be used with merge data provided in your Web Service request. Download the invoice template sample (.MHT) and upload it to your account using the UploadFile method (see code sample).
  2. Create a new Console Application project and add a web reference.
  3. Copy the entire code below, delete anything written in your application and paste the code there.
  4. 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 a reference to the template you uploaded in step #1.
    • Do not change MyXMLData.
    • 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.
    • Use the existing postal recipient or replace with another valid postal address. Leave an empty string ("") where no value is needed.
  5. Start your application. It will start, encode your document as Base64 and send a letter to the PostalMethods SendLetterAndAddress 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 using a pre-uploaded template and by providing your merge data. Congratulations.

Imports System.IO
 
Module module1
    Sub Main()
        Const MyUsername As String = "MyUsername"
        Const MyPassword As String = "MyPassword"
        Const MyDescription As String = "Sending an invoice template using VB.NET"
        Const MyFileExtension As String = "MyTemplate:PurchaseInvoice.mht"
        Const MyXMLData As String = "<MergeData><ReturnAddress>ABC Hardware Inc.<br />93S. Jackson Street<br />Seattle  WA 98104-2818<br />United States</ReturnAddress><Date>July 09, 2009</Date><InvoiceID>8830023-RRX-3200985</InvoiceID><Payment>Net 30</Payment><MakeFor>ABC Hardware</MakeFor><ProductID1>ED-2343</ProductID1><Description1>Paslode Cordless Framing Nailer, 30 Degree Paper Collated</Description1><Quantity1>2</Quantity1><Price1>379.21</Price1><Amount1>758.42</Amount1><ProductID2>FR-9485</ProductID2><Description2>Porta-Nails 2 In. Flooring Nails Master Carton (1000 items)</Description2><Quantity2>10</Quantity2><Price2>11.20</Price2><Amount2>112.00</Amount2></MergeData>"
        Const MyWorkMode As com.postalmethods.api.WorkMode = com.postalmethods.api.WorkMode.Default
        Const MyAttention1 As String = "George Washington"
        Const MyAttention2 As String = ""
        Const MyCompanyName As String = "The White House"
        Const MyAddress1 As String = "1600 Pennsylvania Ave NW"
        Const MyAddress2 As String = ""
        Const MyCity As String = "Washington"
        Const MyState As String = "DC"
        Const MyPostalCode As String = "20500"
        Const MyCountry As String = "United States of America"
 
        Dim objPM As New com.postalmethods.api.PostalWS()
        Dim lngResult As Long
        '
        ' Encode your XML data as binary
        '
        Dim B As Byte() = System.Text.Encoding.UTF8.GetBytes(MyXMLData)
        '
        ' Invoke the SendLetterAndAddress method
        '
        lngResult = objPM.SendLetterAndAddress( _
                                                 MyUsername, _
                                                 MyPassword, _
                                                 MyDescription, _
                                                 MyFileExtension, _
                                                 B, _
                                                 MyWorkMode, _
                                                 MyAttention1, _
                                                 MyAttention2, _
                                                 MyCompanyName, _
                                                 MyAddress1, _
                                                 MyAddress2, _
                                                 MyCity, _
                                                 MyState, _
                                                 MyPostalCode, _
                                                 MyCountry)
 
        ' 
        ' Check response status
        '
        If lngResult > 0 Then
            '
            ' 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)
        End If
 
        ' In debug mode, the following lines prevent your console application from closing automatically upon termination
        Console.WriteLine("Hit ENTER to terminate application")
        Console.ReadLine()
 
    End Sub
 
 
End Module