【问题标题】:Email Attachments in HttpClientHttpClient 中的电子邮件附件
【发布时间】:2015-06-07 02:29:02
【问题描述】:

我目前正在使用 Mailgun 通过他们的 REST API 服务在我的应用程序中执行一些电子邮件发送。他们的示例使用了 RestSharp,它已经在我的项目中获得了 MS Web API 休息客户端,我不愿意为此功能安装另一个。标准电子邮件使用 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;

有什么想法吗?

【问题讨论】:

    标签: c# email attachment mailgun


    【解决方案1】:

    我创建了 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);
    

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

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

    希望能帮到你。

    【讨论】:

    • 似乎是正确的方法,但是如何将“to”、“from”、“subject”等添加到 MultipartFormDataContent 中?
    • 像这样:var content = new MultipartFormDataContent(); content.Add(new StringContent(subject), "subject");
    猜你喜欢
    • 1970-01-01
    • 2020-08-11
    • 1970-01-01
    • 1970-01-01
    • 2016-11-30
    • 2010-11-26
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多