2017-09-25 77 views
0

您好我基本上正在執行一個查詢,返回特定的一天存在的電子郵件的數量x,我想發送一封電子郵件到所有這些電子郵件使用sendgrid的api這裏是我的代碼 - 我遇到下面列出的許多錯誤可以讓人們瞭解一些情況嗎?Azure函數SendGrid

[代碼]

**#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); 

}

** 

我收到錯誤reffering到消息對象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) 

回答

0

一些這些錯誤是彙編錯誤 - 首先解決這些問題。例如,您在第28行缺少一個')'。

您也可以在Visual Studio中編寫函數 - 這將爲您提供使用C#intellisense和錯誤檢查的真實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。這有一個'添加'方法。

+0

我修正了他們../ @MikeS – rahulchawla

+0

不是全部:)它仍然告訴你關於C#編譯錯誤的代碼。第38行的「消息」的使用是非法的。 –

+0

這是sendgrid對象..這是我用來綁定它的文檔https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-sendgrid – rahulchawla

1

麥克小號提及通過使用ICollector發送多封電子郵件,我查了公文約SendGrid output binding,並沒有發現任何樣品,然後我跟着從Queue output sample in C#代碼示例測試此功能如下:

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 
} 

結果:

enter image description here

此外,對於通過VS2017創建函數類庫項目,你可以參考這個tutorial約SendGrid輸出。