RESTSHARP가있는 이메일에 MemoryStream 객체를 첨부하는 방법은 무엇입니까?
-
21-12-2019 - |
문제
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");
. 제휴하지 않습니다 StackOverflow