Mailgun 使用 RestSharp 发送附件

Mailgun sending attachment with RestSharp

我正在使用 RestSharp 尝试使用 Mailgun API 发送附件。我尝试使用硬编码路径从系统中的文件附加,也尝试使用 varbinary(MAX) (SQL 服务器) [=25] 上的 ToArray() 方法从存储在数据库中的二进制文件附加=] 都没有成功。

附件技术上发送,但是当电子邮件到达我的收件箱时,文件大小总是大约 302 字节大并且总是损坏。我尝试了 3 个不同的文件,每次都遇到同样的问题。

电子邮件的其余部分发送、传递和显示正常。只是附件坏了。

代码分解:

// Doesnt work(Data property is varbinary(MAX)
request.AddFileBytes("attachment",databaseModel.Data.ToArray(),databaseModel.Filename, "multipart/form-data");

// Also doesnt work(Data property is varbinary(MAX)
request.AddFile("attachment",databaseModel.Data.ToArray(),databaseModel.Filename, "multipart/form-data");

// Also doesnt work        
var path = @"D:\Template.pdf";
request.AddFile("attachment",path,"multipart/form-data");

我想通了..

不应该在 request.AddFile();

上添加 "multipart/form-data"

删除它可以解决问题。

此代码有效:

public static void Main(string[] args)
{
    Console.WriteLine(SendSimpleMessage().Content.ToString());
    Console.ReadLine();
}

public static IRestResponse SendSimpleMessage()
{
    var path1 = @"C:\Users\User\Pictures\website preview";
    var fileName = "Learn.png";
    RestClient client = new RestClient();
    client.BaseUrl = new Uri("https://api.mailgun.net/v3");
    client.Authenticator =
        new HttpBasicAuthenticator("api",
                                    "key-934345306fead7de0296ec2fb96a143");
    RestRequest request = new RestRequest();
    request.AddParameter("domain", "mydomain.info", ParameterType.UrlSegment);
    request.Resource = "{domain}/messages";
    request.AddParameter("from", "Excited User <example@mydomain.info>");
    request.AddParameter("to", "peter.cech@gmail.com");        
    request.AddParameter("subject", "Hello");
    request.AddParameter("text", "Testing some Mailgun awesomness! This is all about the text only. Just testing the text of this email.";
    request.AddFile("attachment", Path.Combine(path1,fileName));
    request.Method = Method.POST;
    return client.Execute(request);
}