ASP: Send A Letter with SendLetterAndAddress()
Instructions
- Create a new ASP page.
- Copy the entire code below and paste the code inside the ASP page.
- Replace C:\MyFile.pdf with a path to your test PDF file. Remember, method SendLetterAndAdressV2 will add the recipient's address so it would show through the envelope window. You can use one of our Sample Letter Documents.
- Replace MyUsername and MyPassword with your PostalMethods user details.
- Replace all the address parameters with real address values. Leave an empty string ("") where no value is needed.
- Start your application. It will start and send a letter to the PostalMethods SendLetterAndAddressV2 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.
<% ' ' This asp script demonstrates how to send a binary file ' (such as .PDF, .DOC) from an ASP script, using MS SOAP 3.0 ' and the SendLetterAndAddressV2 method within PostalMethods. As asp has no ' built in provisions to reading binary files, we use an ADODB.Stream ' object to do so. ' Option Explicit Const Filename = "C:\MyFile.pdf" Const MyUsername = "MyUsername" Const MyPassword = "MyPassword" Const Description = "Send a letter using ASP" Const AttentionLine1 = "Mr. Danny Kay" Const AttentionLine2 = "Director, Dancing Dept." Const CompanyName = "Comedy Makers" Const AddressLine1 = "1234 Main ST." Const AddressLine2 = "1st FL." Const City = "Brooklyn" Const State = "NY" Const PostalCodeZIP = "11201" Const Country = "USA" Dim objSoap, lngResult Dim B ' ' Read your binary document. ' B = ReadBinaryFile(Filename) ' 'Create the SoapClient object 'MS SOAP v3.0 ' Set objSoap = Server.CreateObject("MSSOAP.SOAPClient30") ' 'Set to True when an Active Server Pages (ASP) application or an ISAPI DLL uses the SoapClient object. ' objSoap.ClientProperty("ServerHTTPRequest") = True ' 'Initializes the SoapClient object. ASP initializes the WSDL on each call. 'For better performance, you may download the WSDL locally as an XML and refer to the local file. ' objSoap.mssoapinit("http://api.postalmethods.com/2009-02-26/PostalWS.asmx?WSDL") ' 'Set to True when a proxy server is to be detected automatically ' objSoap.ConnectorProperty("EnableAutoProxy") = True ' ' Invoke the SendLetterAndAddress() method ' lngResult = objSoap.SendLetterAndAddress( _ MyUsername, _ MyPassword, _ Description, _ "pdf", _ B, _ "Default", AttentionLine1, _ AttentionLine2, _ CompanyName, _ AddressLine1, _ AddressLine2, _ City, _ State, _ PostalCodeZIP, _ Country) If CLng(lngResult) > 0 Then WriteLine("LetterID is: " & lngResult) Else WriteLine("Error is:" & lngResult) End If '********************************************************* ' Utility methods '********************************************************* Function ReadBinaryFile(FileName) Const adTypeBinary = 1 'Create Stream object Dim BinaryStream Set BinaryStream = CreateObject("ADODB.Stream") 'Specify stream type - we want To get binary data. BinaryStream.Type = adTypeBinary 'Open the stream BinaryStream.Open 'Load the file data from disk To stream object BinaryStream.LoadFromFile FileName 'Open the stream And get binary data from the object ReadBinaryFile = BinaryStream.Read 'Clean up BinaryStream.Close Set BinaryStream = Nothing End Function Sub WriteLine(T) Response.Write T & "<BR>" End Sub 'Note: If this script is running from behind a proxy using MS SOAP v.3, you can use one of the two following options: 'Option 1 - Use automatic detection by: ' objSoap.ConnectorProperty("EnableAutoProxy") = True 'Option 2 - Specify a proxy server: ' objSoap.ConnectorProperty("ProxyServer") = "192.168.0.100" 'and optionally, if the proxy server is password-protected specify: ' objSoap.ConnectorProperty("ProxyUser") = "Proxy Username" ' objSoap.ConnectorProperty("ProxyPassword") = "Proxy Password" %>

