提问者:小点点

Azure Function SendGrid


嗨,我基本上正在执行一个查询,该查询返回特定日期存在的 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);

}

**

我收到引用发送网格正在使用的消息对象的错误,例如:

    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)

共2个答案

匿名用户

正如 Mike S 提到的通过使用 IColletor 发送多封电子邮件,我检查了有关 SendGrid 输出绑定的官方文档,没有找到任何示例,然后我按照 C# 中的队列输出示例中的代码示例来测试此功能,如下所示:

运行.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创建函数类库项目,可以参考有关SendGrid输出的本教程。

匿名用户

其中一些错误是编译错误 - 请先修复这些错误。例如,您在第 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。它有一个“添加”方法。

相关问题