/// <summary>
        ///
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public Message ResolveMailByLSoft(String FilePath)
        {
            Message oMsg = new Message();

            ADODB.Stream stm = null;
            try
            {
                stm = new ADODB.Stream();
                stm.Open(System.Reflection.Missing.Value,
                         ADODB.ConnectModeEnum.adModeUnknown,
                         ADODB.StreamOpenOptionsEnum.adOpenStreamUnspecified, "", "");
                stm.Type = ADODB.StreamTypeEnum.adTypeBinary; //二进制方式读入

                stm.LoadFromFile(FilePath);                   //将EML读入数据流

                oMsg.DataSource.OpenObject(stm, "_stream");   //将EML数据流载入到CDO.Message,要做解析的话,后面就可以了。
            }
            catch (IOException)
            {
            }
            finally
            {
                stm.Close();
            }
            return(oMsg);
        }
Example #2
0
        /// <summary>
        /// 解析邮件文件为邮件对象
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public Message ResolveMailByLSoft(String FilePath)
        {
            Message oMsg = new Message();
            ADODB.Stream stm = null;
            //读取EML文件到CDO.MESSAGE,做分析的话,实际是用了下面的部分

            try
            {
                stm = new ADODB.Stream();
                stm.Open(System.Reflection.Missing.Value,
                ADODB.ConnectModeEnum.adModeUnknown,
                ADODB.StreamOpenOptionsEnum.adOpenStreamUnspecified, "", "");
                stm.Type = ADODB.StreamTypeEnum.adTypeBinary;//二进制方式读入

                stm.LoadFromFile(FilePath); //将EML读入数据流

                oMsg.DataSource.OpenObject(stm, "_stream"); //将EML数据流载入到CDO.Message,要做解析的话,后面就可以了。

            }
            catch (IOException)
            {

            }
            finally
            {
                stm.Close();
            }
            return oMsg;
        }
Example #3
0
 protected CDO.Message ReadMessage(String emlFileName)
 {
     ADODB.Stream stream = new ADODB.Stream();
     stream.Open(Type.Missing, ADODB.ConnectModeEnum.adModeUnknown, ADODB.StreamOpenOptionsEnum.adOpenStreamUnspecified, String.Empty, String.Empty);
     stream.LoadFromFile(emlFileName);
     stream.Flush();
     return(ReadMessage(stream));
 }
Example #4
0
 protected CDO.Message ReadMessage(byte[] buffer)
 {
     ADODB.Stream stream = new ADODB.Stream();
     stream.Type = ADODB.StreamTypeEnum.adTypeBinary;
     //stream.Open(Type.Missing, ADODB.ConnectModeEnum.adModeUnknown, ADODB.StreamOpenOptionsEnum.adOpenStreamUnspecified, String.Empty, String.Empty);
     stream.Open();
     stream.Write(buffer);
     stream.Flush();
     return(ReadMessage(stream));
 }
Example #5
0
        protected Message ReadMessage(String emlFileName)
        {
            Message msg    = new Message();
            Stream  stream = new Stream();

            stream.Open(Type.Missing,
                        ADODB.ConnectModeEnum.adModeUnknown,
                        ADODB.StreamOpenOptionsEnum.adOpenStreamUnspecified,
                        String.Empty,
                        String.Empty);
            stream.LoadFromFile(emlFileName);
            stream.Flush();
            msg.DataSource.OpenObject(stream, "_Stream");
            msg.DataSource.Save();
            return(msg);
        }
