/// <summary> /// Sends the specified message. /// </summary> /// <param name="m">The message.</param> public virtual void TrySend(ShortMessageServiceMessage m, out Exception ex) { if (From == null) { throw new NullReferenceException("From"); } var emailMessage = new MailMessage { From = From, IsBodyHtml = false, Body = m.Body, }; emailMessage.To.Add(new MailAddress(GetCarrierEmail(m.CarrierID, m.Phone))); try { _smtpClient.Send(emailMessage); ex = null; } catch (SmtpException e) { ex = e; } }
/// <summary> /// Executes the specified method. /// </summary> /// <param name="smartForm">The smart form.</param> /// <param name="method">The method.</param> /// <param name="args">The args.</param> /// <returns></returns> public object Execute(SmartForm smartForm, string method, params object[] args) { if (smartForm == null) throw new ArgumentNullException("smartForm"); if ((args == null) || (args.Length == 0)) args = _defaultArgs; // send email int smsSent = 0; var usedPhoneIDs = new List<string>(); for (int argIndex = 1; argIndex < args.Length; argIndex++) { string scopeKey = (args[argIndex] as string); if (scopeKey == null) throw new ArgumentNullException(string.Format("args[{0}]", argIndex)); if (scopeKey.Length > 0) scopeKey += "::"; foreach (string phone2 in smartForm[scopeKey + "phone"].Replace(";", ",").Split(',')) { string phone = phone2.Trim(); if (!string.IsNullOrEmpty(phone) && !usedPhoneIDs.Contains(phone.ToLowerInvariant())) { string carrierIDAsText = smartForm[scopeKey + "carrierID"]; ShortMessageServiceCarrierID carrierID; if (!string.IsNullOrEmpty(carrierIDAsText) && EnumEx.TryParse<ShortMessageServiceCarrierID>(carrierIDAsText, out carrierID)) { // execute var message = new ShortMessageServiceMessage { Phone = phone, CarrierID = carrierID, Body = smartForm.CreateMergedText(scopeKey + "textBody"), }; Exception ex; _smsClient.TrySend(message, out ex); smsSent++; } // prevent resends usedPhoneIDs.Add(phone.ToLowerInvariant()); } } usedPhoneIDs.Clear(); } return smsSent; }