title: Outbound emails stopped going after changing the token settings in CRM
body: <p><strong>Initial scenario:</strong></p>
  
  <ul>
  <li><p>In <code>Microsoft Dynamics CRM</code>, which we use with our .NET application, following were the <code>system settings</code> for 'Email':
  'Use Smart Matching' was selected and 'Use tracking token' was not selected.</p></li>
  <li><p>Both outbound and inbound emails were working fine without any issue. </p></li>
  </ul>
  
  <p>Everything was working properly, until last week when inbound emails stopped coming in. So, as per the advice by Microsoft, we changed our settings to get the inbound emails working.</p>
  
  <p><strong>Scenario after changing settings:</strong></p>
  
  <ul>
  <li><p>As per Microsoft, we changed CRM System settings to have 'Use tracking token' option selected and deselected 'Use Smart Matching' option.</p></li>
  <li><p>Inbound emails started coming properly. </p></li>
  </ul>
  
  <p><strong>Issue:</strong></p>
  
  <p>This time some outgoing emails which were generated as part of CRM Data Service code are not going out. The emails which are generated and sent by workflows are going out properly.</p>
  
  <p>There is no issue whatsoever with creation of emails. But, when I try to send it, it fails with following stack trace:</p>
  
  <blockquote>
    <p>Server stack trace:     at
    System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime
    operation, ProxyRpc&amp; rpc)    at
    System.ServiceModel.Channels.ServiceChannel.Call(String action,
    Boolean oneway, ProxyOperationRuntime operation, Object[] ins,
    Object[] outs, TimeSpan timeout)    at
    System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage
    methodCall, ProxyOperationRuntime operation)    at
    System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage
    message)</p>
    
    <p>Exception rethrown at [0]:     at
    System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage
    reqMsg, IMessage retMsg)    at
    System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&amp;
    msgData, Int32 type)    at
    Microsoft.Xrm.Sdk.IOrganizationService.Execute(OrganizationRequest
    request)    at
    Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.ExecuteCore(OrganizationRequest
    request)    at
    Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.Execute(OrganizationRequest
    request)    at
    CRMDataService.Classes.EmailFunctions.CreateAndSendEmail(CrmErrorHandler
    logger, String emailBody, String emailSubject, KeyValuePair<code>2
    fromParty, Dictionary</code>2 toParties, 
                                              Dictionary<code>2 ccParties, Dictionary</code>2 bccParties, Nullable<code>1 regardingObject, Nullable</code>1 owner, 
                                              Boolean bSendEmail, String trackingToken, Email emailFromTemplate, Nullable`1&amp; emailGuid)       in
    c:\Users\vnarkar\Workspace\CRMDataService\Classes\EmailFunctions.cs:line
    115</p>
  </blockquote>
  
  <p>Following is my code which is throwing out exception:</p>
  
  <pre><code>if (bSendEmail) {
                          SendEmailRequest sendEmailReq = new SendEmailRequest();
                          sendEmailReq.EmailId = emailID;
                          sendEmailReq.IssueSend = true;
                          sendEmailReq.TrackingToken = !String.IsNullOrWhiteSpace(trackingToken) ? trackingToken : String.Empty;
                         // sendEmailReq.TrackingToken = String.Empty;
  
  
                          // Add Tracking Token to SendEmail Request
                          /*
                          GetTrackingTokenEmailRequest jj_GetTrackingTokenEmailRequest = new GetTrackingTokenEmailRequest();
                          GetTrackingTokenEmailResponse jj_GetTrackingTokenEmailResponse = null;
                          jj_GetTrackingTokenEmailResponse = (GetTrackingTokenEmailResponse) proxy.Execute(jj_GetTrackingTokenEmailRequest);
                          sendEmailReq.TrackingToken = jj_GetTrackingTokenEmailResponse.TrackingToken;
                          */
  
  
                          proxy.Execute(sendEmailReq); // Fails here
                      }
  </code></pre>
  
  <p>There is now an issue with proxy.Execute(sendEmailReq);</p>
  
  <p>Note: <code>proxy</code> is instance of <code>OrganizationServiceProxy</code>. 
  proxy is generated in the following manner:</p>
  
  <pre><code>var proxy = CRMHelper.GetCrmServiceProxy();
  </code></pre>
  
  <p>GetServiceProxy() method from CRMHelper class is:</p>
  
  <pre><code>public static OrganizationServiceProxy GetCrmServiceProxy()
          {
              OrganizationServiceProxy proxy = null;
  
              try
              {
                  ServerConnection serverConnect = new ServerConnection();
                  ServerConnection.Configuration serverConfig = CachedData.GetServerConfiguration();
  
                  proxy = new OrganizationServiceProxy(serverConfig.OrganizationUri, serverConfig.HomeRealmUri, serverConfig.Credentials, serverConfig.DeviceCredentials);
                  proxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
                  proxy.Timeout = new TimeSpan(0, 10, 0); // Set timeout so long calls won't timeout
              }
              catch (FaultException&lt;Microsoft.Xrm.Sdk.OrganizationServiceFault&gt;)
              {
                  // Generic SQL Error...
              }
  
              return proxy;
          }
  </code></pre>
  
  <p><strong>What I tried?</strong></p>
  
  <p>(1) As the token related settings were changed, my first thought was to assign value to the <code>sendEmailReq.TrackingToken</code>.</p>
  
  <p>Hence I put the following code:</p>
  
  <pre><code> // Add Tracking Token to SendEmail Request
  
  GetTrackingTokenEmailRequest jj_GetTrackingTokenEmailRequest = new GetTrackingTokenEmailRequest();
  
  GetTrackingTokenEmailResponse jj_GetTrackingTokenEmailResponse = null;
  
  jj_GetTrackingTokenEmailResponse = (GetTrackingTokenEmailResponse) proxy.Execute(jj_GetTrackingTokenEmailRequest);
  
  sendEmailReq.TrackingToken = jj_GetTrackingTokenEmailResponse.TrackingToken;
  </code></pre>
  
  <p>But, even this thing fails at:</p>
  
  <pre><code>jj_GetTrackingTokenEmailResponse = (GetTrackingTokenEmailResponse) proxy.Execute(jj_GetTrackingTokenEmailRequest);
  </code></pre>
  
  <p>I suspect there is some problem with <code>proxy</code> or use of instance of <code>OrganizationProxy</code> in this scenario. I could be completely wrong.</p>
  
  <p>(2) Another piece of code written for workflows works with outgoing emails with same CRM settings. But, this code makes use of service (instance of <code>OrganizationService</code>) instead of proxy (instance of <code>OrganizationProxy</code>). Here is portion of the code:</p>
  
  <pre><code>newEmail.RegardingObjectId = new EntityReference(SalesOrder.EntityLogicalName, so.Id);
     //newEmail.OwnerId = null;
     Guid newEmailId = service.Create(newEmail);
     try {
          SendEmailRequest sendEmailReq = new SendEmailRequest();
          sendEmailReq.EmailId = newEmailId;
          sendEmailReq.IssueSend = true;
          sendEmailReq.TrackingToken = String.Empty;
          service.Execute(sendEmailReq);
          }
  </code></pre>
  
  <p>Please let me know your views.</p>