This sample code shows how to create an e-mail activity instance from the specified e-mail message.
using System;
using CrmSdk;
using Microsoft.Crm.Sdk.Utility;
namespace Microsoft.Crm.Sdk.HowTo.Email
{
public class DeliverPromoteEmail
{
public DeliverPromoteEmail()
{
}
public static bool Run(string crmServerUrl, string orgName)
{
bool success = false;
try
{
// Set up the CRM Service.
CrmService service = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.GetCrmService(crmServerUrl, orgName);
service.PreAuthenticate = true;
#region Setup Data Required for this Sample
// Create a user to send an e-mail message to (To: field).
contact emailContact = new contact();
emailContact.firstname = "Adam";
emailContact.lastname = "Carter";
emailContact.emailaddress1 = "adam.carter@example.com";
// Create the contact.
Guid emailContactId = service.Create(emailContact);
// Get a system user to send the e-mail (From: field)
WhoAmIRequest systemUserRequest = new WhoAmIRequest();
WhoAmIResponse systemUserResponse = service.Execute(systemUserRequest) as WhoAmIResponse;
ColumnSet selectClause = new ColumnSet();
selectClause.Attributes = new string[] { "internalemailaddress" };
// Execute the request.
systemuser emailSender = service.Retrieve(EntityName.systemuser.ToString(), systemUserResponse.UserId, selectClause) as systemuser;
#endregion
// Create the request.
DeliverPromoteEmailRequest deliverEmailRequest = new DeliverPromoteEmailRequest();
// Set all required fields
deliverEmailRequest.Subject = "SDK Sample Email";
deliverEmailRequest.To = emailContact.emailaddress1;
deliverEmailRequest.From = emailSender.internalemailaddress;
deliverEmailRequest.Bcc = String.Empty;
deliverEmailRequest.Cc = String.Empty;
deliverEmailRequest.Importance = "high";
deliverEmailRequest.Body = "This message will create an email activity.";
deliverEmailRequest.MessageId = Guid.NewGuid().ToString();
deliverEmailRequest.SubmittedBy = "";
deliverEmailRequest.ReceivedOn = new CrmDateTime();
deliverEmailRequest.ReceivedOn.Value = DateTime.Now.ToString();
// We will not attach a file to the e-mail, but the Attachments property is required.
deliverEmailRequest.Attachments = new BusinessEntityCollection();
deliverEmailRequest.Attachments.EntityName = EntityName.activitymimeattachment.ToString();
deliverEmailRequest.Attachments.BusinessEntities = new activitymimeattachment[0];
// Execute the request.
DeliverPromoteEmailResponse deliverEmailResponse = (DeliverPromoteEmailResponse)service.Execute(deliverEmailRequest);
#region check success
// Query for the delivered e-mail and verify the status code is "Sent".
ColumnSet deliveredMailColumns = new ColumnSet();
deliveredMailColumns.Attributes = new string[] { "statuscode" };
email deliveredEmail = service.Retrieve(EntityName.email.ToString(), deliverEmailResponse.EmailId, deliveredMailColumns) as email;
if (deliveredEmail.statuscode.Value == EmailStatus.Sent)
{
success = true;
}
#endregion
#region Remove Data Required for this Sample
// Delete the sent e-mail messages.
service.Delete(EntityName.email.ToString(), deliveredEmail.activityid.Value);
// Delete the contacts created for e-mail messages.
service.Delete(EntityName.contact.ToString(), emailContactId);
#endregion
}
catch (System.Web.Services.Protocols.SoapException)
{
// Perform error handling here.
throw;
}
catch (Exception)
{
throw;
}
return success;
}
}
}