/// <summary>
        /// Check all 3 recipient lists To, Cc, and Bcc (List of <c>IEmailAddress</c>) for valid email addresses
        /// </summary>
        /// <param name="message">An <c>IBasicMessage</c> object to be sent.</param>
        /// <returns>A List of <c>AddressResult</c> if an invalid email address is found.</returns>
        internal virtual List <AddressResult> HasInvalidRecipients(IBasicMessage message)
        {
            var result = new List <AddressResult>();

            var invalidTo = FindInvalidRecipients(message.To);

            if (invalidTo != null && invalidTo.Any())
            {
                result.AddRange(invalidTo.ToList());
            }

            var invalidCc = FindInvalidRecipients(message.Cc);

            if (invalidCc != null && invalidCc.Any())
            {
                result.AddRange(invalidCc.ToList());
            }

            var invalidBcc = FindInvalidRecipients(message.Bcc);

            if (invalidBcc != null && invalidBcc.Any())
            {
                result.AddRange(invalidBcc.ToList());
            }

            return(result);
        }
 public Task <SendResponse> SendAsync(IBasicMessage message)
 {
     return(Task.Run(() => new SendResponse()
     {
         Result = SendResult.Success
     }));
 }
        /// <summary>
        /// Asynchronously sends a basic email message and returns the response from the Injection API.
        /// </summary>
        /// <param name="message">A <c>BasicMessage</c> object to be sent.</param>
        /// <param name="cancellationToken">A <c>CancellationToken</c> to handle cancellation between async threads.</param>
        /// <returns>A <c>SendResponse</c> of an SocketLabsClient send request.</returns>
        /// <example>
        /// This sample shows you how to Send a Basic Message
        /// <code>
        /// var client = new SocketLabsClient(00000, "apiKey");
        ///
        /// var message = new BasicMessage();
        ///
        /// message.PlainTextBody = "This is the body of my message sent to ##Name##";
        /// message.HtmlBody = "<html>This is the HtmlBody of my message sent to ##Name##</html>";
        /// message.Subject = "Sending a test message";
        /// message.From.Email = "*****@*****.**";
        /// message.To.Add("*****@*****.**");
        /// message.To.Add("*****@*****.**");
        ///
        /// var response = await client.Send(message);
        ///
        /// if (response.Result != SendResult.Success)
        /// {
        ///     // Handle Error
        /// }
        ///</code>
        /// </example>
        public async Task <SendResponse> SendAsync(IBasicMessage message, CancellationToken cancellationToken)
        {
            var validator = new SendValidator();

            var validationResult = validator.ValidateCredentials(_serverId, _apiKey);

            if (validationResult.Result != SendResult.Success)
            {
                return(validationResult);
            }

            validationResult = validator.ValidateMessage(message);
            if (validationResult.Result != SendResult.Success)
            {
                return(validationResult);
            }

            var factory          = new InjectionRequestFactory(_serverId, _apiKey);
            var injectionRequest = factory.GenerateRequest(message);
            var json             = injectionRequest.GetAsJson();

            var retryHandler = new RetryHandler(_httpClient, EndpointUrl, new RetrySettings(NumberOfRetries));
            var httpResponse = await retryHandler.SendAsync(json, cancellationToken);

            var response = new InjectionResponseParser().Parse(httpResponse);

            return(response);
        }
Exemple #4
0
 /// <summary>
 /// Broadcasts a message to every sender. This overload allows a sender to send
 /// a loopback message (i.e. will be received by the same sender), or allows a sender
 /// who is not registered to receive messages to send a message.
 /// </summary>
 /// <param name="message">The message to send.</param>
 public void BroadcastMessage(IBasicMessage message)
 {
     foreach (var queue in m_MsgQueues)
     {
         queue.Enqueue(message);
     }
 }
 public SendResponse Send(IBasicMessage message)
 {
     return(new SendResponse()
     {
         Result = SendResult.Success
     });
 }
