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.
- Send an email
- 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