【问题标题】:Scheduled Mail with SendGrid使用 SendGrid 的预定邮件
【发布时间】:2018-07-06 07:21:20
【问题描述】:

我是在 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。我怎样才能通过这个错误?如果我只是在 Visual Studio 中添加 sengrid 邮件功能,它会给出“运行”命名空间错误。当我在这里复制我的门户代码时,它开始出现“运行”错误。

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

【问题讨论】:

  • 请注意,Visual Studio 是代码编辑器的名称(就像记事本是文本编辑器,或者 Excel 是电子表格编辑器)。 visual-studio 标记只能用于与编辑器本身有关的问题,而不是仅使用 Visual Studio 编写的代码。
  • 您已将代码直接放入.cs 文件中,而不是放入命名空间和类中。
  • 哦。非常感谢。我是新手。我没有注意到这段代码中没有类:D
  • Ridanod 不客气 :) 祝你的项目好运。

标签: c# azure sendgrid


【解决方案1】:

你应该把你的代码改成

因为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;
        }
    }
}

【讨论】:

    【解决方案2】:

    这是我的解决方案,伙计们; 应用程序; Visual Studio 2017,从 Visual Studio 安装程序安装 Azure 包。 然后创建“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");
            }
        }
    }
    

    然后不要忘记在 local.settings.json 中添加您的 sengrid api 密钥。我的看起来像;

    {
        "IsEncrypted": false,
      "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "AzureWebJobsDashboard": "UseDevelopmentStorage=true",
        "AzureWebJobsSendGridApiKey": "SG.BLABLABLA........"
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-02-28
      • 1970-01-01
      • 2013-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多