2

我正在使用官方的 C# Mailjet SDK ( https://github.com/mailjet/mailjet-apiv3-dotnet )。到目前为止工作正常。

但是如何添加附件?

我懂了

Mailjet.Client.Resources

有 InlineAttachments 和 Attachments,但我该如何使用呢?

这是到目前为止的代码片段:

        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() } } });

试过了。喜欢

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

但这不起作用。

更新

像这样工作:

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

1 回答 1

0

根据他们的文档,这样做的方法是:

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", "pilot@mailjet.com"},
              {"Name", "Mailjet Pilot"}
              }},
             {"To", new JArray {
              new JObject {
               {"Email", "passenger1@mailjet.com"},
               {"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()));
     }
于 2018-05-04T08:58:43.510 回答