2014-02-06 45 views
0

我需要發送帶有我的caml查詢挑出的行的列表項名稱的電子郵件。我有這樣的代碼:如何在timerJob發送的電子郵件中顯示SPList項目名稱?

   SPQuery filter = new SPQuery(); 
       filter.Query = string.Format("<Where><Leq><FieldRef Name=\"Revisionsdatum\" /><Value Type=\"DateTime\">{0}</Value></Leq></Where>", DateTime.Today.AddDays(14).ToString("yyyy-MM-ddThh:mm:ssZ")); 
       SPListItemCollection items = yourList.GetItems(filter); 
       foreach (var i in items) 
       { 
        string from = string.Empty; 
        string smtpAddress = string.Empty; 
        string to = "[email protected]";      
        string subject = "Dudate is coming"; 
        string body = "<h1>Hello!</h1><p>In to weeks an importent dudates comes({0}) with the name {2}.";//Here I would like to ad the dudate and the ListItems Name but how? 

        // get a reference to the current site collection's content database 
        SPWebApplication webApplication = this.Parent as SPWebApplication; 
        SPContentDatabase contentDb = webApplication.ContentDatabases[contentDbId]; 

        // get a reference to the "Tasks" list in the RootWeb of the first site collection in the content database 
        SPWeb rootWeb = contentDb.Sites[0].RootWeb; 

        SPList listjob = rootWeb.Lists.TryGetList("Tasks"); 

        // Get sender address from web application settings 
        from = rootWeb.Site.WebApplication.OutboundMailSenderAddress; 

        // Get SMTP address from web application settings 
        smtpAddress = rootWeb.Site.WebApplication.OutboundMailServiceInstance.Server.Address; 

        // Send an email if the news is approved 
        bool emailSent = SendMail(smtpAddress, subject, body, true, from, to, null, null); 


       }     

我會很好的回答你的問題!

回答

0

如下你可以得到它:

foreach (SPListItem item in items) \\ items is your SPListItemCollection 
{ 
    var fieldValue = item["Field Name"].ToString(); 
} 
0

要DateTime對象轉換爲CAML查詢使用SPUtility.CreateISO8601DateTimeFromSystemDateTime()方法。

您需要的字段由i [「Title」]和i [「DueDate」]引用。

在foreach循環中使用StringBuilder對象構造郵件正文並在循環後發送郵件。您的代碼將爲每項任務發送一封郵件。

相關問題