Azure Function Sengrid-附件仅在本地工作

时间:2018-08-15 11:20:01

标签: azure azure-functions sendgrid

我正在尝试将文件添加到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函数上却不能。

文件丢失...

1 个答案:

答案 0 :(得分:1)

我测试了它,它在本地和天蓝色都可以很好地工作。您可以参考以下步骤:

1。在门户网站上创建一个HttpTrigger并配置输出。

enter image description here 2.在HttpTrigger中添加sendgrid函数。设置输出后,ToAddress,FromAddress,MessageText和MessageSubject都包含。

enter image description here

3。输出。

enter image description here