2009年2月23日星期一

Export all SMS messages as xml file from your Windows Mobile

昨天觉得手机中病毒了,老莫名其妙拨号上网。准备重装系统,先备份短信。.NET Compact Framework类库中outlook类中的SMS若干方法形同虚设于是找了一个第三方.NET的类库,MAPIdotnet。他构建在Windows API的MAPIlib基础上,专门用来访问Windows Mobile的短信的。他本身是for Smartphone的,稍微改一下就能在我的Pocket PC上跑了。
            //export all
            FileStream fs = new FileStream("smsExport"+DateTime.Now.ToString("yy_MM_d_H_mm")+".xml", FileMode.OpenOrCreate);
            StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
            //
            sw.Write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?><sms>");
            for (int k = 0, K = this.stores.Length; k < K; k++)
            {
                IMAPIMsgStore store = this.stores[k];
                sw.Write("<store name=\""+store.ToString()+"\">");
                IMAPIFolder folder = store.RootFolder.OpenFolder();
                IMAPIFolderID[] subFolders = folder.GetSubFolders((int)folder.NumSubFolders);
                foreach (IMAPIFolderID fId in subFolders)
                {
                    IMAPIFolder f = fId.OpenFolder();
                    sw.Write("<folder name=\"" + f.ToString() + "\">");
                    IMAPIMessage[] messages = f.GetNextMessages(f.NumSubItems);
                    for (int i = 0, length = messages.Length; i < length; i++)
                    {
                        IMAPIMessage msg = messages[i];
                        msg.PopulateProperties(EMessageProperties.DeliveryTime | EMessageProperties.Sender | EMessageProperties.Subject);
                        //store folder subfolder msg
                        sw.Write("<msg when=\"" + msg.LocalDeliveryTime.ToString("H:mm d/MM/yy") + "\">");
                        sw.Write("<Sender name=\"" + msg.Sender.Name + "\"><![CDATA[" + msg.Sender.FullAddress + "]]></Sender>");
                        sw.Write("<Recipients>");
                        for (int j = 0; j < msg.Recipients.Length; j++)
                        {
                            IMAPIContact recipient = msg.Recipients[j];
                            sw.Write("<Recipient name=\"" + recipient.Name + "\">" + recipient.FullAddress + "</Recipient>");
                        }
                        sw.Write("</Recipients>");
                        sw.Write("<Subject><![CDATA["+msg.Subject+"]]></Subject>");
                     //   sw.Write("<Body><![CDATA[" + msg.Body + "]]></Body>");
                        sw.Write("</msg>");
                    }
                    sw.Write("</folder>");
                }
                sw.Write("</store>");
            }
            sw.Write("</sms>");
            //
            sw.Close();
            fs.Close();

没有评论: