public async Task <IResponse> SendAsync(IPostbode postbode)
        {
            if (!postbode.Mail.Content.IsMime)
            {
                _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes("api" + ":" + ApiKey)));


                var mail = postbode.Mail;

                var form = new Dictionary <string, string>();
                form["from"]    = mail.From.ToString();
                form["to"]      = string.Join(",", mail.To.Select(x => x.ToString()));
                form["subject"] = mail.Subject;
                form["text"]    = mail.Content.Content;

                if (postbode.Mail.Headers != null)
                {
                    foreach (var keyValuePair in postbode.Mail.Headers)
                    {
                        if (!form.ContainsKey(keyValuePair.Key))
                        {
                            form.Add(keyValuePair.Key, keyValuePair.Value);
                        }
                    }
                }
                var response = await _httpClient.PostAsync("https://api.mailgun.net/v2/" + Domain + "/messages", new FormUrlEncodedContent(form));

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    return(new Response(true, response.ReasonPhrase));
                }
                return(new Response(false, response.ReasonPhrase));
            }
            throw new NoMimeDeliverySetException();
        }
Example #2
0
        public async Task <IResponse> SendAsync(IPostbode postbode)
        {
            if (!postbode.Mail.Content.IsMime)
            {
                _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", ApiKey);

                //nasty way to prevent external json lib dependecy
                var data = "{\"personalizations\": " + generatePersJson(postbode) + "," +
                           " \"from\": {\"email\": \"" + postbode.Mail.From + "\"}," +
                           " \"content\": [{\"type\": \"" + postbode.Mail.Content.Type + "\", \"value\": \"" + postbode.Mail.Content.Content + "\"}]" +
                           "}";


                var result = await _httpClient.PostAsync("https://api.sendgrid.com/v3/mail/send", new StringContent(data, Encoding.UTF8, "application/json"));

                var resultContent = await result.Content.ReadAsStringAsync();

                return(new Response(result.IsSuccessStatusCode, resultContent));
            }
            throw new NoMimeDeliverySetException();
        }
 public Task <IResponse> SendAsync(IPostbode service = null)
 {
     return(Task.FromResult <IResponse>(new Response(true, service.Mail.Content.Content)));
 }
 public static IPostbode UseMailgun(this IPostbode postbode, string domain = null, string apikey = null)
 {
     return(postbode.Use(new MailgunDeliveryService(domain, apikey)));
 }
Example #5
0
 public static IPostbode UseSendgrid(this IPostbode postbode, string apikey = null)
 {
     postbode.Use(new SendgridDeliveryService(apikey));
     return(postbode);
 }
Example #6
0
 private string generatePersJson(IPostbode postbode)
 {
     return("[{\"to\": [" + string.Join(",", postbode.Mail.To.Select(x => "{\"email\": \"" + x.Email + "\"}")) + "],\"subject\": \"" + postbode.Mail.Subject + "\"}]");
 }
Example #7
0
 public static IPostbode SetHtmlContent(this IPostbode postbode, string html)
 {
     postbode.SetContent(new HtmlContent(html));
     return(postbode);
 }
Example #8
0
 public static IPostbode SetMimeKitContent(this IPostbode postbode, MimeEntity content)
 {
     postbode.SetContent(new MimeKitContent(content));
     return(postbode);
 }
Example #9
0
 public static IPostbode SetTextContent(this IPostbode postbode, string content)
 {
     postbode.SetContent(new TextContent(content));
     return(postbode);
 }
Example #10
0
 public static IPostbode UseSmtp(this IPostbode postbode)
 {
     postbode.Use(new SmtpDeliveryService());
     return(postbode);
 }
Example #11
0
 public Task <IResponse> SendAsync(IPostbode service = null)
 {
     throw new NotImplementedException("Waiting for .NET Core 1.2.0");
 }