title: Lync API: How to send instant message to contact by email address?
body: <p>I have the email address of a Lync user and want to send him an instant message.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using Microsoft.Lync.Model;
  using Microsoft.Lync.Model.Conversation;
  
  
  namespace Build_Server_Lync_Notifier
  {
      class Program
      {
          static void Main(string[] args)
          {
              if (args.Length != 2)
              {
                  Console.WriteLine("Usage: bsln.exe &lt;uri&gt; &lt;message&gt;");
                  return;
              }
  
              LyncClient client = Microsoft.Lync.Model.LyncClient.GetClient();
              Contact contact = client.ContactManager.GetContactByUri(args[0]);
  
              Conversation conversation = client.ConversationManager.AddConversation();
              conversation.AddParticipant(contact);
  
              Dictionary&lt;InstantMessageContentType, String&gt; messages = new Dictionary&lt;InstantMessageContentType, String&gt;();
              messages.Add(InstantMessageContentType.PlainText, args[1]);
  
              InstantMessageModality m = (InstantMessageModality) conversation.Modalities[ModalityTypes.InstantMessage];
              m.BeginSendMessage(messages, null, messages);
  
              //Console.Read();
          }
      }
  }
  </code></pre>
  
  <p><strong>Screenshot</strong>
  <img src="https://i.stack.imgur.com/Vs3b5.png" alt="Lync problems">
  Link to large screenshot: <a href="https://i.imgur.com/LMHEF.png" rel="nofollow noreferrer">http://i.imgur.com/LMHEF.png</a></p>
  
  <p>As you can see in this screenshot, my program doesn't really seem to work, even though I'm able to manually search up the contact and send an instant message manually.</p>
  
  <p>I also tried using <code>ContactManager.BeginSearch()</code> instead of <code>ContactManager.GetContactByUri()</code>, but got the same result (you can see in the screenshot): <a href="http://pastie.org/private/o9joyzvux4mkhzsjw1pioa" rel="nofollow noreferrer">http://pastie.org/private/o9joyzvux4mkhzsjw1pioa</a></p>