简体   繁体   English

Email 使用 WebJobs 使用 SendGrid 发送

[英]Email sending with SendGrid using WebJobs

I'm trying to integrate SendGrid to a.Net 4.5 application using WebJobs.我正在尝试使用 WebJobs 将 SendGrid 集成到 .Net 4.5 应用程序。

I made the basic configurations required to send a basic email.我做了发送基本 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.到目前为止,我无法升级应用程序的 .Net 版本。 If it is possible to do this without using webjobs that is also fine.如果可以在不使用 webjobs 的情况下做到这一点,那也很好。

Program.cs程序.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函数.cs

public static void ProcessQueueMessage([QueueTrigger("queue")] string message, TextWriter log, [SendGrid(From = "no-reply@company.com", To = "employee@company.com")] out Mail mail)
{
    log.WriteLine(message);

    mail = new Mail();

    var personalization = new Personalization();
    personalization.AddBcc(new Email("employee@company.com"));

    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.发现以下函数: SendGrid_Test_002.Functions.ProcessQueueMessage ServicePointManager.DefaultConnectionLimit 设置为默认值 2。这可以限制对 Azure Storage 等服务的连接吞吐量。 For more information, see https://aka.ms/webjobs-connections .有关详细信息,请参阅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.我只需要使用以下代码将消息输入 webjob 队列。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM