This sample code shows how to create an e-mail activity instance from the specified e-mail message.
1: //# Promote an E-mail Message to Microsoft Dynamics CRM
2: using System;
3: using CrmSdk;
4: using Microsoft.Crm.Sdk.Utility;
5: 6: namespace Microsoft.Crm.Sdk.HowTo.Email
7: {8: public class DeliverPromoteEmail
9: {10: public DeliverPromoteEmail()
11: { 12: 13: } 14: 15: public static bool Run(string crmServerUrl, string orgName)
16: {17: bool success = false;
18: 19: try
20: {21: // Set up the CRM Service.
22: CrmService service = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.GetCrmService(crmServerUrl, orgName);23: service.PreAuthenticate = true;
24: 25: #region Setup Data Required for this Sample
26: 27: // Create a user to send an e-mail message to (To: field).
28: contact emailContact = new contact();
29: emailContact.firstname = "Adam";
30: emailContact.lastname = "Carter";
31: emailContact.emailaddress1 = "adam.carter@example.com";
32: 33: // Create the contact.
34: Guid emailContactId = service.Create(emailContact); 35: 36: // Get a system user to send the e-mail (From: field)
37: WhoAmIRequest systemUserRequest = new WhoAmIRequest();
38: WhoAmIResponse systemUserResponse = service.Execute(systemUserRequest) as WhoAmIResponse;
39: 40: ColumnSet selectClause = new ColumnSet();
41: selectClause.Attributes = new string[] { "internalemailaddress" };
42: 43: // Execute the request.
44: systemuser emailSender = service.Retrieve(EntityName.systemuser.ToString(), systemUserResponse.UserId, selectClause) as systemuser;
45: 46: #endregion
47: 48: // Create the request.
49: DeliverPromoteEmailRequest deliverEmailRequest = new DeliverPromoteEmailRequest();
50: 51: // Set all required fields
52: deliverEmailRequest.Subject = "SDK Sample Email";
53: deliverEmailRequest.To = emailContact.emailaddress1; 54: deliverEmailRequest.From = emailSender.internalemailaddress; 55: deliverEmailRequest.Bcc = String.Empty; 56: deliverEmailRequest.Cc = String.Empty;57: deliverEmailRequest.Importance = "high";
58: deliverEmailRequest.Body = "This message will create an email activity.";
59: deliverEmailRequest.MessageId = Guid.NewGuid().ToString();60: deliverEmailRequest.SubmittedBy = "";
61: deliverEmailRequest.ReceivedOn = new CrmDateTime();
62: deliverEmailRequest.ReceivedOn.Value = DateTime.Now.ToString(); 63: 64: // We will not attach a file to the e-mail, but the Attachments property is required.
65: deliverEmailRequest.Attachments = new BusinessEntityCollection();
66: deliverEmailRequest.Attachments.EntityName = EntityName.activitymimeattachment.ToString();67: deliverEmailRequest.Attachments.BusinessEntities = new activitymimeattachment[0];
68: 69: // Execute the request.
70: DeliverPromoteEmailResponse deliverEmailResponse = (DeliverPromoteEmailResponse)service.Execute(deliverEmailRequest); 71: 72: #region check success
73: 74: // Query for the delivered e-mail and verify the status code is "Sent".
75: ColumnSet deliveredMailColumns = new ColumnSet();
76: deliveredMailColumns.Attributes = new string[] { "statuscode" };
77: 78: email deliveredEmail = service.Retrieve(EntityName.email.ToString(), deliverEmailResponse.EmailId, deliveredMailColumns) as email;
79: 80: if (deliveredEmail.statuscode.Value == EmailStatus.Sent)
81: {82: success = true;
83: } 84: 85: #endregion
86: 87: #region Remove Data Required for this Sample
88: 89: // Delete the sent e-mail messages.
90: service.Delete(EntityName.email.ToString(), deliveredEmail.activityid.Value); 91: 92: // Delete the contacts created for e-mail messages.
93: service.Delete(EntityName.contact.ToString(), emailContactId); 94: 95: #endregion
96: }97: catch (System.Web.Services.Protocols.SoapException)
98: {99: // Perform error handling here.
100: throw;
101: }102: catch (Exception)
103: {104: throw;
105: } 106: 107: return success;
108: } 109: } 110: }
No comments:
Post a Comment