在Azure函数中找不到类型或命名空间名称“SendGridClient”

时间:2017-07-13 12:50:52

标签: c# azure sendgrid azure-functions

我想使用Azure功能使用SendGrid发送电子邮件。我正在使用c#代码来完成它。以下是我的示例代码。当我编译代码时,我遇到了错误,

#r "System.Configuration"
#r "System.Data"
#r "SendGrid"

using System;
using System.Net;
using System.Configuration;
using SendGrid;
using SendGrid.Helpers.Mail;

public static async Task < HttpResponseMessage > Run(HttpRequestMessage req, TraceWriter log) {

         var client = new SendGridClient(sendgridApiKey);
         var msg = new SendGridMessage()
         {
             From = new EmailAddress("sample@gmail.com", "DX Team"),
             Subject = "Hello World from the SendGrid CSharp SDK!",
             PlainTextContent = "Hello, Email!",
             HtmlContent = "<strong>Hello, Email using HTML!</strong>"
         };
         var recipients = new List<EmailAddress>
         {
             new EmailAddress("test@gmail.com", "John"),
             new EmailAddress("sa@gmail.com", "Sam")
         };
        msg.AddTo(recipients);
         msg.SetFooterSetting(
                      true,
                      "Some Footer HTML",
                      "<strong>Some Footer Text</strong>");
        var response = await client.SendEmailAsync(msg);
}

错误: -

The type or namespace name 'SendGridClient' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'SendGridMessage' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'EmailAddress' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'EmailAddress' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'EmailAddress' could not be found (are you missing a using directive or an assembly reference?)
The type or namespace name 'EmailAddress' could not be found (are you missing a using directive or an assembly reference?)
 Compilation failed.

我的推荐是here

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

您缺少NuGet包参考。要添加它,请创建以下内容的project.json文件:

{
  "frameworks": {
    "net46":{
      "dependencies": {
        "Sendgrid": "9.5.0"
      }
    }
   }
}

作为旁注,我建议您尝试使用SendGrid output binding而不是使用自定义代码发送邮件。您基本上拥有并输出Mail类型的参数,其余的将通过绑定完成。

答案 1 :(得分:1)

根据您的描述,我检查了这个问题,您可以参考以下步骤通过azure portal安装SendGrid包,如下所示:

enter image description here

enter image description here

注意:由于您有多个收件人,因此您需要使用msg.AddTos(recipients)

此外,正如J. Steen所述,您可以参考How can I use NuGet packages in my Azure Functions? 了解更多详情。此外,您可以参考here以获取有关如何将预编译的程序集部署到azure函数的更多详细信息。

答案 2 :(得分:0)

对于运行时版本3,我创建了一个function.proj文件,如下所示:

  <Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
    </PropertyGroup>

    <ItemGroup>
       <PackageReference Include="SendGrid" Version="9.12.6" />
   </ItemGroup>

 </Project>

您可以使用编辑器创建此文件,然后在门户中单击查看文件o(在右侧)->上载以将其上载到您的函数中。

并且只是在函数顶部:

using SendGrid;
using SendGrid.Helpers.Mail;

解决了我的类似问题。 我已删除 #r“ System.Configuration” #r“ System.Data”

在顶部。

相关问题