问题描述
我基本上正在执行一个查询,该查询返回特定日期存在的x电子邮件数量,我想使用sendgrid的api将电子邮件发送到所有这些电子邮件中,这是我的代码-我遇到了很多错误下面列出的任何人都可以透露一些信息吗?
Hi I'm basically executing a query which returns x amount of emails that exist on a specific day and I want to send an email to all of these emails using sendgrid's api here is my code - I am running into alot of errors listed below could anyone shed some light?
[代码]
**#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所使用的错误有关的错误,例如:
I Am getting errors reffering to the Message object sendgrid is using such as:
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)
推荐答案
正如Mike S提到的通过ICollector
发送多封电子邮件一样,我检查了有关 SendGrid输出绑定,但未找到任何示例,因此我遵循了以测试此功能如下:
As Mike S mentioned about sending multiple emails via use an ICollector
, I checked the official document about SendGrid output binding and did not find any sample, then I followed the code sample from Queue output sample in C# to test this feature as follows:
run.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);
}
}
function.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输出的教程.
Additionally, for creating Functions class library project via VS2017, you could refer to this tutorial about SendGrid output.
这篇关于Azure功能SendGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!