0

嗨,我基本上是在执行一个查询,该查询返回特定日期存在的 x 数量的电子邮件,我想使用 sendgrid 的 api 向所有这些电子邮件发送一封电子邮件,这是我的代码 - 我遇到了下面列出的很多错误有人阐明了吗?

[代码]

**#r "System.Data"
#r "SendGrid"

using System;
using System.Data;
using SendGrid.Helpers.Mail;

using System.Data.SqlClient;
using System.Text.RegularExpressions;
using Microsoft.SqlServer.Server;
using SendGrid;

        private SqlConnection conn = null;
        private SqlDataAdapter da = null;
        private SqlCommandBuilder cb = null;
        private DataSet ds = null;
        private String location = null;

public void Run(TimerInfo myTimer, TraceWriter log)
    {
        log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
        string connStr = "Data Source=sdc-hwsb.database.windows.net;Initial Catalog=SDC-HotelWSBooking;Integrated Security=False;User ID=sdchwsb;Password=Trivago17!;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
        SqlConnection conn = new SqlConnection(connStr);
        conn.Open();
        string query = "SELECT email FROM dbo.test_bookings2 WHERE startDate = @startDate";
        SqlCommand cmd = new SqlCommand(query, conn);
        cmd.Parameters.AddWithValue("@startDate", DateTime.Today.ToShortDateString());
        int k = 0;
        int f = Convert.ToInt32(cmd.ExecuteNonQuery());
        while (f > 0 & k < f)
        {
            conn = new SqlConnection(connStr);
            da = new SqlDataAdapter(query, conn);
            cb = new SqlCommandBuilder(da);
            ds = new DataSet();
            da.Fill(ds);
            String Email = Convert.ToString(ds.Tables[0].Rows[k]);
            Run1(Email,message);
            k++;

        }

    }


    public static void Run1(string email, out Mail message)
{
      message = new Mail
        {        
        Subject = "Azure news"          
    };
var personalization = new Personalization();
// change to email of recipient
personalization.AddTo(new Email(email));   

Content content = new Content
{
    Type = "text/plain",
    Value = "DD"
};
message.AddContent(content);
message.AddPersonalization(personalization);

}

**

我收到与消息对象 sendgrid 正在使用的错误有关的错误,例如:

    2017-09-25T18:50:37.754 Function started (Id=067b32b9-7bc3-47ca-9f32-b8b92c3b57e9)
2017-09-25T18:50:37.770 Function compilation error
2017-09-25T18:50:37.770 run.csx(38,28): error CS0103: The name 'message' does not exist in the current context
2017-09-25T18:50:37.807 Exception while executing function: Functions.TimerTriggerCSharp1. Microsoft.Azure.WebJobs.Script: Script compilation failed.
2017-09-25T18:50:37.948 Function completed (Failure, Id=067b32b9-7bc3-47ca-9f32-b8b92c3b57e9, Duration=196ms)
4

2 回答 2

2

正如 Mike S 提到通过 use an 发送多封电子邮件,我查看了关于SendGrid 输出绑定ICollector的官方文档并没有找到任何示例,然后我按照C# 中的 Queue output sample的代码示例来测试此功能,如下所示:

运行.csx

#r "SendGrid"

using System;
using SendGrid.Helpers.Mail;

public static void Run(TimerInfo myTimer, TraceWriter log, ICollector<Mail> mails)
{
    log.Info($"C# Timer trigger function executed at: {DateTime.Now}");

    for(int i=0;i<3;i++)
    {
        Mail message = new Mail()
        {
            Subject = $"Hello world from the SendGrid C# TimerTrigger!"
        };

        var personalization = new Personalization();
        personalization.AddTo(new Email("the-email-address-of-recipient"));  

        Content content = new Content
        {
            Type = "text/plain",
            Value = $"Hello world!{i}"
        };

        message.AddContent(content);  
        message.AddPersonalization(personalization); 
        mails.Add(message);
    }
}

函数.json

{
  "bindings": [
    {
      "name": "myTimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 */5 * * * *"
    },
    {
      "type": "sendGrid",
      "name": "mails",
      "apiKey": "sendgrid-apikey",
      "direction": "out",
      "from":"<the-email-address-of-sender>"
    }
  ],
  "disabled": false
}

结果:

在此处输入图像描述

另外,通过 VS2017 创建 Functions 类库项目,你可以参考这篇关于 SendGrid 输出的教程。

于 2017-09-26T06:06:23.070 回答
0

其中一些错误是编译错误 - 首先修复这些错误。例如,您在第 28 行缺少一个 ')'。

您还可以在 Visual Studio 中编写函数 - 这将为您提供具有 C# 智能感知和错误检查的真实 IDE 的强大功能。这将捕获上面的错误。只要您的功能不平凡,这就会很有用。在此处查看详细信息: https ://blogs.msdn.microsoft.com/appserviceteam/2017/08/14/azure-functions-tools-released-for-visual-studio-2017-update-3/

SendGrid 绑定应该在您的 Run() 函数上。

public void Run(TimerInfo myTimer, TraceWriter log, out Mail message)

然后 Run1 只是生成消息的内部助手。

如果您需要发送多条消息,请使用 ICollector / IAsyncCollector。那有一个“添加”方法。

于 2017-09-25T18:51:36.617 回答