Created
August 22, 2018 05:54
-
-
Save goldytech/e2671f54c9c55f1c694579ea126c0f0e to your computer and use it in GitHub Desktop.
Azure Functions SendGrid email from blob container.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace AzFuncAppBlobToSendGrid | |
{ | |
using System; | |
using System.IO; | |
using Microsoft.Azure.WebJobs; | |
using Microsoft.Azure.WebJobs.Host; | |
using Microsoft.Extensions.Logging; | |
using SendGrid.Helpers.Mail; | |
public static class GetDocumentFromBlob | |
{ | |
[FunctionName("GetDocumentFromBlob")] | |
public static void Run([BlobTrigger("email-attachments/{name}", Connection = "slsAppStorage")]Stream myBlob, | |
string name, | |
ILogger log, | |
[SendGrid(ApiKey = "SendGridApiKey")] out SendGridMessage message) | |
{ | |
message = new SendGridMessage(); | |
try | |
{ | |
log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes"); | |
var blobBytes = GetBytesFromStream(myBlob); | |
message.AddTo("PutToEmailAddressHere"); | |
message.AddContent("text/html", "This is the attachment <b>email</b>"); | |
message.AddAttachment(name, Convert.ToBase64String(blobBytes), "application/pdf", "attachment"); | |
message.SetFrom(new EmailAddress("PutFromEmailAddressHere")); | |
message.SetSubject("test msg"); | |
} | |
catch (Exception ex) | |
{ | |
log.LogError(ex, ex.Message); | |
} | |
} | |
private static byte[] GetBytesFromStream(Stream inputStream) | |
{ | |
byte[] result; | |
using (var ms = new MemoryStream()) | |
{ | |
inputStream.CopyTo(ms); | |
result = ms.ToArray(); | |
} | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment