public JsonResult SubscribeToNewsletter(SubscriptionViewModel subscription)
 {
     if (ModelState.IsValid)
     {
         SubscriptionDTO subscriptiondto = Mapper.Map <SubscriptionViewModel, SubscriptionDTO>(subscription);
         if (CodeFirstSecurity.IsAuthenticated)
         {
             subscriptiondto.IsConfirmed = true;
         }
         try
         {
             string token = SubscriptionServices.SubscribeToNewsletter(subscriptiondto);
             if (!CodeFirstSecurity.IsAuthenticated)
             {
                 SmtpClient client = new SmtpClient {
                     Host = BgResources.Email_Server, Port = Int32.Parse(BgResources.Email_SmtpPort), EnableSsl = BgResources.Email_SSL, Credentials = new NetworkCredential(BgResources.Email_UserName, BgResources.Email_Password)
                 };
                 UserMailer.ConfirmSubscription(token, subscriptiondto.SubscriberEmail, subscriptiondto).Send(new SmtpClientWrapper {
                     InnerSmtpClient = client
                 });
                 return(Json(new { result = "ok", message = String.Format(Resources.AppMessages.Subscription_Newsletter_ConfirmationMail_Sent, subscription.SubscriberEmail) }));
             }
             return(Json(new { result = "ok", message = Resources.AppMessages.ConfirmSubscription_Thanks }));
         }
         catch (ApplicationValidationErrorsException ex)
         {
             foreach (string str in ex.ValidationErrors)
             {
                 ModelState.AddModelError("", str);
             }
             return(Json(new { result = "error", errors = ModelState.Where(s => s.Value.Errors.Count > 0).Select(s => new KeyValuePair <string, string>(s.Key, s.Value.Errors.First().ErrorMessage)).ToArray(), modelerror = false }));
         }
         catch (SmtpException ex)
         {
             SubscriptionServices.DeleteSubscription(subscription.SubscriberEmail, SubscriptionType.Newsletter);
             return(Json(new { result = "error", errors = new List <KeyValuePair <string, string> > {
                                   new KeyValuePair <string, string>("", Resources.AppMessages.Error_SendMail)
                               }, modelerror = false }));
         }
     }
     return(Json(new { result = "error", errors = ModelState.Where(s => s.Value.Errors.Count > 0).Select(s => new KeyValuePair <string, string>(s.Key, s.Value.Errors.First().ErrorMessage)).ToArray(), modelerror = true }));
 }