Exemple #6
0
        /// <summary>
        /// Asynchronously sends a basic email message and returns the response from the Injection API.
        /// </summary>
        /// <param name="message">A <c>BasicMessage</c> object to be sent.</param>
        /// <returns>A <c>SendResponse</c> of an SocketLabsClient send request.</returns>
        /// <example>
        /// This sample shows you how to Send a Basic Message
        /// <code>
        /// var client = new SocketLabsClient(00000, "apiKey");
        ///
        /// var message = new BasicMessage();
        ///
        /// message.PlainTextBody = "This is the body of my message sent to ##Name##";
        /// message.HtmlBody = "<html>This is the HtmlBody of my message sent to ##Name##</html>";
        /// message.Subject = "Sending a test message";
        /// message.From.Email = "*****@*****.**";
        /// message.To.Add("*****@*****.**");
        /// message.To.Add("*****@*****.**");
        ///
        /// var response = await client.Send(message);
        ///
        /// if (response.Result != SendResult.Success)
        /// {
        ///     // Handle Error
        /// }
        ///</code>
        /// </example>
        public async Task <SendResponse> SendAsync(IBasicMessage message)
        {
            var validator = new SendValidator();

            var validationResult = validator.ValidateCredentials(_serverId, _apiKey);

            if (validationResult.Result != SendResult.Success)
            {
                return(validationResult);
            }

            validationResult = validator.ValidateMessage(message);
            if (validationResult.Result != SendResult.Success)
            {
                return(validationResult);
            }

            var factory          = new InjectionRequestFactory(_serverId, _apiKey);
            var injectionRequest = factory.GenerateRequest(message);
            var json             = injectionRequest.GetAsJson();

            var httpResponse = await _httpClient.PostAsync(EndpointUrl, json);

            var response = new InjectionResponseParser().Parse(httpResponse);

            return(response);
        }
Exemple #7
0
        private void OnExternalMsgReceived(object sender, EventArgs e)
        {
            IBasicMessage msg = m_MsgQ.Dequeue();

            if (m_CmdHandlers.TryGetValue(msg.MessageType, out ICommand handler))
            {
                msg.HandleMessage(handler);
            }
        }
        private void OnExternalMsgReceived(object sender, EventArgs e)
        {
            var           queue = sender as ObservableQueue <IBasicMessage>;
            IBasicMessage msg   = queue.Dequeue();

            if (m_CmdHandlers.TryGetValue(msg.MessageType, out ICommand handler))
            {
                msg.HandleMessage(handler);
            }
        }
        /// <summary>
        /// Validate a basic email message before sending to the Injection API.
        /// </summary>
        /// <param name="message">An <c>IBasicMessage</c> object to be sent.</param>
        /// <returns>A <c>SendResponse</c> with the validation results</returns>
        public SendResponse ValidateMessage(IBasicMessage message)
        {
            var result = ValidateIMessageBase(message);

            if (result == SendResult.Success)
            {
                return(ValidateRecipients(message));
            }

            return(new SendResponse()
            {
                Result = result
            });
        }
Exemple #10
0
        /// <summary>
        /// Broadcasts a message to every sender. This overload takes care to not
        /// send a message to a receiver with the same ID (i.e. does not loopback messages).
        /// </summary>
        /// <param name="senderId">The ID of the sender. This ID should match the value returned from
        /// RegisterMessageQueue.</param>
        /// <param name="message">The message to send.</param>
        public void BroadcastMessage(int senderId, IBasicMessage message)
        {
            int index = 0;

            foreach (var queue in m_MsgQueues)
            {
                if (senderId != index)
                {
                    queue.Enqueue(message);
                }

                ++index;
            }
        }
Exemple #11
0
 /// <summary>
 /// Synchronously sends a basic email message and returns the response from the Injection API.
 /// </summary>
 /// <param name="message">A <c>BasicMessage</c> object to be sent.</param>
 /// <returns>A <c>SendResponse</c> of an SocketLabsClient send request.</returns>
 /// <example>
 /// This sample shows you how to Send a Basic Message
 /// <code>
 /// var client = new SocketLabsClient(00000, "apiKey");
 ///
 /// var message = new BasicMessage();
 ///
 /// message.PlainTextBody = "This is the body of my message sent to ##Name##";
 /// message.HtmlBody = "<html>This is the HtmlBody of my message sent to ##Name##</html>";
 /// message.Subject = "Sending a test message";
 /// message.From.Email = "*****@*****.**";
 /// message.To.Add("*****@*****.**");
 /// message.To.Add("*****@*****.**");
 ///
 /// var response = client.Send(message);
 ///
 /// if (response.Result != SendResult.Success)
 /// {
 ///     // Handle Error
 /// }
 ///</code>
 /// </example>
 public SendResponse Send(IBasicMessage message)
 {
     try
     {
         //Read this if you have questions: https://blogs.msdn.microsoft.com/pfxteam/2012/04/13/should-i-expose-synchronous-wrappers-for-asynchronous-methods/
         return(Task.Run(() => SendAsync(message)).Result);
     }
     //for synchronous usage, try to simplify exceptions being thrown
     catch (AggregateException e)
     {
         if (e.InnerException != null)
         {
             ExceptionDispatchInfo.Capture(e.InnerException).Throw();
         }
         throw;
     }
 }
        /// <summary>
        /// Cumulative count of recipients in all 3 recipient lists To, Cc, and Bcc (List of <c>IEmailAddress</c>)
        /// </summary>
        /// <param name="message">An <c>IBasicMessage</c> object to be sent.</param>
        /// <returns></returns>
        internal virtual int GetFullRecipientCount(IBasicMessage message)
        {
            var recipientCount = 0;

            if (message.To != null)
            {
                recipientCount += message.To.Count;
            }
            if (message.Cc != null)
            {
                recipientCount += message.Cc.Count;
            }
            if (message.Bcc != null)
            {
                recipientCount += message.Bcc.Count;
            }
            return(recipientCount);
        }
