Как прикрепить объект MemoryStream в электронном письме с restsharp?

StackOverflow https://stackoverflow.com//questions/24023610

Вопрос

Интересно, можете ли вы помочь мне добавить объект вложения MemoryStream для вложения электронной почты с помощью RestSharp?

Вот код, который я получил от MailGun, но он присоединяет физический файл в этом примере:

Спасибо.

public static IRestResponse SendComplexMessage() {
       RestClient client = new RestClient();
       client.BaseUrl = "https://api.mailgun.net/v2";
       client.Authenticator =
               new HttpBasicAuthenticator("api",
                                          "key-3ax6xnXXXXXXXX");
       RestRequest request = new RestRequest();
       request.AddParameter("domain",
                            "samples.mailgun.org", ParameterType.UrlSegment);
       request.Resource = "{domain}/messages";
       request.AddParameter("from", "Excited User <me@samples.mailgun.org>");
       request.AddParameter("to", "foo@example.com");
       request.AddParameter("cc", "baz@example.com");
       request.AddParameter("bcc", "bar@example.com");
       request.AddParameter("subject", "Hello");
       request.AddParameter("text", "Testing some Mailgun awesomness!");
       request.AddParameter("html", "<html>HTML version of the body</html>");

// I need to add memorystream object here ---
       request.AddFile("attachment", Path.Combine("files", "test.jpg"));
       request.AddFile("attachment", Path.Combine("files","test.txt"));
// -------------------------------------------

       request.Method = Method.POST;
       return client.Execute(request);
}
.

Это было полезно?

Решение

достаточно легко.Используйте перегрузку, которая принимает файл в виде байтового массива.Предполагая, что

using(var memoryStream = new MemoryStream(File.ReadAllBytes("test.jpg")))
.

или похожее где-то выше, все, что вам нужно сделать, это

request.AddFile("attachment", memoryStream.ToArray(), "test.jpg");    
.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top