/// <summary> /// Sends message to current member /// </summary> /// <param name="subject">Message subject</param> /// <param name="body">Message body</param> /// <param name="includeSenderInRecipients">Indicates whether sender of message should be included in recipients</param> /// <returns>Value containing true or false, depending on operation success, and response status</returns> /// <exception cref="LinkedInMissingParameterException">Thrown when message's subject or body are null or empty strings</exception> public LinkedInResponse<bool> SendMessage(string subject, string body, bool includeSenderInRecipients) { if (string.IsNullOrEmpty(subject)) throw new LinkedInMissingParameterException("Message subject cannot be null or empty", "Subject"); if (string.IsNullOrEmpty(body)) throw new LinkedInMissingParameterException("Message body cannot be null or empty", "Body"); var options = new LinkedInMessageOptions { Body = body, Subject = subject, IncludeSenderInRecipients = includeSenderInRecipients }; options.Recipients.Add(Id); return RequestRunner.SendMessage(options); }
/// <summary> /// Sends message over LinkedIn network /// </summary> /// <param name="options">The oject of type <see cref="LinkedInMessageOptions"/> representing message options</param> /// <returns>Value containing <see cref="LinkedInSearchResult"/> object and response status</returns> /// <exception cref="LinkedInMissingParameterException">Thrown when message's subject or body are null or empty strings</exception> /// <exception cref="LinkedInNoRecipientsException">Thrown when there are no recipients</exception> /// <example> /// This sample shows how to call this method: /// <code> /// using LinkedIn.NET; /// using LinkedIn.NET.Groups; /// using LinkedIn.NET.Members; /// using LinkedIn.NET.Options; /// using LinkedIn.NET.Search; /// using LinkedIn.NET.Updates; /// ... /// // define message options /// var options = new LinkedInMessageOptions /// { /// Subject = "Message subject", /// Body = "Message body", /// IncludeSenderInRecipients = true /// }; /// // add recipients /// options.Recipients.Add("John Smith"); /// // add more recipients /// options.Recipients.Add("Homer Simpson"); /// // send message /// var response = _Client.SendMessage(options); /// // always check response.Result and response.Status before processing /// if (result.Result != null && result.Status == LinkedInResponseStatus.OK) /// { /// MessageBox.Show(@"Message sent successfully."); /// } /// </code> /// </example> public LinkedInResponse<bool> SendMessage(LinkedInMessageOptions options) { if (string.IsNullOrEmpty(options.Subject)) throw new LinkedInMissingParameterException("Message's subject cannot be null or empty", "Subject"); if (string.IsNullOrEmpty(options.Body)) throw new LinkedInMissingParameterException("Message's body cannot be null or empty", "Body"); if (options.Recipients.Count == 0 && !options.IncludeSenderInRecipients) throw new LinkedInNoRecipientsException("Recipients list is empty"); return RequestRunner.SendMessage(options); }
internal static LinkedInResponse<bool> SendMessage(LinkedInMessageOptions options) { try { var sb = new StringBuilder(Utils.SEND_MESSAGE_URL); sb.Append("?oauth2_access_token="); sb.Append(Singleton.Instance.AccessToken); var message = new StringBuilder("<?xml version='1.0' encoding='UTF-8'?><mailbox-item><recipients>"); foreach (var r in options.Recipients) { message.Append("<recipient>"); message.Append("<person path='/people/"); message.Append(Utils.EscapeXml(r)); message.Append("'/></recipient>"); } if (options.IncludeSenderInRecipients) message.Append("<recipient><person path='/people/~'/></recipient>"); message.Append("</recipients>"); message.Append("<subject>"); message.Append(Utils.EscapeXml(options.Subject)); message.Append("</subject>"); message.Append("<body>"); message.Append(Utils.EscapeXml(options.Body)); message.Append("</body>"); message.Append("</mailbox-item>"); var statusCode = HttpStatusCode.OK; Utils.MakeRequest(sb.ToString(), "POST", ref statusCode, message.ToString()); return new LinkedInResponse<bool>(statusCode == HttpStatusCode.Created, LinkedInResponseStatus.OK, null); } catch (WebException wex) { return Utils.GetResponse(false, wex, null); } catch (Exception ex) { return new LinkedInResponse<bool>(false, LinkedInResponseStatus.OtherException, null, ex); } }