<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Mike Hillyer</title>
    <description>The latest articles on Forem by Mike Hillyer (@mikehillyer).</description>
    <link>https://forem.com/mikehillyer</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F33595%2Fb957c7c0-c697-4dc9-8f8e-e8bc6aea54c7.jpg</url>
      <title>Forem: Mike Hillyer</title>
      <link>https://forem.com/mikehillyer</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mikehillyer"/>
    <language>en</language>
    <item>
      <title>Using the SparkPost Metrics API with C#</title>
      <dc:creator>Mike Hillyer</dc:creator>
      <pubDate>Mon, 16 Oct 2017 13:02:48 +0000</pubDate>
      <link>https://forem.com/sparkpost/using-the-sparkpost-metrics-api-with-c-83f</link>
      <guid>https://forem.com/sparkpost/using-the-sparkpost-metrics-api-with-c-83f</guid>
      <description>&lt;p&gt;*Today’s blog is written by &lt;a href="https://twitter.com/darrencauthon"&gt;Darren Cauthon&lt;/a&gt;, community member and original author of the &lt;a href="https://github.com/SparkPost/csharp"&gt;SparkPost C# Library&lt;/a&gt;. Thanks for the post, Darren! We hope you enjoy.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to Use the SparkPost Metrics API with C
&lt;/h3&gt;

&lt;p&gt;In our first two tutorials on using SparkPost with C#, we learned how to &lt;a href="https://dev.to/sparkpost/getting-started-with-c-and-sparkpost"&gt;send an email from a C# application&lt;/a&gt; and &lt;a href="https://dev.to/sparkpost/using-sparkpost-templates-with-c-c8h"&gt;how to manipulate email templates using C# code&lt;/a&gt;. You’ll want to start there to get the SparkPost C# library installed via NuGet, it’s all good stuff.&lt;/p&gt;

&lt;p&gt;But most of the time when you send an email from your app, you’ll also want to know what happened to that message: was it delivered? Did your user open it or even click on link in the message body? Visibility into message disposition and performance is crucial for many apps.&lt;/p&gt;

&lt;p&gt;That’s why the SparkPost C# library includes methods for getting important metrics about the emails that you send. Today, we’re going to look at how they work.&lt;/p&gt;

&lt;p&gt;There are two mechanisms for getting these analytics: pushing and pulling. With pushing (via a webhook), you can get your metrics streamed in near real-time as SparkPost posts the information to a web endpoint that you provide. A future tutorial will provide some help on setting up a webhook endpoint in a standard .Net MVC application.&lt;/p&gt;

&lt;p&gt;Today, however, we’ll focus on pull-style API-based queries for your metrics via the C# wrapper. You can query the SparkPost API for just about any type of information you’d want about the emails you send within the past few weeks, and the API will return the information you requested.&lt;/p&gt;

&lt;p&gt;This tutorial will also provide some insight into the library’s design, and how you can use it for other SparkPost API calls you’d want to make.&lt;/p&gt;

&lt;h3&gt;
  
  
  A SparkPost Metrics API Quick Start
&lt;/h3&gt;

&lt;p&gt;When using the C# SparkPost library, the first place to check is the &lt;a href="https://developers.sparkpost.com/api/"&gt;SparkPost API documentation&lt;/a&gt;. The C# library was written to mirror the SparkPost web API as closely as possible. Naturally, the &lt;a href="https://developers.sparkpost.com/api/metrics.html"&gt;API docs include specific information about metrics&lt;/a&gt;. Look there to see what sorts of metrics you might want to gather.&lt;/p&gt;

&lt;p&gt;Let’s try the first method in the docs: Gathering metrics by domain. Let’s build a sample request that will return the number of accepted and bounced emails that were sent to Gmail or Yahoo Mail. Here is C# code that will return your results.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var client = new Client("YOUR API KEY");
client.CustomSettings.SendingMode = SendingModes.Sync; 

var query = new MetricsQuery {
    From = DateTime.Now.AddDays(-7),
    Metrics = new[] { "count_accepted", "count_bounce" },
    Domains = new[] { "gmail.com", "yahoo.com" },
};

var response = await client.Metrics.GetDeliverabilityByDomain(query);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here are your results:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yjw3q0AY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://media.sparkpost.com/uploads/2017/10/Screen-Shot-2017-10-13-at-2.31.52-PM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yjw3q0AY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://media.sparkpost.com/uploads/2017/10/Screen-Shot-2017-10-13-at-2.31.52-PM.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It’s really that easy. All of the metrics data you want, for this and any of the other API metric methods, will be returned in this form. The only real question is what data you want, so check out the SparkPost API documentation and make the query you want!&lt;/p&gt;

