none
EWS error Exception Message = "The type initializer for 'Microsoft.Exchange.WebServices.Strings' threw an exception." RRS feed

  • Question

  • Hi I am trying to create a console application to read mails from Exchange server. I am getting this error at autodiscover "The type initializer for 'Microsoft.Exchange.WebServices.Strings' threw an exception."

    I checked my Exchange server version - 14.2.xxxx
    Exchange Web Services Managed API Version 2.0(Version15.0.0.xxxx)
    I am a normal user having email account in my company
    I am developing this code in win server 2008 R2(admin access), VS2010, outlook configured.
    I have figured out EWS intranet url also and tried passing that as well - no luck
    I have tried below code-

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.Security;
    using System.Security.Cryptography.X509Certificates;
    using System.Net.Security;
    using System.Web;
    using Microsoft.Exchange.WebServices.Data;
    namespace FinalEWS
    {
        class Program
        {
            static void Main(string[] args)
            {
               
                Console.Write("Start");
                ExchangeService service = CreateConnection("https://abc.bcd.company.com/EWS/Exchange.asmx");
                //ExchangeService service = CreateConnection("https://abc.bcd.company.com/EWS/Exchange.asmx");
           
                EmailMessage msg = new EmailMessage(service);
                msg.ToRecipients.Add(new EmailAddress("email@company.com"));
                msg.Subject = "EWS Test email";
                msg.Body = new MessageBody(BodyType.HTML, "<p>Hello Email!</p>");
                msg.Send();
                Console.Write("Completed");
            }
           
     public static ExchangeService CreateConnection(String url)
            {
                // Hook up the cert callback to prevent error if Microsoft.NET doesn't trust the server    
                ServicePointManager.ServerCertificateValidationCallback =
                delegate(
                Object obj, X509Certificate certificate,
                X509Chain chain,
                SslPolicyErrors errors)
                {
                    return true;
                };
                ExchangeService service = new ExchangeService();

                //service.Url = new Uri(url);
                service.Credentials = new NetworkCredential("username", "password", "domain");
               
                //service.UseDefaultCredentials = true;
                service.AutodiscoverUrl("firstname.lastname@company.com");
               
                return service;
            }
          
        }
    }


    Gaurav

    Saturday, August 11, 2012 8:36 AM

Answers

  • Are you developing against Exchange 2012 Preview ? if not you would better to use the release version 1.2 of the managed api http://www.microsoft.com/en-au/download/details.aspx?id=28952.

    Version 2.0 is in Beta I've had that error your having and the fix was to install the .NET 4.5 RC (you don't have to compile against it but it seems to change the  'System.Runtime.CompilerServices.ExtensionAttribute' type in 4.0.)

    Cheers
    Glen

    • Marked as answer by GauravHP Wednesday, August 15, 2012 8:22 AM
    Monday, August 13, 2012 7:33 AM