Example #6
0
        /// <summary>
        /// Sends the email MHTML.
        /// </summary>
        /// <param name="message">The message.</param>
        public virtual void SendMhtml(MailMessage message)
        {
            lock (_lock)
                try
                {
                    var m = new CDO.Message();

                    // set message
                    var s = new ADODB.Stream();
                    s.Charset = "ascii";
                    s.Open();
                    s.WriteText(message.Body);
                    m.DataSource.OpenObject(s, "_Stream");

                    // set configuration
                    var f = m.Configuration.Fields;
                    switch (DeliveryMethod)
                    {
                        case SmtpDeliveryMethod.Network:
                            f["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = CDO.CdoSendUsing.cdoSendUsingPort;
                            f["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = Host;
                            break;
                        case SmtpDeliveryMethod.SpecifiedPickupDirectory:
                            f["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = CDO.CdoSendUsing.cdoSendUsingPickup;
                            f["http://schemas.microsoft.com/cdo/configuration/smtpserverpickupdirectory"].Value = PickupDirectoryLocation;
                            break;
                        default: throw new NotSupportedException();
                    }
                    f.Update();

                    // set other values
                    m.MimeFormatted = true;
                    m.Subject = message.Subject;
                    if (message.From != null) m.From = message.From.ToString();
                    var to = (message.To != null ? string.Join(",", message.To.Select(x => x.ToString()).ToArray()) : null);
                    if (!string.IsNullOrEmpty(to)) m.To = to;
                    var bcc = (message.Bcc != null ? string.Join(",", message.Bcc.Select(x => x.ToString()).ToArray()) : null);
                    if (!string.IsNullOrEmpty(to)) m.BCC = bcc;
                    var cc = (message.CC != null ? string.Join(",", message.CC.Select(x => x.ToString()).ToArray()) : null);
                    if (!string.IsNullOrEmpty(to)) m.CC = cc;
                    if (message.Attachments != null)
                        foreach (var attachment in message.Attachments.Where(x => x != null))
                            AddAttachement(m, attachment, false);
                    m.Send();
                }
                catch (Exception ex) { throw ex; }
        }
Example #7
0
        public static CDO.Message GetEmlFromFile(String emlFileName)
        {
            CDO.Message msg = new CDO.Message();
            try
            {
                ADODB.Stream stream = new ADODB.Stream();
                stream.Open(Type.Missing, ADODB.ConnectModeEnum.adModeUnknown,
                            ADODB.StreamOpenOptionsEnum.adOpenStreamUnspecified, String.Empty, String.Empty);
                stream.LoadFromFile(emlFileName);
                stream.Flush();
                msg.DataSource.OpenObject(stream, "_Stream");
                msg.DataSource.Save();
            }
            catch
            {
            }

            return(msg);
        }
        private void AddAttachement(CDO.Message m, Attachment attachment, bool allowUnicode)
        {
            // set message
            var s = new ADODB.Stream();

            s.Charset = "UTF-8";
            s.Open();
            s.Type = ADODB.StreamTypeEnum.adTypeBinary;
            int bytesRead;
            var buffer = new byte[0x4400];

            using (var acs = attachment.ContentStream)
                while ((bytesRead = acs.Read(buffer, 0, 0x4400)) > 0)
                {
                    if (bytesRead == 0x4400)
                    {
                        s.Write(buffer);
                    }
                    else
                    {
                        Array.Resize(ref buffer, bytesRead);
                        s.Write(buffer);
                        break;
                    }
                }
            s.Flush();
            s.Position = 0;
            //
            var p = m.Attachments.Add();

            p.ContentMediaType        = attachment.ContentType.ToString();
            p.ContentTransferEncoding = "base64";
            var ds = p.GetDecodedContentStream();

            s.CopyTo(ds);
            ds.Flush();
        }
Example #9
0
 private void AddAttachement(CDO.Message m, Attachment attachment, bool allowUnicode)
 {
     // set message
     var s = new ADODB.Stream();
     s.Charset = "UTF-8";
     s.Open();
     s.Type = ADODB.StreamTypeEnum.adTypeBinary;
     int bytesRead;
     var buffer = new byte[0x4400];
     using (var acs = attachment.ContentStream)
         while ((bytesRead = acs.Read(buffer, 0, 0x4400)) > 0)
             if (bytesRead == 0x4400)
                 s.Write(buffer);
             else
             {
                 Array.Resize(ref buffer, bytesRead);
                 s.Write(buffer);
                 break;
             }
     s.Flush();
     s.Position = 0;
     //
     var p = m.Attachments.Add();
     p.ContentMediaType = attachment.ContentType.ToString();
     p.ContentTransferEncoding = "base64";
     var ds = p.GetDecodedContentStream();
     s.CopyTo(ds);
     ds.Flush();
 }
Example #10
0
 /// <summary>
 /// Read eml file into CDO message
 /// </summary>
 /// <param name="filepath">Path to eml file</param>
 /// <returns>CDO message</returns>
 private static CDO.Message ReadEml(string filepath)
 {
     var oMsg = new CDO.Message();
     var stm = new ADODB.Stream();
     stm.Open(System.Reflection.Missing.Value);
     stm.Type = ADODB.StreamTypeEnum.adTypeBinary;
     stm.LoadFromFile(filepath);
     oMsg.DataSource.OpenObject(stm, "_stream");
     stm.Close();
     return oMsg;
 }
Example #11
0
        private bool LoadMessage(string sLoadType, string sMessage)
        {
            ADODB.Stream objMsgStream = new ADODB.Stream();
            string       sFileName    = "";

            objCDOMsg = null;
            objCDOMsg = new CDO.Message();
            bLoaded   = false;

            try {
                this.Cursor = Cursors.WaitCursor;


                // First Get the Raw Mime and display it.
                string sFileText = null;
                if (sLoadType == "file")
                {
                    //MyApp.AppUtil xObj = new MyApp.AppUtil();
                    sFileName = txtFileName.Text.Trim();
                    sFileText = UserIoHelper.GetFileAsString(sFileName);


                    txtMime.Text = sFileText;


                    // Now Lets read this thing
                    objMsgStream.Open();
                    objMsgStream.LoadFromFile(sFileName);
                    objCDOMsg.BodyPart.DataSource.OpenObject(objMsgStream, "IStream");
                    m_MessageMime = sFileText;
                }
                else
                {
                    sFileName    = Path.GetTempFileName();
                    txtMime.Text = sMessage;


                    UserIoHelper.SaveStringAsFile(sMessage, sFileName);

                    // Ran into problems writing mime text to a message, working around by using a temp file
                    objMsgStream.Open();
                    objMsgStream.LoadFromFile(sFileName);
                    // Read temp file

                    objCDOMsg.BodyPart.DataSource.OpenObject(objMsgStream, "IStream");
                    // read it
                    File.Delete(sFileName);
                    // Get rid of temp file
                    m_MessageMime = sMessage;
                }

                LoadMessageFields();
                //LoadAttachemntsFields();
                ExtractBodyPart(objCDOMsg.BodyPart);


                //Marshal.ReleaseComObject(objMsgStream);
            }
            catch (Exception ex) {
                bLoaded = false;
                MessageBox.Show("Error Loading File: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

            try {
                System.Windows.Forms.TreeNode pNode = new System.Windows.Forms.TreeNode();
                TreeView1.Nodes.Clear();
                pNode = TreeView1.Nodes.Add("Root");
                // m_BaseURI
                pNode.Tag = objCDOMsg.BodyPart;



                int iBodyPartCount1 = 1;
                int iBodyPartCount2 = 1;
                int iBodyPartCount3 = 1;
                int iBodyPartCount4 = 1;
                int iBodyPartCount5 = 1;
                int iBodyPartCount6 = 1;
                int iBodyPartCount7 = 1;

                System.Windows.Forms.TreeNode aNode1 = null;
                System.Windows.Forms.TreeNode aNode2 = null;
                System.Windows.Forms.TreeNode aNode3 = null;
                System.Windows.Forms.TreeNode aNode4 = null;
                System.Windows.Forms.TreeNode aNode5 = null;
                System.Windows.Forms.TreeNode aNode6 = null;
                System.Windows.Forms.TreeNode aNode7 = null;

                iBodyPartCount1 = 1;
                // Note: I'm using this big loop because i want to be able to see things better
                // as this code loops through each body part.  Using a recursive call to a method
                // just makes the debugging process more difficult.
                foreach (CDO.IBodyPart objBodypart1 in objCDOMsg.BodyPart.BodyParts)
                {
                    AddNode(objBodypart1, ref pNode, ref aNode1, iBodyPartCount1);

                    iBodyPartCount2 = 1;
                    foreach (CDO.IBodyPart objBodypart2 in objBodypart1.BodyParts)
                    {
                        AddNode(objBodypart2, ref aNode1, ref aNode2, iBodyPartCount2);
                        iBodyPartCount3 = 1;
                        foreach (CDO.IBodyPart objBodypart3 in objBodypart2.BodyParts)
                        {
                            AddNode(objBodypart3, ref aNode2, ref aNode3, iBodyPartCount3);
                            iBodyPartCount4 = 1;
                            foreach (CDO.IBodyPart objBodypart4 in objBodypart3.BodyParts)
                            {
                                AddNode(objBodypart4, ref aNode3, ref aNode4, iBodyPartCount4);
                                iBodyPartCount5 = 1;
                                foreach (CDO.IBodyPart objBodypart5 in objBodypart4.BodyParts)
                                {
                                    AddNode(objBodypart5, ref aNode4, ref aNode5, iBodyPartCount5);
                                    iBodyPartCount6 = 1;
                                    foreach (CDO.IBodyPart objBodypart6 in objBodypart5.BodyParts)
                                    {
                                        AddNode(objBodypart6, ref aNode5, ref aNode6, iBodyPartCount6);
                                        iBodyPartCount7 = 1;
                                        foreach (CDO.IBodyPart objBodypart7 in objBodypart6.BodyParts)
                                        {
                                            AddNode(objBodypart7, ref aNode6, ref aNode7, iBodyPartCount7);
                                            iBodyPartCount7 += 1;
                                            //Marshal.ReleaseComObject(objBodypart7);
                                        }
                                        iBodyPartCount6 += 1;
                                        // Marshal.ReleaseComObject(objBodypart6);
                                    }
                                    iBodyPartCount5 += 1;
                                    //Marshal.ReleaseComObject(objBodypart5);
                                }
                                iBodyPartCount4 += 1;
                                //Marshal.ReleaseComObject(objBodypart4);
                            }
                            iBodyPartCount3 += 1;
                            // Marshal.ReleaseComObject(objBodypart3);
                        }
                        iBodyPartCount2 += 1;
                        //Marshal.ReleaseComObject(objBodypart2);
                    }
                    iBodyPartCount1 += 1;
                    //Marshal.ReleaseComObject(objBodypart1);
                }
                pNode.ExpandAll();

                bLoaded = true;
            } catch (Exception ex) {
                bLoaded = false;
                MessageBox.Show("Error Loading Message Body Parts: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

            objMsgStream = null;
            Cursor       = Cursors.Default;
            return(bLoaded);
        }
        /// <summary>
        /// Sends the email MHTML.
        /// </summary>
        /// <param name="message">The message.</param>
        public virtual void SendMhtml(MailMessage message)
        {
            lock (_lock)
                try
                {
                    var m = new CDO.Message();

                    // set message
                    var s = new ADODB.Stream();
                    s.Charset = "ascii";
                    s.Open();
                    s.WriteText(message.Body);
                    m.DataSource.OpenObject(s, "_Stream");

                    // set configuration
                    var f = m.Configuration.Fields;
                    switch (DeliveryMethod)
                    {
                    case SmtpDeliveryMethod.Network:
                        f["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value  = CDO.CdoSendUsing.cdoSendUsingPort;
                        f["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = Host;
                        break;

                    case SmtpDeliveryMethod.SpecifiedPickupDirectory:
                        f["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = CDO.CdoSendUsing.cdoSendUsingPickup;
                        f["http://schemas.microsoft.com/cdo/configuration/smtpserverpickupdirectory"].Value = PickupDirectoryLocation;
                        break;

                    default: throw new NotSupportedException();
                    }
                    f.Update();

                    // set other values
                    m.MimeFormatted = true;
                    m.Subject       = message.Subject;
                    if (message.From != null)
                    {
                        m.From = message.From.ToString();
                    }
                    var to = (message.To != null ? string.Join(",", message.To.Select(x => x.ToString()).ToArray()) : null);
                    if (!string.IsNullOrEmpty(to))
                    {
                        m.To = to;
                    }
                    var bcc = (message.Bcc != null ? string.Join(",", message.Bcc.Select(x => x.ToString()).ToArray()) : null);
                    if (!string.IsNullOrEmpty(to))
                    {
                        m.BCC = bcc;
                    }
                    var cc = (message.CC != null ? string.Join(",", message.CC.Select(x => x.ToString()).ToArray()) : null);
                    if (!string.IsNullOrEmpty(to))
                    {
                        m.CC = cc;
                    }
                    if (message.Attachments != null)
                    {
                        foreach (var attachment in message.Attachments.Where(x => x != null))
                        {
                            AddAttachement(m, attachment, false);
                        }
                    }
                    m.Send();
                }
                catch (Exception ex) { throw ex; }
        }