Link to home
Start Free TrialLog in
Avatar of Pkotian
Pkotian

asked on

Sending Email in C# using CRM

Hi,

Iam tryig to send email in my code behind using CRM. But whenever i try i am getting "erver could not process teh request" error. I have attached the code here. Iam very new to the CRM. iam getting teh error in the line
SendEmailResponse res = (SendEmailResponse)service.Execute(req);
i think that it might be the problem with callerGUId i provide. Is there any particlar GUID i needs to give like administartors GUID. Actually i tried with users GUID.

 Please can anyoe help me to overcome with this error ASAP

Thanks
Pkotian
public  bool Run()
		{
         bool success = false;
 
         try
         {
 
            CrmService service = new CrmService();
            service.Credentials = System.Net.CredentialCache.DefaultCredentials;
 
            // Get the ID of the system user.
            WhoAmIRequest userRequest = new WhoAmIRequest();
            WhoAmIResponse user = (WhoAmIResponse) service.Execute(userRequest);
 
            #region Setup Data Required for this Sample
 
            // Create an email message.
            email email = new email();
            email.torecipients = "someone@example.com";
            email.subject = "Please disregard now ";
            email.description = "This is a test message. Please disregard now ";
 
            CrmBoolean direction = new CrmBoolean();
            direction.Value = true;
            email.directioncode = direction;
 
            TargetCreateEmail targetCreate = new TargetCreateEmail();
            targetCreate.Email = email;
 
            CreateRequest request = new CreateRequest();
            request.Target = targetCreate;
 
            CreateResponse response = (CreateResponse)service.Execute(request);
            Guid emailID = response.id;
 
            #endregion
 
            // Specify the system user who is sending the message.
            service.CallerIdValue = new CallerId();
            service.CallerIdValue.CallerGuid = new Guid("08a3c4ce-98a7-dc11-be35-0018f80b1359"); //user.UserId;
 
            // Create a SendEmail request.
            SendEmailRequest req = new SendEmailRequest();
                        req.EmailId = emailID;
            req.TrackingToken = "";
            req.IssueSend = true;
 
            // Send the email message.
            SendEmailResponse res = (SendEmailResponse)service.Execute(req);
 
            #region check success
 
            if( !res.Subject.Equals(""))
               success = true;
 
            #endregion
 
            #region Remove Data Required for this Sample
 
            // Delete the email message.
            service.Delete(EntityName.email.ToString(), emailID);
         
            #endregion
         }
         catch(System.Web.Services.Protocols.SoapException)
         {
            // Perform error handling here.
         }
 
         return success;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of bmosoftware
bmosoftware
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Pkotian
Pkotian

ASKER

Hi ,
thanks for the reply.
I tried with acitivityparty method  as you specified. It sends the mail only when email.to and email.from id is mine that is owner GUID which is taken from windows credentials. If I change the email.to to my application login user's GUID it throws the same error as "Server could not process the request".
Please can you help me in this.
Thanks,
Pkotian
Can you confirm that your application login user's GUID has a valid email address?
Avatar of Pkotian

ASKER

Yes i confirmed. That is the valid email address which exist on CRM Database too. I can get the GUID from CRM database but when i assign same GUID to the Email.To iam getting the error on sending email
Avatar of Pkotian

ASKER

hey thanks,
Its working fine. I have one more doubt. can we attach the documents to the mails?
If yes then how can we?
Yes you can attach documents to the emails. Check out Mitch Milam's blog entry on creating emails, there is a section there on adding attachments. http://blogs.infinite-x.net/2007/04/15/working-with-crm-emails/
Avatar of Pkotian

ASKER

Hi
iam facing one problem. When i deployed the same application in the webserver, Iam getting the same error as "Server could not process the request". Web server login username exist in the CRM server. Still I am getting the same error.  When i tried to send a email in CRM  manually in the same Web server, It works fine.
Please can you help me in this ASAP
Do you want to give me the code you are using so I can see if I can find anything, as this should work. What has changed from your previous post were it did work ?
Avatar of Pkotian

ASKER

Actually previously i was working with my intranet. Now i deployed my application in Webserver.
I havent change any thing in the code.
I have attached the method which iam calling in my application for sending the email.
In my local intranet still it works.
Web Server user is also exist in the CRM database.
public bool SendemailCRM(string ToGuID,string CCGuID,string BCCGuID,string strSubject,string strBody)
        {
            Guid emailID = new Guid();
            bool success=false;
            try
            {
 
                CrmService service = new CrmService();
                service.Credentials = System.Net.CredentialCache.DefaultCredentials;
                ws_crm.email email = new email();//Creating the email request
 
                #region Get the ID of the system user.
                WhoAmIRequest userRequest = new WhoAmIRequest();
                WhoAmIResponse user = (WhoAmIResponse)service.Execute(userRequest);
                #endregion
 
                #region Activity for From Email id
                ws_crm.activityparty FromParty = new activityparty();
                FromParty.partyid = new Lookup();
                FromParty.partyid.type = EntityName.systemuser.ToString();
                FromParty.partyid.Value =  user.UserId;
                #endregion
 
                #region Activity for From Email id
                ws_crm.activityparty ToParty = new activityparty();
                ToParty.partyid = new Lookup();
                ToParty.partyid.type = EntityName.contact.ToString();
                ToParty.partyid.Value = new Guid(ToGuID);
                #endregion
 
               
 
 
                #region  Create an email message.
                email.to = new activityparty[] { ToParty };
                email.from = new activityparty[] { FromParty };
 
                if (CCGuID.Trim() != "")
                {
                    #region Activity for CC Email id
                    ws_crm.activityparty CCParty = new activityparty();
                    CCParty.partyid = new Lookup();
                    CCParty.partyid.type = EntityName.contact.ToString();
                    CCParty.partyid.Value = new Guid(CCGuID);
                    #endregion
                    email.cc = new activityparty[] { CCParty };
 
                }
 
                if (BCCGuID.Trim() != "")
                {
                    #region Activity for BCC Email id
                    ws_crm.activityparty BCCParty = new activityparty();
                    BCCParty.partyid = new Lookup();
                    BCCParty.partyid.type = EntityName.systemuser.ToString();
                    BCCParty.partyid.Value = user.UserId;
                    #endregion
                    email.cc = new activityparty[] { BCCParty };
                }
 
 
 
                // Set email properties
 
                email.subject = strSubject;
                email.description = strBody;
 
 
 
                CrmBoolean direction = new CrmBoolean();
                direction.Value = true;
                email.directioncode = direction;
                #endregion
 
                #region  Create an Response
 
                TargetCreateEmail targetCreate = new TargetCreateEmail();
                targetCreate.Email = email;
 
                CreateRequest request = new CreateRequest();
                request.Target = targetCreate;
 
                CreateResponse response = (CreateResponse)service.Execute(request);
                emailID = response.id;
 
                #endregion
 
 
                // Specify the system user who is sending the message.
                service.CallerIdValue = new CallerId();
                service.CallerIdValue.CallerGuid = user.UserId; //new Guid("08a3c4ce-98a7-dc11-be35-0018f80b1359"); //user.UserId;
 
                #region  Create and  SendEmail request.
                SendEmailRequest req = new SendEmailRequest();
                req.EmailId = emailID;
                req.TrackingToken = "";
                req.IssueSend = true;
 
                // Send the email message.
                SendEmailResponse res = (SendEmailResponse)service.Execute(req);
                #endregion
 
 
                #region check success
 
                if (!res.Subject.Equals(""))
                    success = true;
 
                #endregion
 
                #region Remove Data Required for this email
 
                //Delete the email message.
                service.Delete(EntityName.email.ToString(), emailID);
 
                #endregion
 
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return success;
           
        }

Open in new window

Is your webserver configured to use and IFD Deployment ?
Avatar of Pkotian

ASKER

yes
In the web server everything else works other than sending email and downloading attachments.
Hi Pkotian,

Please will you confirm a couple of things for me. Are you using CRM 3.0 or 4.0, where are you trying to create this functionality ? From a Custom ASPX page, from a workflow assembly in CRM 3.0 from a Callout in CRM 4.0 ?
Avatar of Pkotian

ASKER

Hi,
Iam Using CRM 3.0. Iam trying this functionality from a Custom ASPX page in my application. That is the login page which is sending mail on click of forgot password.

One more thing i wanted to ask is , i have set the default CRM Customer username, Password and Domain name in webconfig file so that my application uses that as a system user credentials. But iam getting error "Server could not process the request" in local machine as well.


i have set the system credential as below
 crm.Credentials = new System.Net.NetworkCredential(domainUsername, domainPassword, domainName);
where domainUsername, domainPassword, domainName are set in webconfig.
Avatar of Pkotian

ASKER

Hi,
I wanted one help.
Do u have any example for sending a mail in CRM using Email template in C#.
Hi,
I want to know that for above code where activity party is used,what kind of reference is used, is it sdk DLL's or web references ?
 
ok, I am using CRM 4.0 and i am not getting service.CallerIdValue = new CallerId(); service. and above code is not working.