I'm trying to integrate SendGrid to a .Net 4.5 application using WebJobs.
I made the basic configurations required to send a basic email. I'm trying to run and test it in my local machine. I can't figure out how to push messages to the queue. I can't upgrade the .Net version of the application as of now. If it is possible to do this without using webjobs that is also fine.
Program.cs
static void Main()
{
var config = new JobHostConfiguration();
config.UseTimers();
config.Queues.MaxDequeueCount = 2;
config.Queues.MaxPollingInterval = TimeSpan.FromSeconds(4);
config.Queues.BatchSize = 2;
if (config.IsDevelopment)
{
config.UseDevelopmentSettings();
}
config.UseSendGrid();
var host = new JobHost(config);
host.RunAndBlock();
}
Functions.cs
public static void ProcessQueueMessage([QueueTrigger("queue")] string message, TextWriter log, [SendGrid(From = "[email protected]", To = "[email protected]")] out Mail mail)
{
log.WriteLine(message);
mail = new Mail();
var personalization = new Personalization();
personalization.AddBcc(new Email("[email protected]"));
mail.AddPersonalization(personalization);
mail.Subject = "Test Email Subject";
mail.AddContent(new Content("text/html", $"The message '{message}' was successfully processed."));
}
Found the following functions: SendGrid_Test_002.Functions.ProcessQueueMessage ServicePointManager.DefaultConnectionLimit is set to the default value of 2. This can limit the connection throughput to services like Azure Storage. For more information, see https://aka.ms/webjobs-connections. Job host started
I get this on the console.
Thanks in advance :)
I just had to feed the messages into the webjob queue using the following code.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
// Create the queue client.
CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
// Retrieve a reference to a container.
CloudQueue queue = queueClient.GetQueueReference("email-queue-name");
// Create the queue if it doesn't already exist
queue.CreateIfNotExists();
// Create message to be sent to the queue
CloudQueueMessage message = new CloudQueueMessage(JsonConvert.SerializeObject(new
{
From = emailContent.From,
To = emailContent.To,
Cc = emailContent.Cc,
Bcc = emailContent.Bcc,
Subject = emailContent.Subject,
Message = emailContent.Message
}).ToString());
queue.AddMessage(message);