function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MPetersenMPetersen 

Invalid Session ID - Illegal Session

Hello,

 

I am trying to call an Apex Class that will send an email from C#.  I don't know if I am missing something or trying to call the class incorrectly or what, but I receive an error saying I have "Invalid Session ID Illegal Session".  I have looked on the boards for this error and have found a number of responses for PHP, but I know nothing about that language so cannot figure out exactly what their solution is.

 

Here is my Apex Class:

 

 global class DomainNameRenewalEmails {
    
    webservice static void runFirstRenewalEmail(){
        
        for(Domains__c domains : [SELECT Name, Id, Contact__c FROM Domains__c WHERE Do_Not_Renew__c = False AND Domain_Expiration__c >= :StartDate AND Domain_Expiration__c < :EndDate]){
               
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            
            mail.setSenderDisplayName('Member Services');
               
               mail.setTargetObjectId(domains.Contact__c);
            mail.setWhatId(domains.Id);
            mail.setTemplateId('00X40000000y7KI');
            
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        }
    }

}

 

 

And here is my call to the class from c#:

 

 DomainRenewalEmailClasses.DomainNameRenewalEmailsService run = new DomainRenewalEmailClasses.DomainNameRenewalEmailsService();
                
                try
                {
                    run.runFirstRenewalEmail();
                }
                catch (SoapException e)
                {
                    Console.WriteLine("Error: " + e);
                }
               

 

As this is the first time I am attempting this it is entirely possible that I have missed something very obvious that I am not aware of.  Any help would be greatly appreciated.

 

Also,  I am using the login method that I copied from the startup guide:

 

 private bool login()
        {
            // Create a service object
            binding = new SforceService();

            // Timeout after a minute
            binding.Timeout = 60000;

            // Try logging in
            LoginResult lr;
            try
            {
                Console.WriteLine("LOGGING IN NOW...");
                //***********************************************
                //*******Add admin username and password*********
                //***********************************************
                lr = binding.login("marc.petersen@teamwheretolive.com.wtl", "Taco1999");
            }
            // ApiFault is a proxy stub generated from the WSDL contract when
            // the web service was imported
            catch (SoapException e)
            {
                // Write the fault code to the console
                Console.WriteLine(e.Code);

                // Write the fault message to the console
                Console.WriteLine("An unexpected error has occurred: " + e.Message);

                // Write the stack trace to the console
                Console.WriteLine(e.StackTrace);

                // Return False to indicate that the login was not successful
                Console.WriteLine("Press any key to continue...");
                Console.ReadLine();
                return false;
            }

            // Check if the password has expired
            if (lr.passwordExpired)
            {
                Console.WriteLine("An error has occurred. Your password has expired.");
                Console.WriteLine("Press any key to continue...");
                Console.ReadLine();
                return false;
            }

            /** Once the client application has logged in successfully, it will use
             * the results of the login call to reset the endpoint of the service
             * to the virtual server instance that is servicing your organization
             */
            binding.Url = lr.serverUrl;

            /** The sample client application now has an instance of the SforceService
             * that is pointing to the correct endpoint. Next, the sample client
             * application sets a persistent SOAP header (to be included on all
             * subsequent calls that are made with SforceService) that contains the
             * valid sessionId for our login credentials. To do this, the sample
             * client application creates a new SessionHeader object and persist it to
             * the SforceService. Add the session ID returned from the login to the
             * session header
             */
            binding.SessionHeaderValue = new sforce.SessionHeader();
            binding.SessionHeaderValue.sessionId = lr.sessionId;

            // Return true to indicate that we are logged in, pointed
            // at the right URL and have our security token in place.
            Console.WriteLine("SUCCESSFULLY LOGGED IN...");
            return true;
        }

 

Thanks in advance.

 

Marc

Best Answer chosen by Admin (Salesforce Developers) 
MPetersenMPetersen

WOW!!  OK, so I figured it out.

 

I left 

 

sendMail.SessionHeaderValue = new SendDomainNameRenewalEmails.DomainRenewalEmailClasses.SessionHeader();
            sendMail.SessionHeaderValue.sessionId = lr.sessionId;

 

out of Login().  After I added this it works like a charm.