protected virtual void SendNotification(OperationContext context, string notificationType, string mediaType, string recipient, Guid? userId, IDictionary<string, object> parameters)
 {
     var msg = new NotificationMessage() { Type = notificationType, MediaType = mediaType, Recipients = recipient, MainRecipientUserId = userId, Culture = context.UserCulture, Parameters = parameters };
       msg.From = _settings.DefaultEmailFrom;
       msg.Parameters[LoginNotificationKeys.BackHitUrlBase] = _settings.BackHitUrlBase;
       _notificationService.Send(context, msg);
 }
 public string GetTemplatedValue(IEntitySession session, NotificationMessage message, string part) {
   if (_templateService == null)
     return null;
   var templateName = message.Type + "." + message.MediaType + "." + part;
   //Try with template
   var template = _templateService.GetTemplate(session, templateName, message.Culture, null);
   if (template == null && message.Culture != "EN-US") 
     template = _templateService.GetTemplate(session, templateName, "EN-US"); //try again with EN-US
   Util.Check(template != null, "Template {0}, culture '{1}' not found in TextTemplate table.", templateName, message.Culture);  
   var text = _templateService.Transform(template, message.Parameters);
   return text;
 }
 protected virtual void SendPin(ILoginProcess process, ExtraFactorTypes factorType, string factor, string pin)
 {
     var session = EntityHelper.GetSession(process);
       string mediaType = GetMediaType(factorType);
       Util.CheckNotEmpty(mediaType, "Cannot send pin, unsupported factor type: {0}.", factorType);
       var notificationType = GetPinNotificationType(process.ProcessType);
       var userId = process.Login.UserId;
       var msg = new NotificationMessage() { Type = notificationType, MediaType = mediaType, Recipients = factor, MainRecipientUserId = userId, Culture = session.Context.UserCulture };
       msg.From = _settings.DefaultEmailFrom;
       msg.Parameters[LoginNotificationKeys.BackHitUrlBase] = _settings.BackHitUrlBase;
       msg.Parameters[LoginNotificationKeys.Pin] = pin;
       msg.Parameters[LoginNotificationKeys.ProcessToken] = process.Token;
       msg.Parameters[LoginNotificationKeys.UserName] = process.Login.UserName;
       _notificationService.Send(session.Context, msg);
 }
 public async Task SendAsync(OperationContext context, NotificationMessage message) {
   Util.CheckNotEmpty(message.Recipients, "Recipient(s) not specified.");
   try {
     var session = context.OpenSystemSession();
     var subject = message.GetString("Subject") ?? GetTemplatedValue(session, message, "Subject");
     var body = message.GetString("Body") ?? GetTemplatedValue(session, message, "Body");
     Util.CheckNotEmpty(message.From, "Email From address not specified in message.");
     Util.CheckNotEmpty(subject, "Subject not specified or Subject template '{0}.Subject' not found.", message.Type);
     Util.CheckNotEmpty(body, "Email body not specified or Body template '{0}.Body' not found.", message.Type);
     message.Status = MessageStatus.Sending;
     var mail = new MailMessage(message.From, message.Recipients, subject, body);
     await _emailService.SendAsync(context, mail);
     message.Status = MessageStatus.Sent;
   } catch (Exception ex) {
     message.Status = MessageStatus.Error;
     message.Error = ex.ToLogString(); 
   }
 }
 public async Task SendAsync(OperationContext context, NotificationMessage message) {
   message.AttemptCount++;
   var provider = _providers.FirstOrDefault(c => c.CanSend(message));
   var args = new NotificationEventArgs(message, provider);
   if (Sending != null) {
     Sending(this, args);
     if (message.Status == MessageStatus.Blocked || message.Status == MessageStatus.Sent) {
       if (_log != null)
         _log.LogMessage(context, message);
       return;
     }
     provider = args.Provider; 
   }
   Util.Check(provider != null, 
     "Notification service failed to find notification provider for a message, message type: '{0}', media type: '{1}'.", 
     message.Type, message.MediaType);
   await provider.SendAsync(context, message).ConfigureAwait(false); 
   if (Sent != null)
     Sent(this, args); 
   if (_log != null)
     _log.LogMessage(context, message);
 }
 public NotificationLogEntry(OperationContext context, NotificationMessage message)
     : base(context)
 {
     base.Id = Guid.NewGuid();
       Message = message;
 }
 public NotificationEventArgs(NotificationMessage message, INotificationProvider provider)
 {
     Message = message;
       Provider = provider;
 }
 public bool CanSend(NotificationMessage message) {
   return message.MediaType.Equals(NotificationMediaTypes.Email, StringComparison.InvariantCultureIgnoreCase);  
 }
 public Guid LogMessage(OperationContext context, NotificationMessage message)
 {
     var entry = new NotificationLogEntry(context, message);
       _backgroundSave.AddObject(entry);
       return entry.Id.Value; //it is always there
 }
Exemple #10
0
 public void Send(OperationContext context, NotificationMessage message) {
   AsyncHelper.RunSync(() => SendAsync(context, message));
 }