2

I want to send email using SendGrid using Azure function. I am using c# code to do it. Below is my sample code. When I compile the code, I am getting below error,

#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);
}

Error:-

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.

My references is here

How do I solve this?

Galet
  • 5,853
  • 21
  • 82
  • 148
  • 1
    This might be of use: https://stackoverflow.com/questions/36411536/how-can-i-use-nuget-packages-in-my-azure-functions – J. Steen Jul 13 '17 at 12:54

3 Answers3

1

You are missing the NuGet package reference. To add it, create a project.json file of the following content:

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

As a side note, I recommend you trying to use SendGrid output binding instead of sending the mail with custom code. You'll basically have and output parameter of type Mail, and the rest will be done by the binding.

Mikhail Shilkov
  • 34,128
  • 3
  • 68
  • 107
  • After creating project.json with above content. how should I use it in my function? It will be useful If you provide me steps – Galet Jul 14 '17 at 04:25
1

According to your description, I checked this issue, you could refer to the following steps to install SendGrid package via azure portal as follows:

enter image description here

enter image description here

Note: Since you have multiple recipients, you need to use msg.AddTos(recipients).

Additionally, as J. Steen commented that you could refer to How can I use NuGet packages in my Azure Functions? for more details. Moreover, you could refer to here for more details about how to deploy pre-compiled assemblies to azure functions.

Bruce Chen
  • 18,207
  • 2
  • 21
  • 35
  • Which method is better between above method and this one - https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-sendgrid – Galet Jul 14 '17 at 07:12
  • Per my understanding, bindings is a simple way for us, e.g we do not need to init the `SendGridClient` instance or execute the sendEmail operation by yourself. I would prefer choosing init the SendGridClient by myself and send the email, since it would be more flexible. – Bruce Chen Jul 14 '17 at 07:25
  • Ok Thank you for your suggestion. – Galet Jul 14 '17 at 07:47
0

For runtime version 3, I have created a function.proj file like this:

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

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

 </Project>

You can create this file using an editor and click View Files o(on the right side) -> Upload in the portal to upload it to your function.

And simply at the top of the function:

using SendGrid;
using SendGrid.Helpers.Mail;

solved my similar problem. I have deleted #r "System.Configuration" #r "System.Data"

at the top.

ruzgarustu
  • 165
  • 1
  • 7