【问题标题】:Lync API: How to send instant message to contact by email address?Lync API:如何通过电子邮件地址向联系人发送即时消息?
【发布时间】:2012-09-16 20:39:52
【问题描述】:

我有一个 Lync 用户的电子邮件地址并想向他发送即时消息。

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();
        }
    }
}

截图 大图链接:http://i.imgur.com/LMHEF.png

正如您在此屏幕截图中看到的那样,我的程序似乎并没有真正工作,即使我能够手动搜索联系人并手动发送即时消息。

我也尝试使用ContactManager.BeginSearch() 而不是ContactManager.GetContactByUri(),但得到了相同的结果(您可以在屏幕截图中看到):http://pastie.org/private/o9joyzvux4mkhzsjw1pioa

【问题讨论】:

  • 我看不到任何说明它为什么不工作的东西,你能在 lync 中启用调试日志(一般“选项卡”下的设置)并将输出放在这里
  • 我将尝试将成功(手动)尝试的日志与同时尝试 C# 失败的日志进行比较。
  • 嗯,那里有一些错误对我来说毫无意义,但看起来有点怀疑为什么消息没有发送,它说它无法匹配 SIP 数据或主机 (09 /26/2012|20:26:20.335 16E8:1488 错误 :: SIP_URL::InternalInitialize 解析 SIP URL 时未找到主机)我会先查看这个,因为这应该可以解决问题。

标签: c# lync lync-2010


【解决方案1】:

好的,所以我现在开始工作了。让它处于工作状态,尽管我需要进行一些认真的重构。

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;
        }
    }
}

【讨论】:

    【解决方案2】:

    试试下面的代码它对我来说很好

    protected void Page_Load(object sender, EventArgs e)
    { 
       SendLyncMessage();
    }
    private static void SendLyncMessage()
    {
      string[] targetContactUris = {"sip:xxxx@domain.com"};
      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);
    }
    

    【讨论】:

      猜你喜欢
      • 2012-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-21
      • 1970-01-01
      • 2015-01-29
      相关资源
      最近更新 更多