Search code examples
c#templatessparkpost

Sparkpost C# API add attachments to templates


I've been going thumbing through the documentation and searching the internet to find documenation on how to add attachments to created templates. I'm using darrencauthon's CSharp-Sparkpost to handle the API calls. So far what I have is not working. Does anyone have a working solution (possible?) or a better solution for C#? I'm not opposed to using a different library. This is the link to CSharp-Sparkpost

Here's what I've got so far:

var t = new Transmission();

t.Content.From.Email = "from@thisperson.com";
t.Content.TemplateId = "my-template-email";

new Recipient
{
    Address = new Address { Email = recipient }
}
.Apply(t.Recipients.Add);


new Attachment
{
    Data = //CSVDATA,
    Name = "Table.csv",
    Type = "text/csv"
}.Apply(t.Content.Attachments.Add);


var client = new SparkPost.Client(Util.GetPassword("sparkpostapikey"));
client.Transmissions.Send(t).Wait();

I've verified that I can send this attachment without a template and also verified that I can send this template without the attachment. So... the Email is getting sent; however, the content received is only the template and substitution data. No attachment with the template email.


Solution

  • Using Darren's library, and combining the requirements for my project, this is the solution I've come up with. I'm just making an additional API call to grab the template Html so I can build the transmission without having to send the template_id. Still using the CSharp-Sparkpost library to make all of the calls. I modified Darren's example SendInline program as such:

        static async Task ExecuteEmailer()
        {
            var settings = ConfigurationManager.AppSettings;
            var fromAddr = settings["fromaddr"];
            var toAddr = settings["toaddr"];
    
            var trans = new Transmission();
    
            var to = new Recipient
            {
                Address = new Address
                {
                    Email = toAddr
                },
                SubstitutionData = new Dictionary<string, object>
                {
                    {"firstName", "Stranger"}
                }
            };
    
            trans.Recipients.Add(to);
    
            trans.SubstitutionData["firstName"] = "Sir or Madam";
    
            trans.Content.From.Email = fromAddr;
            trans.Content.Subject = "SparkPost sending attachment using template";
            trans.Content.Text = "Greetings {{firstName or 'recipient'}}\nHello from C# land.";
    
            //Add attachments to transmission object
            trans.Content.Attachments.Add(new Attachment()
            {
                Data = Convert.ToBase64String(System.IO.File.ReadAllBytes(@"C:\PathToFile\ExcelFile.xlsx")),
                Name = "ExcelFile.xlsx",
                Type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
            });
    
            Console.Write("Sending mail...");
    
            var client = new Client(settings["apikey"]);
            client.CustomSettings.SendingMode = SendingModes.Sync;
    
            //retrieve template html and set Content.Html
            var templateResponse = await client.Templates.Retrieve("template-email-test");
            trans.Content.Html = templateResponse.TemplateContent.Html;
    
            //Send transmission 
            var response = client.Transmissions.Send(trans);
    
            Console.WriteLine("done");
        }
    

    Template with attached file