private void WriteEmailsToDirectory(List <string> lstEmailsSid, string folderToSync, uint policyKey) { ASCommandResponse commandResponse = null; int fileNo = 1; foreach (string sid in lstEmailsSid) { string nazwaPlikuZKatalogiem = mailDir + "\\" + sid.Replace(":", ""); if (!File.Exists(nazwaPlikuZKatalogiem + ".eml")) { string fetchXml = SetFetchDataObjectAsXml(folderToSync, sid, true); StringBuilder xmlBuilder = new StringBuilder(); xmlBuilder.Append(fetchXml); ASCommandRequest commandRequest = CreateCommandRequest("ItemOperations", cred, devID, devType, protVer, server, username, xmlBuilder.ToString(), policyKey); // Send the request commandResponse = commandRequest.GetResponse(); EAS.generated.ItemOperationsResponseNamespace.ItemOperations emailFetched = GetFetchedMail(commandResponse.XmlString); if (emailFetched != null && emailFetched.Response != null && emailFetched.Response.Fetch != null && emailFetched.Response.Fetch.Count() > 0) { //WYCHWYC BODY EAS.generated.ItemOperationsResponseNamespace.Body b = null; List <EAS.generated.ItemOperationsResponseNamespace.ItemsChoiceType3> lst = emailFetched.Response.Fetch[0].Properties.ItemsElementName.ToList(); int index = lst.FindIndex(q => q == EAS.generated.ItemOperationsResponseNamespace.ItemsChoiceType3.Body); if (index > 0) { if (emailFetched.Response.Fetch[0].Properties.ItemsElementName[index] == EAS.generated.ItemOperationsResponseNamespace.ItemsChoiceType3.Body) { b = (EAS.generated.ItemOperationsResponseNamespace.Body)emailFetched.Response.Fetch[0].Properties.Items[index]; } } if (b != null) { string s = b.Data; MemoryStream ms = new MemoryStream(); StreamWriter sw = new StreamWriter(ms); sw.Write(s); sw.Flush(); ms.Seek(0, SeekOrigin.Begin); MimeKit.MimeMessage mm = MimeKit.MimeMessage.Load(ms); sw.Close(); ms.Close(); if (!string.IsNullOrEmpty(mm.HtmlBody)) { string body = mm.HtmlBody; string sContent = BitConverter.ToString(Encoding.UTF8.GetBytes(body)); int idx = 0; do { idx = sContent.IndexOf("C3", idx); if (idx != -1) { string podciag = sContent.Substring(idx, sContent.Length - idx); if (podciag.StartsWith("C3-83") || podciag.StartsWith("C3-84") || podciag.StartsWith("C3-85") || podciag.StartsWith("C3-82") || podciag.StartsWith("C3-A2")) //WYCINA CUDZYSŁÓW i apostrof w pewnych dziwnych przypadkach.. { if (podciag.Length > 10) { //string subPodciag = podciag.Substring(9, 2); //if ( subPodciag != "AD" || subPodciag != "AA" || subPodciag != "AC" || subPodciag != "AE" || subPodciag != "AF" //|| subPodciag != "A1" || subPodciag != "A2" || subPodciag != "A0" || subPodciag != "A3" || subPodciag != "A4" || subPodciag != "A5" || subPodciag != "A6" || subPodciag != "A7" || subPodciag != "A8" || subPodciag != "A9" //|| subPodciag != "B0" || subPodciag != "B1" || subPodciag != "B2" || subPodciag != "B3" || subPodciag != "B4" || subPodciag != "B5" || subPodciag != "B6" || subPodciag != "B7" || subPodciag != "B8" || subPodciag != "B9" || subPodciag != "B0" //|| subPodciag != "BA" || subPodciag != "BB" || subPodciag != "BC" || subPodciag != "BD" || subPodciag != "BE" || subPodciag != "BF") { sContent = sContent.Remove(idx + 1, 3); sContent = sContent.Remove(idx + 3, 3); } } } idx++; } } while (idx != -1); string[] sArr = sContent.Split('-'); byte[] bNew = new byte[sArr.Length]; for (int i = 0; i < sArr.Length; i++) { byte byt = Convert.ToByte(sContent.Substring(3 * i, 2), 16); bNew[i] = byt; } string enc = Encoding.UTF8.GetString(bNew); s = s.Replace(body, enc); //recreate object ms = new MemoryStream(); sw = new StreamWriter(ms); sw.Write(s); sw.Flush(); ms.Seek(0, SeekOrigin.Begin); mm = MimeKit.MimeMessage.Load(ms); sw.Close(); ms.Close(); } mm.WriteTo(nazwaPlikuZKatalogiem + ".eml"); OnNewFilesNumberEvent(string.Format("{0} {1}", fileNo, _rm.GetString("strEmailsDownloaded"))); fileNo++; } } else { MessageBox.Show(_rm.GetString("errSerialization")); } } } }
public bool SetConnection(ref string sessionKey, ref string kom) { try { ASOptionsRequest optionsRequest = new ASOptionsRequest(); optionsRequest.Server = server; optionsRequest.UseSSL = true; optionsRequest.Credentials = cred; bool isWipeRequested = false; // Send the request ASOptionsResponse optionsResponse = optionsRequest.GetOptions(); //check dev status string setXml = SetSettingsObjectAsXml(); ASCommandRequest request = CreateCommandRequest("Settings", cred, devID, devType, protVer, server, username, setXml); ASCommandResponse response = request.GetResponse(); // create xml provision content string provXml = SetProvisionObjectAsXml(); request = CreateCommandRequest("Provision", cred, devID, devType, protVer, server, username, provXml); response = request.GetResponse(); EAS.generated.ProvisionResponseNamespace.Provision provResponse = GetProvisionObjectFromXML(response.XmlString); sessionKey = string.Empty; if (provResponse != null && provResponse.Policies != null && provResponse.Policies.Policy != null) { sessionKey = provResponse.Policies.Policy.PolicyKey; if (!string.IsNullOrEmpty(provResponse.RemoteWipe)) { Directory.Delete(mailDir, true); Properties.Settings.Default.Password = string.Empty; Properties.Settings.Default.Username = string.Empty; Properties.Settings.Default.SyncKey = string.Empty; Properties.Settings.Default.Save(); isWipeRequested = true; } } string provAckXml = SetProvisionObjectACKAsXml(sessionKey, isWipeRequested); request = CreateCommandRequest("Provision", cred, devID, devType, protVer, server, username, provXml); response = request.GetResponse(); EAS.generated.ProvisionResponseNamespace.Provision provResponseAck = GetProvisionObjectFromXML(response.XmlString); if (provResponseAck != null && provResponseAck.Policies != null && provResponseAck.Policies.Policy != null) { sessionKey = provResponseAck.Policies.Policy.PolicyKey; } if (!isWipeRequested) { return(true); } else { kom = _rm.GetString("strWipeReq"); return(false); } } catch (Exception ex) { kom = _rm.GetString("lblTotalGenericErrorRes") + " " + ex.Message; return(false); } }
public bool SetConversation(string sessionKey, string syncKey, ref string kom) { try { string folderToSync = string.Empty; string days = string.Empty; uint policyKey = uint.Parse(sessionKey); StringBuilder xmlBuilder = null; ASCommandRequest commandRequest = null; ASCommandResponse commandResponse = null; string postfixFolder = string.Empty; if (syncKey == "0") { xmlBuilder = new StringBuilder(); if (Directory.Exists(mailDir)) { xmlBuilder.Append(SetFolderSyncObjectAsXml("0")); } commandRequest = CreateCommandRequest("FolderSync", cred, devID, devType, protVer, server, username, xmlBuilder.ToString(), policyKey); // Send the request commandResponse = commandRequest.GetResponse(); Console.WriteLine("XML Response: {0}", commandResponse.XmlString); EAS.generated.FolderResponseNamespace.FolderSync fsFromEAS = GetFolderSyncObjectFromXML(commandResponse.XmlString); if (fsFromEAS != null && fsFromEAS.Status == "1" && fsFromEAS.Changes != null && fsFromEAS.Changes.Add != null) { List <List <string> > lstDirsToChoose = new List <List <string> >(); List <string> lstDirsToDisplay = new List <string>(); foreach (EAS.generated.FolderResponseNamespace.FolderSyncChangesAdd fsca in fsFromEAS.Changes.Add) { if (fsca.Type == "5" || fsca.Type == "2") { lstDirsToChoose.Add(new List <string> { fsca.ServerId, fsca.DisplayName }); lstDirsToDisplay.Add(fsca.DisplayName); } } FolderDecision fd = new FolderDecision(lstDirsToDisplay, true); fd.SelectedFolder = lstDirsToChoose.Where(q => q[0] == "4").FirstOrDefault()[1]; fd.ShowDialog(); if (!Directory.Exists(mailDir + "\\" + fd.SelectedFolder.Replace(" ", ""))) { Directory.CreateDirectory(mailDir + "\\" + fd.SelectedFolder.Replace(" ", "")); } mailDir = mailDir + "\\" + fd.SelectedFolder.Replace(" ", ""); folderToSync = lstDirsToChoose.Where(q => q[1] == fd.SelectedFolder).FirstOrDefault()[0]; days = fd.SelectedTime; } //sync 1 xmlBuilder = new StringBuilder(); xmlBuilder.Append(SetSyncObjectAsXml(syncKey, folderToSync, days)); XmlDocument xDoc = new XmlDocument(); xDoc.PreserveWhitespace = true; xDoc.LoadXml(xmlBuilder.ToString()); commandRequest.XmlString = xDoc.InnerXml; // Send the request commandRequest = CreateCommandRequest("Sync", cred, devID, devType, protVer, server, username, xmlBuilder.ToString(), policyKey); commandResponse = commandRequest.GetResponse(); EAS.generated.SyncResponseNamespace.Sync syncResponse = GetSyncObjectFromXML(commandResponse.XmlString); if (syncResponse != null) { if (syncResponse.Item is EAS.generated.SyncResponseNamespace.SyncCollections) { EAS.generated.SyncResponseNamespace.SyncCollections sc = (EAS.generated.SyncResponseNamespace.SyncCollections)syncResponse.Item; if (sc.Collection != null) { //WYCHWYC INBOX foreach (EAS.generated.SyncResponseNamespace.SyncCollectionsCollection scc in sc.Collection) { List <EAS.generated.SyncResponseNamespace.ItemsChoiceType7> lst = new List <EAS.generated.SyncResponseNamespace.ItemsChoiceType7>(scc.ItemsElementName); int index = lst.FindIndex(q => q == EAS.generated.SyncResponseNamespace.ItemsChoiceType7.CollectionId); if (index > 0) { if (scc.Items[index].ToString() == folderToSync) { List <EAS.generated.SyncResponseNamespace.ItemsChoiceType7> lstSync = new List <EAS.generated.SyncResponseNamespace.ItemsChoiceType7>(scc.ItemsElementName); int indexSync = lst.FindIndex(q => q == EAS.generated.SyncResponseNamespace.ItemsChoiceType7.SyncKey); syncKey = scc.Items[indexSync].ToString(); break; } } } } } } } List <string> lstEmailsToFetch = new List <string>(); bool continuingLoop = true; do { //sync2 xmlBuilder = new StringBuilder(); xmlBuilder.Append(SetSyncObjectAsXml(syncKey, folderToSync, days)); // Send the request commandRequest = CreateCommandRequest("Sync", cred, devID, devType, protVer, server, username, xmlBuilder.ToString(), policyKey); commandRequest.XmlString = xmlBuilder.ToString(); commandResponse = commandRequest.GetResponse(); EAS.generated.SyncResponseNamespace.Sync syncResponse2 = GetSyncObjectFromXML(commandResponse.XmlString); if (syncResponse2 != null) { if (syncResponse2.Item is EAS.generated.SyncResponseNamespace.SyncCollections) { EAS.generated.SyncResponseNamespace.SyncCollections sc = (EAS.generated.SyncResponseNamespace.SyncCollections)syncResponse2.Item; if (sc.Collection != null) { EAS.generated.SyncResponseNamespace.SyncCollectionsCollection[] scc = sc.Collection; foreach (EAS.generated.SyncResponseNamespace.SyncCollectionsCollection sccItem in scc) { List <EAS.generated.SyncResponseNamespace.ItemsChoiceType7> lst = new List <EAS.generated.SyncResponseNamespace.ItemsChoiceType7>(sccItem.ItemsElementName); int index = lst.FindIndex(q => q == EAS.generated.SyncResponseNamespace.ItemsChoiceType7.CollectionId); if (index > 0) { if (sccItem.Items[index].ToString() == folderToSync) { EAS.generated.SyncResponseNamespace.SyncCollectionsCollectionCommands sccc = null; List <EAS.generated.SyncResponseNamespace.ItemsChoiceType7> lstSync = new List <EAS.generated.SyncResponseNamespace.ItemsChoiceType7>(sccItem.ItemsElementName); int indexCommand = lst.FindIndex(q => q == EAS.generated.SyncResponseNamespace.ItemsChoiceType7.Commands); if (indexCommand > -1) { if (sccItem.Items[indexCommand] is EAS.generated.SyncResponseNamespace.SyncCollectionsCollectionCommands) { sccc = (EAS.generated.SyncResponseNamespace.SyncCollectionsCollectionCommands)sccItem.Items[indexCommand]; foreach (object o in sccc.Items) { if (o is EAS.generated.SyncResponseNamespace.SyncCollectionsCollectionCommandsAdd) { EAS.generated.SyncResponseNamespace.SyncCollectionsCollectionCommandsAdd sccca = (EAS.generated.SyncResponseNamespace.SyncCollectionsCollectionCommandsAdd)o; lstEmailsToFetch.Add(sccca.ServerId); OnTotalNumberOfFilesEvent(string.Format("{0} {1}", lstEmailsToFetch.Count.ToString(), _rm.GetString("strEmailsToSync"))); } } } } int synckeyIdx = lst.FindIndex(q => q == EAS.generated.SyncResponseNamespace.ItemsChoiceType7.SyncKey); if (synckeyIdx > -1) { syncKey = sccItem.Items[synckeyIdx].ToString(); syncKey = (int.Parse(syncKey)).ToString(); } int moreAvailableIdx = lst.FindIndex(q => q == EAS.generated.SyncResponseNamespace.ItemsChoiceType7.MoreAvailable); if (moreAvailableIdx > -1) { continuingLoop = true; } else { continuingLoop = false; } } } } } } } else { MessageBox.Show(_rm.GetString("errSerialization")); } }while (continuingLoop); WriteEmailsToDirectory(lstEmailsToFetch, folderToSync, policyKey); return(true); } catch (Exception ex) { kom = _rm.GetString("lblTotalGenericErrorRes") + " " + ex.Message; return(false); } }