Запланированная почта с SendGrid - PullRequest
0 голосов
/ 06 июля 2018

Я сделал это на портале Azure. Как я могу преобразовать эту почту в C #?

run.csx выглядит так:

#r "SendGrid"

using System;
using SendGrid.Helpers.Mail;
using Microsoft.Azure.WebJobs.Host;

public static Mail Run(TimerInfo myTimer, TraceWriter log)
{
    var today = DateTime.Today.ToShortDateString();
    log.Info($"Generating daily report for {today} at {DateTime.Now}");

    Mail message = new Mail()
    {
        Subject = "15 DK'LIK TEST MAILI"
    };

    Content content = new Content
    {
        Type = "text/plain",
        Value = "Bu mail 15 dk da bir yinelenecektir."
    };

    message.AddContent(content);
    return message;
}

function.json выглядит;

{
  "bindings": [
    {
      "type": "timerTrigger",
      "name": "myTimer",
      "schedule": "0 */15 * * * *",
      "direction": "in"
    },
    {
      "type": "sendGrid",
      "name": "$return",
      "direction": "out",
      "apiKey": "CustomSendGridKeyAppSettingName",
      "from": "blabla@hotmail.com",
      "to": "blabla@hotmail.com"
    }
  ],
  "disabled": true
}

На C # выдает 2 ошибки. Я добавил sendgrid nuget. Как я могу передать эти ошибки? Если я просто добавлю почтовую функцию Sengrid в Visual Studio, это даст ошибку «run» в пространстве имен. Когда я копирую код своего портала, он начинает выдавать ошибку «run».

https://i.imgur.com/omJHQxp.png

Ответы [ 2 ]

0 голосов
/ 11 июля 2018

Это мое решение, ребята; Приложение; Visual Studio 2017, Установленный пакет Azure из установщика Visual Studio. Затем создайте «Azure Function V1» CS файл выглядит так:

using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using SendGrid.Helpers.Mail;

namespace ScheduledMail
{   
    public static class FifteenMinMail
    {
        [FunctionName("FifteenMinMail")]
        public static void Run([TimerTrigger("0 */15 * * * *")]TimerInfo myTimer, [SendGrid(ApiKey = "AzureWebJobsSendGridApiKey")] out SendGridMessage message, TraceWriter log)
        {
            log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
            message = new SendGridMessage();
            message.AddTo("BLABLABLA @gmail or @outlook etc here.");
            message.AddContent("text/html", "This mail will repeat every 15 minutes.");
            message.SetFrom(new EmailAddress("BLABLABLA @gmail or @outlook etc here."));
            message.SetSubject("TEST Mail");
        }
    }
}

Тогда не забудьте добавить свой ключ API Sengrid в local.settings.json. Моя выглядит как;

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "AzureWebJobsDashboard": "UseDevelopmentStorage=true",
    "AzureWebJobsSendGridApiKey": "SG.BLABLABLA........"
  }
}
0 голосов
/ 06 июля 2018

Вы должны изменить свой код на

Потому что методы C # должны быть внутри класса, а класс должен быть внутри Пространство имен

using System;
using SendGrid.Helpers.Mail;
using Microsoft.Azure.WebJobs.Host;

namespace YourProject
{
    public class TempClass
    {
        public static Mail Run(TimerInfo myTimer, TraceWriter log)
        {
            var today = DateTime.Today.ToShortDateString();
            log.Info($"Generating daily report for {today} at {DateTime.Now}");

            Mail message = new Mail()
            {
                Subject = "15 DK'LIK TEST MAILI"
            };

            Content content = new Content
            {
                Type = "text/plain",
                Value = "Bu mail 15 dk da bir yinelenecektir."
            };

            message.AddContent(content);
            return message;
        }
    }
}
...