使用SendGrid的计划邮件

时间:2018-07-06 07:21:20

标签: c# azure sendgrid

我在Azure门户上做到了如何转换为以C#发送此计划的邮件?

run.csx外观;

dependencies {    
implementation(project(':react-native-maps')){
    exclude group: 'com.google.android.gms', module: 'play-services-base'
    exclude group: 'com.google.android.gms', module: 'play-services-maps'
}
implementation 'com.google.android.gms:play-services-base:10.0.1'
implementation 'com.google.android.gms:play-services-maps:10.0.1'
compile project(':react-native-splash-screen')
compile project(':react-native-vector-icons')
compile fileTree(dir: "libs", include: ["*.jar"])
compile "com.android.support:appcompat-v7:23.0.1"
compile "com.facebook.react:react-native:+"  // From node_modules
compile project(':react-native-fcm')
implementation 'com.google.firebase:firebase-core:16.0.1'    
}

function.json看起来像

#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;
}

在C#上给出2错误。我添加了sendgrid nuget。如何传递此错误?如果我只是在Visual Studio中添加sengrid邮件功能,则会出现“运行”命名空间错误。当我在此处复制门户网站代码时,它开始出现“运行”错误。

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

2 个答案:

答案 0 :(得分:2)

您应该将代码更改为

  

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

答案 1 :(得分:0)

这是我的解决方案专家; 应用程式; 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");
        }
    }
}

然后不要忘记将您的Sengrid api密钥添加到local.settings.json中。我的样子;

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