C# Mailjet SDK: Adding attachment

1.6k views Asked by At

I am using the official C# Mailjet SDK (https://github.com/mailjet/mailjet-apiv3-dotnet). Works fine so far.

But how do I add attachments?

I see

Mailjet.Client.Resources

has InlineAttachments and Attachments, but how do I use it?

This is the code snippet so far:

        MailjetRequest request = new MailjetRequest { Resource = Send.Resource }
            .Property(Send.FromEmail, emailOperatable.FromEmailaddress)
            .Property(Send.FromName, emailOperatable.FromName)
            .Property(Send.Subject, emailOperatable.Subject)
            .Property(Send.TextPart, emailOperatable.TextBody)
            .Property(Send.HtmlPart, emailOperatable.HtmlBody)
            .Property(Send.Recipients, new JArray { new JObject { { "Email", emailOperatable.ContactEmailaddress }, { "Name", emailOperatable.CreateSendToName() } } });

Tried sth. like

request.Property(Send.Attachments, "path/to/file.zip");

But that does not work.

Update

Works like this:

 .Property(Send.Attachments, new JArray { new JObject { { "Content-Type", "<content type>" }, { "Filename", "<file name>" }, { "content", "<base 64 encoded content>" } } });
2

There are 2 answers

0
JVK On

It appears that the naming is a bit different, this worked for me in 2022 with the API v3

Mind the capitalization of some letters (or the lack thereof...)! Note: content field is base64 encoded filedata.

.Property(
                    Send.Attachments,
                    new JArray {
                      new JObject {
                           {"Content-type", "text/plain"},
                           {"Filename", "test.txt"},
                           {"content", "VGhpcyBpcyB5b3VyIGF0dGFjaGVkIGZpbGUhISEK"}
                      }
                    }
                )
0
facundofarias On

According to their docs, the way to do it is:

MailjetClient client = new MailjetClient(Environment.GetEnvironmentVariable("MJ_APIKEY_PUBLIC"), Environment.GetEnvironmentVariable("MJ_APIKEY_PRIVATE"))
     {
        Version = ApiVersion.V3_1,
     };
     MailjetRequest request = new MailjetRequest
     {
        Resource = Send.Resource,
     }
        .Property(Send.Messages, new JArray {
            new JObject {
             {"From", new JObject {
              {"Email", "[email protected]"},
              {"Name", "Mailjet Pilot"}
              }},
             {"To", new JArray {
              new JObject {
               {"Email", "[email protected]"},
               {"Name", "passenger 1"}
               }
              }},
             {"Subject", "Your email flight plan!"},
             {"TextPart", "Dear passenger 1, welcome to Mailjet! May the delivery force be with you!"},
             {"HTMLPart", "<h3>Dear passenger 1, welcome to Mailjet!</h3><br />May the delivery force be with you!"},
             {"Attachments", new JArray {
              new JObject {
               {"ContentType", "text/plain"},
               {"Filename", "test.txt"},
               {"Base64Content", "VGhpcyBpcyB5b3VyIGF0dGFjaGVkIGZpbGUhISEK"}
               }
              }}
             }
            });
     MailjetResponse response = await client.PostAsync(request);
     if (response.IsSuccessStatusCode)
     {
        Console.WriteLine(string.Format("Total: {0}, Count: {1}\n", response.GetTotal(), response.GetCount()));
        Console.WriteLine(response.GetData());
     }
     else
     {
        Console.WriteLine(string.Format("StatusCode: {0}\n", response.StatusCode));
        Console.WriteLine(string.Format("ErrorInfo: {0}\n", response.GetErrorInfo()));
        Console.WriteLine(response.GetData());
        Console.WriteLine(string.Format("ErrorMessage: {0}\n", response.GetErrorMessage()));
     }