Пример #1
0
 public static IRestResponse SendMail(string toEmail      = null, string subject = null, string body = null,
                                      string templateName = null, List <KeyValuePair <string, string> > templateParameters = null)
 {
     toEmail = string.IsNullOrEmpty(toEmail) ? DeveloperEmail() : toEmail;
     if (string.IsNullOrEmpty(toEmail))
     {
         return(new RestResponse
         {
             ErrorException = new ArgumentNullException(nameof(toEmail),
                                                        "Email requires a TO field to know where to send it or a developer email " +
                                                        "environment variable to notify this user of key developments.")
         });
     }
     try
     {
         RestClient client = new();
         client.BaseUrl       = new Uri(ResetPassword.MailApiUrl);
         client.Authenticator = new HttpBasicAuthenticator("api", ResetPassword.MailApiKey());
         RestRequest request = new();
         request.AddParameter("domain", ResetPassword.MailDomainName(), ParameterType.UrlSegment);
         request.Resource = "{domain}/messages";
         request.AddParameter("from", $"Wishlist Web <app201235650@{ResetPassword.MailDomainName()}>");
         request.AddParameter("to", toEmail);
         request.AddParameter("subject", subject);
         if (!string.IsNullOrEmpty(body))
         {
             request.AddParameter("text", body);
         }
         if (!string.IsNullOrEmpty(templateName))
         {
             request.AddParameter("template", templateName);
             if (templateParameters != null)
             {
                 var parameterJson = "";
                 foreach (var param in templateParameters)
                 {
                     parameterJson += GetJsonString(param.Key, param.Value);
                 }
                 request.AddParameter("h:X-Mailgun-Variables", "{" + parameterJson + "}");
             }
         }
         request.Method = Method.POST;
         var response = client.Execute(request);
         if (response.IsSuccessful)
         {
             return(response);
         }
         else
         {
             return(new RestResponse
             {
                 ErrorException = new Exception(response.StatusDescription),
                 ErrorMessage = response.Content
             });
         }
     }
     catch (Exception ex)
     {
         return(new RestResponse
         {
             ErrorException = ex,
             ErrorMessage = ex.Message
         });
     }
 }