Exemple #13
0
        /// <summary>
        /// Generate the <c>InjectionRequest</c> for sending to the Injection Api
        /// </summary>
        /// <param name="message">An <c>IBasicMessage</c> object to be sent.</param>
        /// <returns>An <c>InjectionRequest</c> for sending to the Injection Api</returns>
        public InjectionRequest GenerateRequest(IBasicMessage message)
        {
            var request = new InjectionRequest(_serverId, _apiKey);

            var jsonMsg = GenerateBaseMessageJson(message);

            jsonMsg.To  = PopulateList(message.To);
            jsonMsg.Cc  = PopulateList(message.Cc);
            jsonMsg.Bcc = PopulateList(message.Bcc);

            request.Messages.Add(jsonMsg);

            if (message.ReplyTo != null)
            {
                jsonMsg.ReplyTo = new AddressJson(message.ReplyTo.Email, message.ReplyTo.FriendlyName);
            }

            return(request);
        }
        /// <summary>
        /// Validate email recipients for a basic message
        /// </summary>
        /// <remarks>
        /// Checks the To, Cc, and the Bcc recipient fields (List of <c>IEmailAddress</c>) for the following:
        /// <list type="bullet">
        /// <item>
        /// <description>At least 1 recipient is in the list.</description>
        /// </item>
        /// <item>
        /// <description>Cumulative count of recipients in all 3 lists do not exceed the <c>MaximumRecipientsPerMessage</c>.</description>
        /// </item>
        /// <item>
        /// <description>Recipients in lists are valid.</description>
        /// </item>
        /// </list>
        ///  If errors are found, the <c>SendResponse</c> will contain the invalid email addresses
        /// </remarks>
        /// <param name="message">An <c>IBasicMessage</c> object to be sent.</param>
        /// <returns>A <c>SendResponse</c> with the validation results</returns>
        internal virtual SendResponse ValidateRecipients(IBasicMessage message)
        {
            var fullRecipientCount = GetFullRecipientCount(message);

            if (fullRecipientCount <= 0)
            {
                return new SendResponse()
                       {
                           Result = SendResult.RecipientValidationNoneInMessage
                       }
            }
            ;

            if (fullRecipientCount > MaximumRecipientsPerMessage)
            {
                return new SendResponse()
                       {
                           Result = SendResult.RecipientValidationMaxExceeded
                       }
            }
            ;

            var invalidRec = HasInvalidRecipients(message);

            if (invalidRec != null && invalidRec.Any())
            {
                return new SendResponse()
                       {
                           Result         = SendResult.RecipientValidationInvalidRecipients,
                           AddressResults = invalidRec.ToArray()
                       }
            }
            ;

            return(new SendResponse()
            {
                Result = SendResult.Success
            });
        }
 /// <summary>
 /// Broadcasts a message to every other view that is subscribed to the input message
 /// type.
 /// </summary>
 /// <param name="msg">The message to broadcast.</param>
 protected void SendMessage(IBasicMessage msg)
 {
     m_ViewMsgMgr.BroadcastMessage(m_SenderId, msg);
 }
Exemple #16
0
 protected void BroadcastMessage(IBasicMessage msg)
 {
     m_MsgMgr.BroadcastMessage(m_MsgSenderId, msg);
 }