【问题标题】:Attaching to sendgrid api not working when using sendgrid mail helper使用 sendgrid 邮件帮助程序时附加到 sendgrid api 不起作用
【发布时间】:2016-10-17 17:31:39
【问题描述】:

我已经查看了我可以在此处找到的有关将文件附加到 sendgrid 电子邮件的问题,但似乎没有一个问题与我有关。

我的问题是这个。如何使用 api 在 sendgrid 中发送带有附件的电子邮件?

 dynamic sg = new SendGridAPIClient(apiKey);
        var from = new SendGrid.Helpers.Mail.Email("jkennedy@domain.com");
        var subject = "Hello World from the SendGrid C# Library!";
        var to = new SendGrid.Helpers.Mail.Email(toAddress);
        var content = new Content("multipart/form-data", "Textual content");
        var attachment = new Attachment {Filename = attachmentPath };
        var mail = new Mail(from, subject, to, content);

        var ret = mail.Get();

        mail.AddAttachment(attachment);


        dynamic response = await sg.client.mail.send.post(requestBody: ret);

如果我在获取邮件发送后放置 mail.attachment 但没有附件。如果我将 addattachment 行放在 get 之前,我会收到“错误请求”消息。

我还没有找到具体如何做到这一点的示例。

另外,文件的路径是 c:\tblaccudatacounts.csv

【问题讨论】:

    标签: c# sendgrid


    【解决方案1】:

    在为此苦苦挣扎了几个小时后,我使用 sendgrid 的 V3 API 找到了答案。这是我学到的。

    在您的示例中,您在添加附件之前调用var ret = mail.Get();。由于mail.Get() 本质上是将邮件对象序列化为SendGrid 所期望的Json 格式,所以在mail.Get() 调用之后添加附件实际上不会将其添加到邮件对象中。

    您应该知道的另一件事是,API 没有简单地将文件路径作为输入的方法(至少我能找到,希望有人能纠正我)。您至少需要手动设置内容(作为 base 64 字符串)和文件名。您可以找到更多信息here

    这是我的工作解决方案:

    string apiKey = "your API Key";
    dynamic sg = new SendGridAPIClient(apiKey);
    
    Email from = new Email("your@domain.com");
    string subject = "Hello World from the SendGrid CSharp Library!";
    Email to = new Email("destination@there.com");
    Content body = new Content("text/plain", "Hello, Email!");
    Mail mail = new Mail(from, subject, to, body);
    
    byte[] bytes = File.ReadAllBytes("C:/dev/datafiles/testData.txt");
    string fileContentsAsBase64 = Convert.ToBase64String(bytes);
    
    var attachment = new Attachment
    {
        Filename = "YourFile.txt",
        Type = "txt/plain",
        Content = fileContentsAsBase64
    };
    
    mail.AddAttachment(attachment);
    
    dynamic response = await sg.client.mail.send.post(requestBody: mail.Get());
    

    【讨论】:

    • 是的,从 V3 开始,他们已经引入了这种 Base64 编码数据用于附件。在此之前它可以正常工作。
    【解决方案2】:

    我想通了。我正在使用第三方编写的助手。我选择了 SendGrid 的实际建议。请参阅下面的代码,该代码现在正在运行。

    var myMessage = new SendGridMessage {From = new MailAddress("info@email.com")};
            myMessage.AddTo("Jeff Kennedy <info@info.com>");
            myMessage.Subject = "test email";
            myMessage.Html = "<p>See Attachedment for Lead</p>";
            myMessage.Text = "Hello World plain text!";
            myMessage.AddAttachment("C:\\tblaccudatacounts.csv");
            var apiKey = "apikey given by sendgrid";
            var transportWeb = new Web(apiKey);
            await transportWeb.DeliverAsync(myMessage);
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-21
    相关资源
    最近更新 更多