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

PkotianAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bmosoftwareCommented:
Hi Pkotian,

A couple of things, that code looks like it comes straight out of the Sdk, unfortunately there have been some problems with some of the sample code in the Sdk for CRM 4.0, especially the earlier versions. Please make sure you have downloaded the latest version http://www.microsoft.com/downloads/details.aspx?FamilyID=82e632a7-faf9-41e0-8ec1-a2662aae9dfb&DisplayLang=en

Additionally the user that you are sending from, make sure that they have an email address specified on their user record, otherwise sending emails won't work and will generate the error you are getting.

Also the user you are sending to... someone@example.com needs to exist as an email address against a record in the system otherwise you will also not be able to send. I have put some sample code below using the acitivityparty method rather than the torecipients, the code send an email to the user who is creating the email (as a test), once you are sure that this works you can change the activitypart accordingly.


          Guid emailID = new Guid();
                try
                {
                    // Create an activity party for the email
                    CrmSdk.activityparty party = new MyCrmSdk.activityparty();
                    party.partyid = new MyCrmSdk.Lookup();
                    party.partyid.type = MyCrmSdk.EntityName.systemuser.ToString();
                    party.partyid.Value = systemUser.UserId;
 
                    // Create an email message.
                    CrmSdk.email email = new MyCrmSdk.email();
 
                    // Set email properties
                    email.to = new MyCrmSdk.activityparty[] { party };
                    email.from = new MyCrmSdk.activityparty[] { party };
                    email.subject = Subject;
                    email.description = Body;
 
                    CrmSdk.CrmBoolean direction = new MyCrmSdk.CrmBoolean();
                    direction.Value = true;
                    email.directioncode = direction;
 
                    CrmSdk.TargetCreateEmail targetCreate = new MyCrmSdk.TargetCreateEmail();
                    targetCreate.Email = email;
 
                    CrmSdk.CreateRequest request = new MyCrmSdk.CreateRequest();
                    request.Target = targetCreate;
 
                    CrmSdk.CreateResponse response = (MyCrmSdk.CreateResponse)service.Execute(request);
                    emailID = response.id;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PkotianAuthor Commented:
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
0
bmosoftwareCommented:
Can you confirm that your application login user's GUID has a valid email address?
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

PkotianAuthor Commented:
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
0
PkotianAuthor Commented:
hey thanks,
Its working fine. I have one more doubt. can we attach the documents to the mails?
If yes then how can we?
0
bmosoftwareCommented:
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/
0
PkotianAuthor Commented:
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
0
bmosoftwareCommented:
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 ?
0
PkotianAuthor Commented:
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

0
bmosoftwareCommented:
Is your webserver configured to use and IFD Deployment ?
0
PkotianAuthor Commented:
yes
In the web server everything else works other than sending email and downloading attachments.
0
bmosoftwareCommented:
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 ?
0
PkotianAuthor Commented:
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.
0
PkotianAuthor Commented:
Hi,
I wanted one help.
Do u have any example for sending a mail in CRM using Email template in C#.
0
wasekh_OWCommented:
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 ?
 
0
wasekh_OWCommented:
ok, I am using CRM 4.0 and i am not getting service.CallerIdValue = new CallerId(); service. and above code is not working.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Email Servers

From novice to tech pro — start learning today.