【问题标题】:Azure Function Sengrid - Attachment only working locallyAzure Function Sengrid - 附件仅在本地工作
【发布时间】:2018-08-15 11:20:00
【问题描述】:

我正在尝试将文件添加到 azure 函数发送的邮件中。

这是我的功能:

#r "Newtonsoft.Json"
#r "SendGrid"
#r "System.Web"

using System.Web;
using SendGrid.Helpers.Mail;
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Http;
using System.IO;
using Microsoft.Azure.WebJobs.Host;
using System;

public static IActionResult Run(HttpRequest req, TraceWriter log, out SendGridMessage message)
{
    log.Info("C# HTTP trigger function processed a request.");

    string requestBody = new StreamReader(req.Body).ReadToEnd();
    EmailContent data = JsonConvert.DeserializeObject<EmailContent>(requestBody);

    if (data == null)
    {
        throw new ArgumentNullException("Data could not be null");
    }

    message = new SendGridMessage();

    message.AddTo(data.Email);
    message.SetFrom(new EmailAddress("no-reply@netflio.com"));
    message.AddContent("text/html", HttpUtility.HtmlDecode(data.Body));
    message.AddAttachment(data.AttachmentName, Convert.ToBase64String(data.Attachment));
    message.SetSubject(data.Subject);

    return (ActionResult)new OkObjectResult("OK");
}

public class EmailContent
{

    public string Email { get; set; }
    public string Subject { get; set; }
    public string Body { get; set; }
    public byte[] Attachment { get; set; }
    public string AttachmentName { get; set; }

}

我的函数在我的本地机器上运行良好,但在我的天蓝色函数上却不行。

文件丢失...

【问题讨论】:

    标签: azure azure-functions sendgrid


    【解决方案1】:

    我对其进行了测试,它在本地和 azure 上都运行良好。您可以参考以下步骤:

    1.在portal上创建一个HttpTrigger并配置Outputs。

    2.在HttpTrigger中添加sendgrid函数。当您设置输出时,ToAddress、FromAddress、MessageText 和 MessageSubject 都包含。

    3.输出。

    【讨论】:

      猜你喜欢
      • 2018-09-30
      • 1970-01-01
      • 1970-01-01
      • 2021-01-01
      • 2021-09-15
      • 2012-08-08
      • 1970-01-01
      • 2012-07-29
      相关资源
      最近更新 更多