&lt;h3&gt;
  
  
  How to Keep the SparkPost C# Library Up-To-Date with API Changes
&lt;/h3&gt;

&lt;p&gt;One of the difficulties in using a library for a service like SparkPost is keeping up-to-date with changes. SparkPost is always adding new features to its API, but are those new features reflected in the library? Sometimes there is a lag between when API developers add features and when library authors add support.&lt;/p&gt;

&lt;p&gt;The C# library tries to mitigate those issues by allowing you to extend the library, yourself. The example above showed how to use the MetricsQuery, but you can pass any object to use as a query—so long as it matches the style and naming of the SparkPost service.&lt;/p&gt;

&lt;p&gt;Here is a concrete example: SparkPost developers added a new API feature, the ability to pass a “limit to limit the size of your result. That feature was not available when the MetricsQuery class was written, so it does not have Limit available. So does that mean you can’t use Limit in C#? No, that’s still available to you – just have to pass it yourself. Like with an anonymous object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var client = new Client("YOUR API KEY");
client.CustomSettings.SendingMode = SendingModes.Sync; 

var response = client.Metrics.GetDeliverabilityByDomain(new {
  From = DateTime.Now.AddDays(-0),
  Metrics = "count_accepted,count_bounce",
  Domains = "gmail.com,yahoo.com",
  Limit = 10
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another method would be to extend the MetricsQuery and add an “int Limit { get; set; } property. But the main point to notice is that so long as you stay consistent with the SparkPost documentation and naming, this library can extend itself.&lt;/p&gt;

&lt;h3&gt;
  
  
  The SparkPost C# Library Is Open Source
&lt;/h3&gt;

&lt;p&gt;I’m the original author of the &lt;a href="https://github.com/SparkPost/csharp"&gt;SparkPost library&lt;/a&gt;, but there are more than a dozen developers who have contributed code to this library, like this Metrics library, which was contributed by Aaron Sherber. Each developer on this library was either a SparkPost employee or a customer of SparkPost, all of us with an interest in making this library work well.&lt;/p&gt;

&lt;p&gt;As the SparkPost API expands, this library will expand with it. And we’re accepting contributions from anybody who wants to help!&lt;/p&gt;

&lt;p&gt;The post &lt;a href="https://www.sparkpost.com/blog/metrics-api-csharp/"&gt;Using the SparkPost Metrics API with C#&lt;/a&gt; appeared first on &lt;a href="https://www.sparkpost.com"&gt;SparkPost&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>c</category>
      <category>csharp</category>
      <category>sdk</category>
      <category>metrics</category>
    </item>
    <item>
      <title>Using SparkPost Templates with C#</title>
      <dc:creator>Mike Hillyer</dc:creator>
      <pubDate>Mon, 02 Oct 2017 13:05:01 +0000</pubDate>
      <link>https://forem.com/sparkpost/using-sparkpost-templates-with-c-c8h</link>
      <guid>https://forem.com/sparkpost/using-sparkpost-templates-with-c-c8h</guid>
      <description>

</description>
      <category>csharp</category>
      <category>email</category>
      <category>howto</category>
      <category>c</category>
    </item>
    <item>
      <title>Getting Started with C# and SparkPost</title>
      <dc:creator>Mike Hillyer</dc:creator>
      <pubDate>Wed, 13 Sep 2017 13:01:58 +0000</pubDate>
      <link>https://forem.com/sparkpost/getting-started-with-c-and-sparkpost</link>
      <guid>https://forem.com/sparkpost/getting-started-with-c-and-sparkpost</guid>
      <description>&lt;h3&gt;
  
  
  There’s a Better Way to Send Email in C#
&lt;/h3&gt;

&lt;p&gt;Sending email is a very common need for C# applications. Think about some of the use cases almost every app needs to handle: new user welcomes, event notifications, password resets, and so on. These transactional emails are workhorses, and it’s really important that they get to your users. As a C# developer, what’s the best way to get them done?&lt;/p&gt;

&lt;p&gt;There are several C# email libraries out there, including system.net.mail, systems.web.mail. SmtpClient, MailKit, MimeKit, etc., but they all have one thing in common: you need an SMTP server to use them. Even if you have access to a server, it’s probably not tuned to ensure your messages get to your user’s inbox quickly and consistently. Using the SparkPost email delivery service is an easier way.&lt;/p&gt;

&lt;p&gt;Let’s give it a try. Here’s how to send a message using SparkPost and C#.&lt;/p&gt;

&lt;h3&gt;
  
  
  Hello, World! I’m Sending Email with C# and SparkPost
&lt;/h3&gt;

&lt;p&gt;The first step in sending email using C# with SparkPost is to &lt;a href="https://pages.sparkpost.com/bl-account-signup-generic.html?src=Blog&amp;amp;pc=bl-rfi-generic-2017&amp;amp;utm_source=blog&amp;amp;utm_medium=blog&amp;amp;utm_campaign=all&amp;amp;utm_content=signup"&gt;sign up for a SparkPost developer account&lt;/a&gt; and get your sending domain configured. Follow our &lt;a href="https://www.sparkpost.com/docs/getting-started/getting-started-sparkpost/"&gt;Getting Started Guide&lt;/a&gt; to get your account set up right with a sending domain. I’ll wait right here.&lt;/p&gt;

&lt;p&gt;With your account set up, your sending domain configured, and your API key in hand, you’re ready to send your first email using C#. Create an application and install Sparkpost Nuget package from your Nuget &lt;a href="http://docs.nuget.org/consume/package-manager-console"&gt;Package Manager Console&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;PM&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Install&lt;/span&gt;&lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="n"&gt;Package&lt;/span&gt; &lt;span class="n"&gt;SparkPost&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With SparkPost installed, we can format and send our first C# email message:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// start by creating a new SparkPost transmission&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;transmission&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;SparkPost&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Transmission&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;transmission&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Content&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;From&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Email&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"me@YOURSENDINGDOMAIN.com"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;transmission&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Content&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Subject&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello from SparkPost!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;transmission&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Content&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Text&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"This is a test email."&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// add recipients who will receive your email&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;recipient&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Recipient&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Address&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Address&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;Email&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"you@YOUREMAILADDRESS.com"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="n"&gt;transmission&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Recipients&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;recipient&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// create a new API client using your API key&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&amp;lt;YOUR API KEY HERE&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// if you do not understand async/await, use the sync sending mode:&lt;/span&gt;
&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CustomSettings&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SendingMode&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;SendingModes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Sync&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Transmissions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;transmission&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// if you understand async/await, use the default async sending mode:&lt;/span&gt;
&lt;span class="c1"&gt;// var response = client.Transmissions.Send(transmission);&lt;/span&gt;

&lt;span class="c1"&gt;// to get the transmission id back from SparkPost&lt;/span&gt;
&lt;span class="c1"&gt;// response.Result.Id;&lt;/span&gt;

&lt;span class="c1"&gt;// to get the status code of the SparkPost response&lt;/span&gt;
&lt;span class="c1"&gt;// response.Result.StatusCode;&lt;/span&gt;

&lt;span class="c1"&gt;// to get the raw json result from SparkPost&lt;/span&gt;
&lt;span class="c1"&gt;// response.Result.Content;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that’s it! Launch the application and you should see a test message in your inbox. And the best part? You don’t have to manage any servers, monitor any services, or worry about whether the message will reach its recipient. You take care of deciding what to send and to whom, and SparkPost takes care of the rest.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stay Tuned for More C# Email Tips
&lt;/h3&gt;

&lt;p&gt;You’re well on your way to being a C# email master, thanks to one simple API. You have a working sending domain, you’ve sent your first message, and soon you will conquer the email world!&lt;/p&gt;

&lt;p&gt;That said, your journey is not yet complete. Do you really want to have to combine content and design by building your email templates in code? Of course not! Stay tuned and I will show you how to separate the content of the message out of your API call and pre-store the message template, simplifying your code even further.&lt;/p&gt;

&lt;p&gt;Till then, &lt;a href="https://pages.sparkpost.com/bl-account-signup-generic.html?src=Blog&amp;amp;pc=bl-rfi-generic-2017&amp;amp;utm_source=blog&amp;amp;utm_medium=blog&amp;amp;utm_campaign=all&amp;amp;utm_content=signup"&gt;sign up for SparkPost&lt;/a&gt;, &lt;a href="https://www.sparkpost.com/docs/getting-started/getting-started-sparkpost/#preparing-your-from-address"&gt;set up your domain&lt;/a&gt;, and check out our &lt;a href="https://developer.sparkpost.com/api/"&gt;great API documentation&lt;/a&gt;! If you have any other questions around C# and SparkPost, come to the #csharp channel in our &lt;a href="http://slack.sparkpost.com"&gt;community slack&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This post was originally published on &lt;a href="https://www.sparkpost.com/blog/csharp-and-sparkpost/"&gt;sparkpost.com&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>api</category>
      <category>csharp</category>
      <category>beginners</category>
      <category>sdk</category>
    </item>
  </channel>
</rss>
