Andy
Andy

Reputation: 2603

Unable to use the SendGrid to trigger the email for changes in blob storage

I am new to MS functions and trying to create a C# function for triggering email when a new file is added in the Azure Blob storage.

Code sample below :

#r "SendGrid"
using Microsoft.Extensions.Logging;
using System;
using System.Text.RegularExpressions;
using SendGrid.Helpers.Mail;


public static void Run(string myBlob, string filename, ILogger log, out Mail message)
{
    var email = Regex.Match(myBlob, @"^email\:\ (.+)$", RegexOptions.Multiline).Groups[1].Value;
    log.LogInformation($"Got order from {email}\n License File Name: {filename} ");

    message = new Mail();
    var personalization = new Personalization();
    personalization.AddTo(new Email(email));
    message.AddPersonalization(personalization);

    Attachment attach = new Attachment();
    var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(myBlob);
    attach.Content = System.Convert.ToBase64String(plainTextBytes);
    attach.Type = "text/plain";
    attach.Filename = "license.lic";
    attach.Disposition = "attachment";
    attach.ContentId = "License File";
    message.AddAttachment(attach);

    var msgContent = new Content("text/html", "Your license file attached");
    message.AddContent(msgContent);
    message.Subject = "Thanks for your order";
    message.From = new Email("[email protected]");  

}

I am running function version ~2 (checked the app setting variables).

My question is why am I getting the error for Mail parameter (out Mail message )?

SendGrid is installed correctly the last time I checked.

Following is the log which show compilation error: ( I have no idea why it is unable to recognize the Mail param in the method )

2018-11-26T05:46:57.013 [Error] run.csx(8,73): error CS0246: The type or namespace name 'Mail' could not be found (are you missing a using directive or an assembly reference?)

Upvotes: 1

Views: 654

Answers (1)

Jerry Liu
Jerry Liu

Reputation: 17800

Mail is the class used in v1 functions, which works with v8 SendGrid SDK. While for v2 functions, SDK is v9 and we use SendGridMessage, Mail is not available anymore so the error occurs.

If you are certain that Storage and SendGrid extensions are installed, have a try at code below.

#r "SendGrid"

using System.Text.RegularExpressions;
using SendGrid.Helpers.Mail;

public static void Run(string myBlob, string filename, ILogger log, out SendGridMessage message)
{  
    var email = Regex.Match(myBlob, @"^email\:\ (.+)$", RegexOptions.Multiline).Groups[1].Value;
    log.LogInformation($"Got order from {email}\n License File Name: {filename} ");

    message = new SendGridMessage();
    message.AddTo(new EmailAddress(email));
    var base64Content = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(myBlob));

    message.AddAttachment(
        "license.lic",
        base64Content,
        "text/plain",
        "attachment",
        "License File"
    );

    message.AddContent("text/html", "Your license file attached");
    message.Subject = "Thanks for your order";
    message.From = new EmailAddress("[email protected]");
}

function.json

{
  "bindings": [
    {
      "name": "myBlob",
      "type": "blobTrigger",
      "direction": "in",
      "path": "yourContainerName/{filename}",
      "connection": "AzureWebJobsStorage"
    },
    {
      "type": "sendGrid",
      "name": "message",
      "apiKey": "YOURSENDAPIKEYAPPSETTING",
      "direction": "out"
    }
  ]
}

Upvotes: 3

Related Questions