我想知道您是否可以帮我使用RestSharp为电子邮件附件添加MemoryStream附件对象?
以下是我从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);
}
答案 0 :(得分:5)
够容易。使用将文件作为字节数组的重载。假设
using(var memoryStream = new MemoryStream(File.ReadAllBytes("test.jpg")))
或类似的地方,您需要做的只是
request.AddFile("attachment", memoryStream.ToArray(), "test.jpg");