无法在C#中使用EWS发送或阅读电子邮件

时间:2017-01-09 15:20:46

标签: c# .net xml exchangewebservices

我目前正在开发一台无法调试的机器 此时,我正在尝试使用我的Web应用程序执行一些与电子邮件相关的事务。

  1. 发送电子邮件
  2. 从功能邮箱中检索电子邮件
  3. 出于某种原因,两者似乎都无法执行。 我编写了这些代码块,看看我是否可以启动并运行

    SendEmails部分msg.Send()功能崩溃了。我记录了msg.ToRecipients.Addressmsg.Subjectmsg.Body,并且我们都填写了我需要的数据。

    GetEmailsFromFolder功能在点击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;
    }
    

    我想在CreateConnection()函数中,url是正确且有效的,因为当我在浏览器中运行它并在输入我的凭据后,我得到以下XML返回:(只有一个小的复制粘贴)< / p>

    <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>
    

    注意1:其他位置有一台Exchange服务器。每个人都使用他们的凭据登录并访问他们的帐户。

    注意2:在两次崩溃的情况下,我都会遇到以下异常(我设法记录):

      

    Microsoft.Exchange.WebServices.Data.ServiceRequestException:The   从服务收到的响应不包含有效的XML。   System.Xml.XmlException:缺少根元素。在   System.Xml.XmlTextReaderImpl.Throw(Exception e)at   System.Xml.XmlTextReaderImpl.ParseDocumentContent()at   System.Xml.XmlTextReader.Read()中的System.Xml.XmlTextReaderImpl.Read()   在System.Xml.XmlCharCheckingReader.Read()处   Microsoft.Exchange.WebServices.Data.EwsXmlReader.Read()at   Microsoft.Exchange.WebServices.Data.EwsXmlReader.Read(XmlNodeType   nodeType)at   Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ReadXmlDeclaration(EwsServiceXmlReader   读者)---内部异常堆栈跟踪结束--- at   Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ReadXmlDeclaration(EwsServiceXmlReader   读者)   Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ReadSoapFault(EwsServiceXmlReader   读者)   Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ProcessWebException(引发WebException   webException)at   Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(IEwsHttpWebRequest   请求)   Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ValidateAndEmitRequest(IEwsHttpWebRequest&安培;   请求)   Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest1.Execute()   在   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,   字符串主题,字符串主体)   D:\ T.App \ Factory \ Helper \ ExchangeWebServiceHelper.cs:第77行   PADAccountSwitching.App.Factory.NewBankFactory.DownloadRequests()in   D:\ T.App \ Factory \ NewBankFactory.cs:第313行

0 个答案:

没有答案