2013-05-26 71 views
0

我正嘗試在crm2011中創建併發送電子郵件請求。我有電子郵件,但是當我嘗試發送電子郵件時,我收到一個例外情況:試圖發送電子郵件請求,但我得到異常

帶ID = 00000000-0000-0000-0000-000000000000的電子郵件不存在。

這裏是我的代碼:

OrganizationServiceProxy p = new OrganizationServiceProxy(
    new Uri(""), null, ccr, null); 

WhoAmIRequest systemUserRequest = new WhoAmIRequest(); 
WhoAmIResponse systemUserResponse = (WhoAmIResponse)p.Execute(systemUserRequest); 
Guid _userId = systemUserResponse.UserId; 

Entity email = new Entity("email"); 
email.Attributes.Add("subject", "test"); 

Entity[] To = new Entity[1]; 
To[0] = new Entity("activityparty"); 
To[0]["partyid"] = new EntityReference("contact", new Guid("some guidid")); 
email.Attributes.Add("to", To); 

Entity[] From = new Entity[1]; 
From[0] = new Entity("activityparty"); 
From[0]["partyid"] = new EntityReference("systemuser", _userId); 
email.Attributes.Add("from", From); 

try 
{ 
    Guid emailGuid = p.Create(email);   
} 
catch (Exception e) 
{ 
    Console.WriteLine("error " + e.Message); 
    Console.ReadLine(); 
} 

OrganizationRequest request = new OrganizationRequest() { RequestName = "SendEmail" }; 

request["EmailId"] = email.Id; 
request["TrackingToken"] = ""; 
request["IssueSend"] = true; 

// THE CODE FAILS HERE: 
OrganizationResponse rsp = p.Execute(request); 
+0

你得到一個空的Guid'00000000-0000-0000-0000-000000000000',而不是一個有效的。你檢查了這個原因嗎? – Saravanan

回答

1

主要錯誤是在這一行:

request["EmailId"] = email.Id; 

當您創建的電子郵件中,Id屬性未填充的記錄內,但Guid位於變量內emailGuid

我建議改變這樣的代碼:

try 
{ 
    Guid emailGuid = p.Create(email);   
    OrganizationRequest request = new OrganizationRequest() { RequestName = "SendEmail" }; 
    request["EmailId"] = emailGuid; // now is the right variable 
    request["TrackingToken"] = ""; 
    request["IssueSend"] = true; 
    OrganizationResponse rsp = p.Execute(request); 
} 
catch (Exception e) 
{ 
    Console.WriteLine("error " + e.Message); 
    Console.ReadLine(); 
} 
相關問題