Archive

Archive for January, 2017

Send physical #mail automatically from c#

docmail_logo.png

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″ ?>
    <configuration>
    <startup>
    <supportedRuntime version=”v4.0″ sku=”.NETFramework,Version=v4.5″ />
    </startup>
    <system.serviceModel>
    <bindings>
    <basicHttpBinding>
    <binding name=”DMWSSoap” maxReceivedMessageSize=”2147483647″ >
    <security mode=”Transport” />

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

    >
    </endpoint>
    </client>
    </system.serviceModel>
    </configuration>

  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
    SendFile(“email@gmail.com”,”password”);
    }

    //(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
    System.Threading.Thread.Sleep(1000);
    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

Verify #SSL expiry using C#

main-qimg-e68f47f29c69ce4b02aea40b29b54819

Need to remember when to renew a SSL cert?, here’s some handy code in c#

using System;
using System.Net;
using System.Security.Cryptography.X509Certificates;

namespace CheckSSL
{
class Program
{
static void Main(string[] args)
{
//Do webrequest to get info on secure site
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(“https://www.avatarapi.com&#8221;);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Close();

//retrieve the ssl cert and assign it to an X509Certificate object
X509Certificate cert = request.ServicePoint.Certificate;

//convert the X509Certificate to an X509Certificate2 object by passing it into the constructor
X509Certificate2 cert2 = new X509Certificate2(cert);

string cn = cert2.GetIssuerName();
string cedate = cert2.GetExpirationDateString();

Console.WriteLine(cn);
Console.WriteLine(cedate);

Console.ReadLine();
}
}
}

Categories: Uncategorized

A simple #Watchkit app in #ObjectiveC

screen-shot-2017-01-08-at-09-57-40

Yes, I just got an apple watch, and the first thing I want to do with it, is to learn to program on it. so, apart from a simple “Hello World” app. I wanted to create a very simple companion app for a bitcoin app (https://itunes.apple.com/gb/app/get-bitcoin/id1072062149?mt=8)  – which just looks up the BTC / USD exchange rate and shows it on the watch.

So, here’s the code;

@interface InterfaceController()

@property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceLabel *lblHello;

@end

@implementation InterfaceController

– (void)awakeWithContext:(id)context {

    [super awakeWithContext:context];

    // Configure interface objects here.

     [self.lblHello setText:@”Loading…”];

    // make request to https://blockchain.info/tobtc?currency=USD&value=1

    NSURL *url = [NSURL URLWithString:@”https://blockchain.info/tobtc?currency=USD&value=1&#8243;];

    NSError* error;

    NSString *content = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&error];

    // Check for network errors

    if (error)

    {

       [self.lblHello setText:@”Network error”];

       return;

    }

    // Get reciprocal

    float fRate = [content floatValue];

    float fReciprocal = 1 / fRate;

    

    NSString *str = [NSString stringWithFormat:@”$%.2lf”, fReciprocal];

    

    [self.lblHello setText:str];

}

Categories: Uncategorized