Пример #1
0
        private void UploadNotesAsMails(string folder, List<Note> notesEvernote, string exportFile)
        {
            int uploadcount = 0;
            foreach (Note n in notesEvernote)
            {
                if (n.Action == NoteAction.UploadToIMAP)
                {
                    uploadcount++;
                }
            }

            int counter = 0;
            foreach (Note n in notesEvernote)
            {
                if (cancelled)
                {
                    break;
                }

                if (n.Action == NoteAction.UploadToIMAP)
                {
                    SetInfo(null, string.Format("uploading note ({0} of {1}) : \"{2}\"", counter + 1, uploadcount, n.Title), counter++, uploadcount);

                    XmlTextReader xtrInput;
                    XmlDocument xmlDocItem;

                    xtrInput = new XmlTextReader(exportFile);

                    while (xtrInput.Read())
                    {
                        while ((xtrInput.NodeType == XmlNodeType.Element) && (xtrInput.Name.ToLower() == "note"))
                        {
                            if (cancelled)
                            {
                                break;
                            }

                            xmlDocItem = new XmlDocument();
                            xmlDocItem.LoadXml(xtrInput.ReadOuterXml());
                            XmlNode node = xmlDocItem.FirstChild;

                            // node is <note> element
                            // node.FirstChild.InnerText is <title>
                            node = node.FirstChild;

                            if (node.InnerText == n.Title)
                            {
                                node = node.NextSibling;
                                if (n.Content == node.InnerXml.Replace("\r", string.Empty))
                                {
                                    XmlNodeList atts = xmlDocItem.GetElementsByTagName("resource");
                                    foreach (XmlNode xmln in atts)
                                    {
                                        Attachment attachment = new Attachment();
                                        attachment.Base64Data = xmln.FirstChild.InnerText;
                                        byte[] data = Convert.FromBase64String(xmln.FirstChild.InnerText);
                                        byte[] hash = new System.Security.Cryptography.MD5CryptoServiceProvider().ComputeHash(data);
                                        string hashHex = BitConverter.ToString(hash).Replace("-", string.Empty).ToLower();

                                        attachment.Hash = hashHex;

                                        XmlNodeList fns = xmlDocItem.GetElementsByTagName("file-name");
                                        if (fns.Count > n.Attachments.Count)
                                        {
                                            attachment.FileName = fns.Item(n.Attachments.Count).InnerText;
                                        }

                                        XmlNodeList mimes = xmlDocItem.GetElementsByTagName("mime");
                                        if (mimes.Count > n.Attachments.Count)
                                        {
                                            attachment.ContentType = mimes.Item(n.Attachments.Count).InnerText;
                                        }

                                        n.Attachments.Add(attachment);
                                    }
                                }
                            }
                        }
                    }

                    xtrInput.Close();

                    string htmlBody = n.Content;

                    List<LinkedResource> linkedResources = new List<LinkedResource>();
                    List<System.Net.Mail.Attachment> attachedResources = new List<System.Net.Mail.Attachment>();
                    foreach (Attachment attachment in n.Attachments)
                    {
                        Regex rx = new Regex(@"<en-media\b[^>]*?hash=""" + attachment.Hash + @"""[^>]*/>", RegexOptions.IgnoreCase);
                        if ((attachment.ContentType != null) && (attachment.ContentType.Contains("image") && rx.Match(htmlBody).Success))
                        {
                            // replace the <en-media /> tag with an <img /> tag
                            htmlBody = rx.Replace(htmlBody, @"<img src=""cid:" + attachment.Hash + @"""/>");
                            byte[] data = Convert.FromBase64String(attachment.Base64Data);
                            Stream s = new MemoryStream(data);
                            ContentType ct = new ContentType();
                            ct.Name = attachment.FileName;
                            ct.MediaType = attachment.ContentType;
                            LinkedResource lr = new LinkedResource(s, ct);
                            lr.ContentId = attachment.Hash;
                            linkedResources.Add(lr);
                        }
                        else
                        {
                            byte[] data = Convert.FromBase64String(attachment.Base64Data);
                            Stream s = new MemoryStream(data);
                            ContentType ct = new ContentType();
                            ct.Name = attachment.FileName != null ? attachment.FileName : string.Empty;
                            ct.MediaType = attachment.ContentType != null ? attachment.ContentType : string.Empty;
                            System.Net.Mail.Attachment a = new System.Net.Mail.Attachment(s, ct);
                            attachedResources.Add(a);
                        }
                    }

                    htmlBody = htmlBody.Replace(@"<![CDATA[<?xml version=""1.0"" encoding=""UTF-8""?>", string.Empty);
                    htmlBody = htmlBody.Replace(@"<!DOCTYPE en-note SYSTEM ""http://xml.evernote.com/pub/enml2.dtd"">", string.Empty);
                    htmlBody = htmlBody.Replace("<en-note>", "<body>");
                    htmlBody = htmlBody.Replace("</en-note>]]>", "</body>");
                    htmlBody = htmlBody.Trim();
                    htmlBody = @"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.01 Transitional//EN""><head></head>" + htmlBody;
                    MailMessage mailMsg = new MailMessage();

                    AlternateView altViewHtml = AlternateView.CreateAlternateViewFromString(htmlBody, Encoding.UTF8, MediaTypeNames.Text.Html);
                    foreach (LinkedResource lr in linkedResources)
                    {
                        altViewHtml.LinkedResources.Add(lr);
                    }

                    // Add the alternate views instead of using MailMessage.Body
                    mailMsg.AlternateViews.Add(altViewHtml);
                    foreach (System.Net.Mail.Attachment a in attachedResources)
                    {
                        mailMsg.Attachments.Add(a);
                    }

                    mailMsg.From = new MailAddress("EveImSync <*****@*****.**>");
                    mailMsg.To.Add(new MailAddress("EveImSync <*****@*****.**>"));
                    mailMsg.Subject = n.Title;
                    string eml = mailMsg.GetEmailAsString();

                    Regex rex = new Regex(@"^date:(.*)$", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                    eml = rex.Replace(eml, "Date: " + n.Date.ToString("ddd, dd MMM yyyy HH:mm:ss K"));

                    // find the folder to upload to
                    string tagfolder = folder;
                    if (n.Tags.Count > 0)
                    {
                        tagfolder = tagfolder + "/" + n.Tags[0];
                    }

                    IFolder currentFolder = GetOrCreateFolderByPath(tagfolder);
                    string customFlag = "xeveim" + n.ContentHash;

                    // now upload the new note
                    int numMsg = currentFolder.Messages.Length;
                    client.RequestManager.SubmitAndWait(new AppendRequest(eml, customFlag, currentFolder, null), false);

                    if (n.Tags.Count > 1)
                    {
                        IMessage[] oldMsgs = client.MailboxManager.GetMessagesByFolder(currentFolder);
                        client.RequestManager.SubmitAndWait(new MessageListRequest(currentFolder, null), false);
                        IMessage[] newMsgs = client.MailboxManager.GetMessagesByFolder(currentFolder);
                        IMessage newMsg = null;
                        foreach (IMessage imsg in newMsgs)
                        {
                            bool found = false;
                            foreach (IMessage omsg in oldMsgs)
                            {
                                if (imsg.UID == omsg.UID)
                                {
                                    found = true;
                                    break;
                                }
                            }

                            if (!found)
                            {
                                newMsg = client.MailboxManager.GetMessageByUID(imsg.UID, currentFolder.ID);
                                break;
                            }
                        }

                        // copy the email to all tag folders
                        for (int i = 1; i < n.Tags.Count; ++i)
                        {
                            if (cancelled)
                            {
                                break;
                            }

                            tagfolder = folder + "/" + n.Tags[i];
                            IFolder tagFolder = GetOrCreateFolderByPath(tagfolder);

                            client.RequestManager.SubmitAndWait(new CopyMessageRequest(newMsg, tagFolder, null), true);
                            client.RequestManager.SubmitAndWait(new MessageListRequest(tagFolder, null), true);
                        }
                    }
                }
            }
        }