HttpClient中的电子邮件附件

时间:2015-04-02 07:55:53

标签: c# email attachment mailgun

我目前正在使用Mailgun通过他们的REST API服务在我的应用程序中执行一些电子邮件发送。他们的示例使用RestSharp,它已经在我的项目中获得了MS Web API rest客户端,我不愿意为此功能安装另一个。标准电子邮件使用HttpClient可以正常工作,但是当添加附件时,我有点不知所措。

发送带附件的电子邮件的代码如下......

RestClient client = new RestClient();

client.BaseUrl = new Uri("https://api.mailgun.net/v3");
client.Authenticator = new HttpBasicAuthenticator("api", "MailgunKeyGoesHere");
RestRequest request = new RestRequest();
request.AddParameter("domain",
               "mailgundomain.mailgun.org", ParameterType.UrlSegment);
request.Resource = "{domain}/messages";
request.AddParameter("from", "Mailgun Sandbox <postmaster@mailgundomain.mailgun.org>");
request.AddParameter("to", "My Email <myemail@testdomain.co.uk>");
request.AddParameter("subject", "Hello");
request.AddParameter("text", "This is the test content");

request.AddFile("attachment", Path.Combine("C:\\temp", "test.jpg"));

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

这在我在Linqpad中运行测试时效果很好。然而,我的代码并不是我似乎无法看到该做什么。

var client = new HttpClient();

client.BaseAddress = new Uri(string.Format("{0}/{1}/messages", @"https://api.mailgun.net/v3", "mailgundomain.mailgun.org"));
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", "MailgunKeyGoesHere");

var kvpContent = new List<KeyValuePair<string, string>>
{
   new KeyValuePair<string, string>("Content-Disposition: form-data; name=\"from\"", "Mailgun Sandbox <postmaster@mailgundomain.mailgun.org>"),
   new KeyValuePair<string, string>("Content-Disposition: form-data; name=\"subject\"", "Test Email"),
   new KeyValuePair<string, string>("Content-Disposition: form-data; name=\"text\"", "It Worked!!"),
   new KeyValuePair<string, string>("Content-Disposition: form-data; name=\"to\"", "My Email <myemail@testdomain.co.uk>"),
};

var fileData = File.ReadAllBytes(@"C:\Temp\test.jpg");

//This is where it goes wrong. I know at the moment fileData.ToString() is wrong but this is the last thing I tried
kvpContent.Add(new KeyValuePair<string, string>("Content-Disposition: form-data; name=\"attachment\"; filename=\"test.jpg\" Content-Type: application/octet-stream",
                  fileData.ToString()));


var formContent = new FormUrlEncodedContent(kvpContent);

var response = client.PostAsync(client.BaseAddress, formContent).Result;

任何想法?

1 个答案:

答案 0 :(得分:3)

我创建了MultipartFormDataContent而不是FormUrlEncodedContent,并在MultipartFormDataContent对象上添加了内容。

您可以通过以下方式添加创建ByteArrayContent对象的附件:

ByteArrayContent fileContent = new ByteArrayContent(File.ReadAllBytes(filePath));

fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                 {
                      Name = "attachment",
                      FileName = "MyAttachment.pdf"
                 };

content.Add(fileContent);

where content是我的MultipartFormDataContent对象,我在HTTP Post方法中传递了关于HttpClient的这个对象。例如:

HttpResponseMessage response = client.PostAsync(url, content).Result; 

我希望能有所帮助。