在 Azure 函数中找不到类型或命名空间名称 'SendGridClient'
The type or namespace name 'SendGridClient' could not be found in Azure function
我想使用 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
我该如何解决这个问题?
您缺少 NuGet 包参考。要添加它,请创建一个包含以下内容的 project.json
文件:
{
"frameworks": {
"net46":{
"dependencies": {
"Sendgrid": "9.5.0"
}
}
}
}
作为旁注,我建议您尝试使用 SendGrid output binding 而不是使用自定义代码发送邮件。您基本上将拥有 Mail
类型的输出参数,其余的将由绑定完成。
根据你的描述,我检查了这个问题,你可以参考以下步骤通过azure portal安装SendGrid包如下:
注意:由于您有多个收件人,因此需要使用msg.AddTos(recipients)
。
此外,正如 J. Steen 所说,您可以参考 如何在我的 Azure Functions 中使用 NuGet 包?
了解更多详情。此外,您可以参考 here 以了解有关如何将预编译程序集部署到 azure 函数的更多详细信息。
对于运行时版本 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"
在顶部。
我想使用 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
我该如何解决这个问题?
您缺少 NuGet 包参考。要添加它,请创建一个包含以下内容的 project.json
文件:
{
"frameworks": {
"net46":{
"dependencies": {
"Sendgrid": "9.5.0"
}
}
}
}
作为旁注,我建议您尝试使用 SendGrid output binding 而不是使用自定义代码发送邮件。您基本上将拥有 Mail
类型的输出参数,其余的将由绑定完成。
根据你的描述,我检查了这个问题,你可以参考以下步骤通过azure portal安装SendGrid包如下:
注意:由于您有多个收件人,因此需要使用msg.AddTos(recipients)
。
此外,正如 J. Steen 所说,您可以参考 如何在我的 Azure Functions 中使用 NuGet 包? 了解更多详情。此外,您可以参考 here 以了解有关如何将预编译程序集部署到 azure 函数的更多详细信息。
对于运行时版本 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"
在顶部。