Home > Uncategorized > Send physical #mail automatically from c#

Send physical #mail automatically from c#


In a world of email, sometimes a letter caries more weight. It is more likely to be read, and has a nice physical quality to it. Docmail offers an automated service to send PDFs as physical mail to an address automatically, and here’s some C# code to show how it’s done.

  1. Create a new project, and make a service reference to https://www.cfhdocmail.com/LiveAPI/DMWS.asmx – call the reference ServiceReference2
  2. Importantly: modify your app.config changing the binding to say maxReceivedMessageSize=”2147483647″  like follows;

    <?xml version=”1.0″ encoding=”utf-8″ ?>
    <supportedRuntime version=”v4.0″ sku=”.NETFramework,Version=v4.5″ />
    <binding name=”DMWSSoap” maxReceivedMessageSize=”2147483647″ >
    <security mode=”Transport” />

    <binding name=”DMWSSoap1″ maxReceivedMessageSize=”2147483647″/>
    <endpoint address=”https://www.cfhdocmail.com/LiveAPI/DMWS.asmx&#8221;
    binding=”basicHttpBinding” bindingConfiguration=”DMWSSoap”
    contract=”ServiceReference2.DMWSSoap” name=”DMWSSoap”


  3. Ensure you’ve got a username and password with Docmail, as I’ve omitted this from the code, and the postal address is obscured.

    using System;
    using System.IO;

    namespace Docmail
    class Program
    static void Main()
    // Need reference to https://www.cfhdocmail.com/LiveAPI/DMWS.asmx
    // put maxReceivedMessageSize=”2147483647″ into the binding

    //(where ServiceReference2 is the root namespace for the web service reference)
    private static void SendFile(string sUsr, string sPwd)
    var bFile = File.ReadAllBytes(@”C:\Users\Fiach\Documents\MBCloud Invoice.pdf”);
    var oService = new ServiceReference2.DMWSSoapClient();
    var oSAH = new ServiceReference2.ServiceAuthHeader
    Username = sUsr,
    Password = sPwd
    var oMailing = new ServiceReference2.Mailing();
    var oAddress = new ServiceReference2.Address();
    oMailing.MailingDescription = “Test mail.”;
    oMailing.IsBlackAndWhite = false;
    oMailing.IsDuplex = false;
    oMailing.AddressNameFormat = “T”;
    oAddress.Title = “MR”;
    oAddress.FirstName = “Bob”;
    oAddress.Surname = “Jones”;
    oAddress.JobTitle = “”;
    oAddress.FullName = “Bon Jones”;
    oAddress.Address1 = “10 Main Street”;
    oAddress.Address2 = “London”;
    oAddress.Address3 = “SW1 444”;
    oAddress.Address4 = “England”;
    oAddress.Address5 = “(UK)”;
    //add address to mailing
    var oAddresses = new ServiceReference2.Address[1];
    oAddresses.SetValue(oAddress, oAddresses.Length – 1);
    oMailing.Addresses = oAddresses;
    var oTemplate = new ServiceReference2.Template();
    oTemplate.FileData = bFile;
    oTemplate.FileName = “tsftfile.pdf”;
    oTemplate.TemplateName = “TemplateName”;
    oTemplate.AddressedDocument = true;
    //add template to mailing
    var oTemplates = new ServiceReference2.Template[1];
    oTemplates.SetValue(oTemplate, oTemplates.Length – 1);
    oMailing.Templates = oTemplates;
    //Save mailing
    var oMailingResult = oService.SaveMailing(oSAH, oMailing);
    if (!oMailingResult.Success) throw new Exception(string.Format(“Mailing error: {0}”, oMailingResult.FailureMessage));
    //Create a proof of the mailing
    var oProof = oService.GetProof(oSAH, oMailingResult.MailingGUID, true);
    var iSecondCount = 0;
    //Loop for 3 mins waiting for a proof
    while ((iSecondCount < 180) && oProof.Success && !oProof.ProofReady)
    //Sleep for 5 seconds
    oProof = oService.GetProof(oSAH, oMailingResult.MailingGUID, true);
    iSecondCount += 1;
    if (!oProof.Success | !oProof.ProofReady) throw new Exception(“Proof not ready within 3 mins”);
    //Place the order
    var oOrderResult = oService.PlaceOrder(oSAH, oMailingResult.MailingGUID, “”);
    if (!oOrderResult.Success) throw new Exception(string.Format(“PlaceOrder error: {0}”, oOrderResult.FailureMessage));

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: