Microsoft Lync and Skype for Business have a rich set of .NET APIs which make it easy to extend the platform and integrate it with other applications. This blog helps explain how to use those APIs.

Sending formatted IMs from a UCMA application

Posted: January 29th, 2012 | Author: | Filed under: UCMA 3.0 | Tags: , , , | 2 Comments »

I can’t honestly say that I’ve run into a lot of situations where I’ve said to myself, “If only I could send an HTML formatted instant message right now, my problems would go away.” But adding formatting to IMs can definitely help with some specialized communication applications such as bots where you want to distinguish different types of messages, or emphasize certain parts of a conversation. So, to make those nice visual touches easier, and for anyone out there who is in a tight spot by virtue of not knowing how to send an IM with special formatting, I thought I’d write up a quick post on it.

Normally, instant messages from a UCMA application are sent as plain text. This is the easy way to do it, and in code it looks something like this:

call.Flow.BeginSendInstantMessage("Jackdaws love my big sphinx of quartz.",
    ar =>
    {
        try
        {
            call.Flow.EndSendInstantMessage(ar);
        }
        catch (RealTimeException ex)
        {
            Console.WriteLine(ex);
        }
    },
    null);

(If you’re wondering about my sample message, it’s one of those sentences that use all of the letters of the alphabet. I don’t really have any tips on the decorative preferences of jackdaws.)

Run this code using an already-established IM call, and you’ll get a perfectly respectable instant message like this:

Instant message without formatting

However, there is a second overload of the InstantMessagingFlow.BeginSendInstantMessage method. Here is an example of how you can use it to send an HTML-formatted instant message:

string htmlMessage = "<html><body><span style=\"color: red; font-variant: small-caps;\">" +
    "Jackdaws love my <span style=\"font-size: 26px;\">big</span> sphinx of quartz." +
    "</span></body></html>";
byte[] htmlBytes = Encoding.UTF8.GetBytes(htmlMessage);

call.Flow.BeginSendInstantMessage(
    new System.Net.Mime.ContentType("text/html"), 
    htmlBytes,
    ar =>
    {
        try
        {
            call.Flow.EndSendInstantMessage(ar);
        }
        catch (RealTimeException ex)
        {
            Console.WriteLine(ex);
        }
    },
    null);

The first parameter for this overload of BeginSendInstantMessage needs to be an instance of System.Net.Mime.ContentType; for an HTML message, you can provide the string “text/html” as the parameter for the ContentType constructor. (A normal plain-text IM would have a MIME type of “text/plain”.)

The second parameter is the byte-encoded message body, which you can get by calling Encoding.UTF8.GetBytes with your HTML string.

With code like the above, you get a beautifully formatted instant message like this:

Instant message with formatting

I think this one really communicates the magnitude of the sphinx of quartz we’re talking about here.

That’s about all there is to it. It doesn’t seem to be necessary to wrap everything in <html> and <body> tags, but I usually do it just to feel good about myself. Also, note that you can send links, but they may get blocked depending on the policy settings in your environment.

It’s also possible to send other types of content, such as image/gif or rtf or application/x-ms-ink or multi-part/alternative. Let me know if you find an interesting way to use any of these!


2 Comments on “Sending formatted IMs from a UCMA application”

  1. 1 Kadir Luzumlar said at 6:54 am on August 28th, 2015:

    Hi
    I’m trying to find out sending file from UCMA applicationendpoint to userendpoint (lync client).

    I could not find any sample which is consistently working.

    could you please help ?

  2. 2 Florian Hötzinger said at 8:08 am on June 22nd, 2016:

    We were not able to send images through this method. Did you get this to work by any chance?
    We are on Skype for Business….


Leave a Reply

  • Note: Comment moderation is in use because of excessive spam. Your comment may not appear immediately.

  •