2012-09-25 16 views
5

मेरे पास एक Lync उपयोगकर्ता का ईमेल पता है और उसे एक त्वरित संदेश भेजना चाहते हैं।Lync API: ईमेल पते से संपर्क करने के लिए तत्काल संदेश कैसे भेजें?

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 <uri> <message>"); 
       return; 
      } 

      LyncClient client = Microsoft.Lync.Model.LyncClient.GetClient(); 
      Contact contact = client.ContactManager.GetContactByUri(args[0]); 

      Conversation conversation = client.ConversationManager.AddConversation(); 
      conversation.AddParticipant(contact); 

      Dictionary<InstantMessageContentType, String> messages = new Dictionary<InstantMessageContentType, String>(); 
      messages.Add(InstantMessageContentType.PlainText, args[1]); 

      InstantMessageModality m = (InstantMessageModality) conversation.Modalities[ModalityTypes.InstantMessage]; 
      m.BeginSendMessage(messages, null, messages); 

      //Console.Read(); 
     } 
    } 
} 

स्क्रीनशॉट Lync problems बड़े स्क्रीनशॉट के लिए लिंक: http://i.imgur.com/LMHEF.png

आप इस स्क्रीनशॉट में देख सकते हैं, मेरा कार्यक्रम नहीं वास्तव में काम करने के लिए, प्रतीत होता है, भले ही मैं मैन्युअल रूप से खोज करने में सक्षम हूँ संपर्क ऊपर और मैन्युअल रूप से एक त्वरित संदेश भेजें।

मैं भी ContactManager.GetContactByUri() के बजाय ContactManager.BeginSearch() उपयोग करने की कोशिश, लेकिन एक ही परिणाम (आप स्क्रीनशॉट में देख सकते हैं) मिल गया: http://pastie.org/private/o9joyzvux4mkhzsjw1pioa

+0

मैं राज्य के लिए कुछ भी नहीं देख सकते हैं यह क्यों काम नहीं कर रहा है, क्या आप एल में डीबग लॉग सक्षम कर सकते हैं ync (सामान्य "टैब" के तहत सेटिंग्स) और आउटपुट को – Neo

+0

@Neo http://pastie.org/private/igdb3rgsdjfmujyl2j7q –

+0

मैं अपने सी # से लॉग के साथ एक सफल (मैन्युअल) प्रयास से लॉग की तुलना करने की कोशिश करूंगा औसत समय में प्रयास करें। –

उत्तर

6

ठीक है, तो मैं इसे अब काम मिल गया। इसे एक कामकाजी राज्य में मिला, हालांकि मुझे कुछ गंभीर रिफैक्टरिंग करने की ज़रूरत है।

Program.cs

using System; 

namespace Build_Server_Lync_Notifier 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      if (args.Length != 2) 
      { 
       Console.WriteLine("Usage: bsln.exe <uri> <message>"); 
       return; 
      } 

      LyncManager lm = new LyncManager(args[0], args[1]); 

      while (!lm.Done) 
      { 
       System.Threading.Thread.Sleep(500); 
      } 
     } 
    } 
} 

LyncManager.cs

using Microsoft.Lync.Model; 
using Microsoft.Lync.Model.Conversation; 
using System; 
using System.Collections.Generic; 

namespace Build_Server_Lync_Notifier 
{ 
    class LyncManager 
    { 
     private string _uri; 
     private string _message; 
     private LyncClient _client; 
     private Conversation _conversation; 

     private bool _done = false; 
     public bool Done 
     { 
      get { return _done; } 
     } 

     public LyncManager(string arg0, string arg1) 
     { 
      _uri = arg0; 
      _message = arg1; 
      _client = Microsoft.Lync.Model.LyncClient.GetClient(); 
      _client.ContactManager.BeginSearch(
       _uri, 
       SearchProviders.GlobalAddressList, 
       SearchFields.EmailAddresses, 
       SearchOptions.ContactsOnly, 
       2, 
       BeginSearchCallback, 
       new object[] { _client.ContactManager, _uri } 
      ); 
     } 

     private void BeginSearchCallback(IAsyncResult r) 
     { 
      object[] asyncState = (object[]) r.AsyncState; 
      ContactManager cm = (ContactManager) asyncState[0]; 
      try 
      { 
       SearchResults results = cm.EndSearch(r); 
       if (results.AllResults.Count == 0) 
       { 
        Console.WriteLine("No results."); 
       } 
       else if (results.AllResults.Count == 1) 
       { 
        ContactSubscription srs = cm.CreateSubscription(); 
        Contact contact = results.Contacts[0]; 
        srs.AddContact(contact); 
        ContactInformationType[] contactInformationTypes = { ContactInformationType.Availability, ContactInformationType.ActivityId }; 
        srs.Subscribe(ContactSubscriptionRefreshRate.High, contactInformationTypes); 
        _conversation = _client.ConversationManager.AddConversation(); 
        _conversation.AddParticipant(contact); 
        Dictionary<InstantMessageContentType, String> messages = new Dictionary<InstantMessageContentType, String>(); 
        messages.Add(InstantMessageContentType.PlainText, _message); 
        InstantMessageModality m = (InstantMessageModality)_conversation.Modalities[ModalityTypes.InstantMessage]; 
        m.BeginSendMessage(messages, BeginSendMessageCallback, messages); 
       } 
       else 
       { 
        Console.WriteLine("More than one result."); 
       } 
      } 
      catch (SearchException se) 
      { 
       Console.WriteLine("Search failed: " + se.Reason.ToString()); 
      } 
      _client.ContactManager.EndSearch(r); 
     } 

     private void BeginSendMessageCallback(IAsyncResult r) 
     { 
      _conversation.End(); 
      _done = true; 
     } 
    } 
} 
3

कोड के नीचे की कोशिश करो अपने मेरे लिए ठीक काम कर रहा

protected void Page_Load(object sender, EventArgs e) 
{ 
    SendLyncMessage(); 
} 
private static void SendLyncMessage() 
{ 
    string[] targetContactUris = {"sip:[email protected]"}; 
    LyncClient client = LyncClient.GetClient(); 
    Conversation conv = client.ConversationManager.AddConversation(); 

    foreach (string target in targetContactUris) 
    { 
    conv.AddParticipant(client.ContactManager.GetContactByUri(target)); 
    } 
    InstantMessageModality m = conv.Modalities[ModalityTypes.InstantMessage] as InstantMessageModality; 
    m.BeginSendMessage("Test Message", null, null); 
} 
+0

धन्यवाद! यह मेरे लिए भी काम करता है! – derekantrican

संबंधित मुद्दे