0

I'm currently developing on a machine where it's not possible to debug.
At this point I'm trying to do some email related matters with my web application.

  1. Send an email
  2. Retrieve emails from functional mailbox

For some reason, both seem to be impossible to perform. I wrote these blocks of code to see if I can get it up and running

The SendEmails function crashes on the msg.Send() part. I did a log on the msg.ToRecipients.Address, msg.Subject and the msg.Body and they are all filled in with the data I need.

The GetEmailsFromFolder function breaks as soon as it hits FindItemsResults<Item> results = service.FindItems(inbox, fView);

public static ExchangeService CreateConnection()
{
    string url = WebConfigurationManager.AppSettings["EWSAsmxUrlNp"];

    //ServicePointManager.ServerCertificateValidationCallback = delegate(
    //    Object obj,
    //    X509Certificate certificate,
    //    X509Chain chain,
    //    SslPolicyErrors errors)
    //{
    //    return true;
    //};

    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
    service.Url = new Uri(url);

    //service.UseDefaultCredentials = true;
    service.Credentials = new WebCredentials(@"DOM\User1", "password");

    return service;
}

public static void SendEmails(string to, string subject, string body)
{
    ExchangeService service = CreateConnection();

    EmailMessage msg = new EmailMessage(service);
    msg.ToRecipients.Add(to);
    msg.Subject = subject;
    msg.Body = new MessageBody(BodyType.HTML, body);
    msg.Send();
}

public static FindItemsResults<Item> GetEmailsFromFolder(Mailbox mailbox)
{
    ExchangeService service = CreateConnection();

    FolderView fView = new FolderView(100);
    fView.PropertySet = new PropertySet(BasePropertySet.IdOnly, FolderSchema.DisplayName);
    FolderId inbox = new FolderId(WellKnownFolderName.Inbox, mailbox.ToString());
    FindItemsResults<Item> results = service.FindItems(inbox, fView);

    return results;
}

I guess that in the CreateConnection() function the url is correct and working, because when I run it in the browser and after I enter my credentials I get the following XML returned: (only a small copy paste)

<wsdl:definitions targetNamespace="http://schemas.microsoft.com/exchange/services/2006/messages">
   <wsdl:types>
      <xs:schema>
         <xs:import namespace="http://schemas.microsoft.com/exchange/services/2006/messages" schemaLocation="messages.xsd"/>
      </xs:schema>
   </wsdl:types>
   <wsdl:message name="UploadItemsSoapIn">
      <wsdl:part name="request" element="tns:UploadItems"/>
      <wsdl:part name="Impersonation" element="t:ExchangeImpersonation"/>
      <wsdl:part name="MailboxCulture" element="t:MailboxCulture"/>
      <wsdl:part name="RequestVersion" element="t:RequestServerVersion"/>
   </wsdl:message>
   <wsdl:message name="UploadItemsSoapOut">
      <wsdl:part name="UploadItemsResult" element="tns:UploadItemsResponse"/>
      <wsdl:part name="ServerVersion" element="t:ServerVersionInfo"/>
   </wsdl:message>
   ... more here ...
</wsdl:definitions>

Note 1: There is an Exchange server on a different location. Everybody logs in with their credentials and gets access to their account.

Note 2: With both crashes I get the following Exception (which I managed to log):

Microsoft.Exchange.WebServices.Data.ServiceRequestException: The response received from the service didn't contain valid XML. System.Xml.XmlException: Root element is missing. at System.Xml.XmlTextReaderImpl.Throw(Exception e) at System.Xml.XmlTextReaderImpl.ParseDocumentContent() at System.Xml.XmlTextReaderImpl.Read() at System.Xml.XmlTextReader.Read() at System.Xml.XmlCharCheckingReader.Read() at Microsoft.Exchange.WebServices.Data.EwsXmlReader.Read() at Microsoft.Exchange.WebServices.Data.EwsXmlReader.Read(XmlNodeType nodeType) at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ReadXmlDeclaration(EwsServiceXmlReader reader) --- End of inner exception stack trace --- at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ReadXmlDeclaration(EwsServiceXmlReader reader) at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ReadSoapFault(EwsServiceXmlReader reader) at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ProcessWebException(WebException webException) at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(IEwsHttpWebRequest request) at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ValidateAndEmitRequest(IEwsHttpWebRequest& request) at Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest1.Execute() at Microsoft.Exchange.WebServices.Data.ExchangeService.InternalCreateItems(IEnumerable1 items, FolderId parentFolderId, Nullable1 messageDisposition, Nullable1 sendInvitationsMode, ServiceErrorHandling errorHandling) at Microsoft.Exchange.WebServices.Data.Item.InternalCreate(FolderId parentFolderId, Nullable1 messageDisposition, Nullable1 sendInvitationsMode) at Microsoft.Exchange.WebServices.Data.EmailMessage.InternalSend(FolderId parentFolderId, MessageDisposition messageDisposition) at Microsoft.Exchange.WebServices.Data.EmailMessage.Send() at T.App.Factory.Helper.ExchangeWebServiceHelper.SendEmails(String to, String subject, String body) in D:\T.App\Factory\Helper\ExchangeWebServiceHelper.cs:line 77 at PADAccountSwitching.App.Factory.NewBankFactory.DownloadRequests() in D:\T.App\Factory\NewBankFactory.cs:line 313

Jordec
  • 1,516
  • 2
  • 22
  • 43
  • It sounds like you are not hitting the service. This usually happens when a regular response (like a 404 HTML page) is being returned. I would suggest you somehow log what is being returned. – JuanR Jan 09 '17 at 15:26
  • Check this out too: http://stackoverflow.com/questions/11344400/exchange-web-services-the-response-received-from-the-service-didnt-contain-va – JuanR Jan 09 '17 at 15:29
  • Possible duplicate of [Exchange Web Services - The response received from the service didn't contain valid XML](http://stackoverflow.com/questions/11344400/exchange-web-services-the-response-received-from-the-service-didnt-contain-va) – JuanR Jan 09 '17 at 15:30
  • @Juan, I've read your answer and it probably has something to do with the XML. Could you perhaps tell me what value I specifically need to log to see what I get returned? Else this will take me quite a while to find what my XML looks like. – Jordec Jan 09 '17 at 15:44
  • I usually use a sniffer like wireshark or fiddler to find root cause of issue. Comparing results from a working location and a non working location will help find issue. A good response should end with a status of 200 Done. I would check your IE browser, firewalls and virus checkers to see if port or server is being blocked. – jdweng Jan 09 '17 at 16:36
  • Check this out on how to log. Once we see the response from the server, it should be easier to figure out: https://msdn.microsoft.com/en-us/library/office/dn495632(v=exchg.150).aspx – JuanR Jan 09 '17 at 16:59
  • If you open a browser and go to https://mail.mydomain.com/ews/exchange.asmx what do you get? I'm referring to the 'service.Url' which you have set – MadDev Feb 17 '17 at 16:16

0 Answers0