Esempio n. 1
0
 /// <summary>
 /// save attachment to file
 /// </summary>
 /// <param name="attItem">Attachment</param>
 /// <param name="strFileName">File to be saved to</param>
 /// <returns>true if save successfully, false if failed</returns>
 public bool SaveAttachment(Attachment attItem, string strFileName)
 {
     byte[] da;
     try
     {
         //				FileStream fs=File.Create(strFileName);
         //				byte[] da;
         //				if(attItem.ContentFileName.Length>0)
         //				{
         //					da=attItem.DecodedAttachment;
         //				}
         //				else
         //				{
         //					this.GetMessageBody(attItem.DecodeAttachmentAsText());
         //					da=Encoding.Default.GetBytes((string)this.MessageBody[this.MessageBody.Count-1]);
         //				}
         //				fs.Write(da,0,da.Length);
         //				fs.Close();
         //				return true;
         if(attItem.InBytes)
         {
             da=attItem.RawBytes;
         }
         else if(attItem.ContentFileName.Length>0)
         {
             da=attItem.DecodedAttachment;
         }
         else if(attItem.ContentType.ToLower()=="message/rfc822".ToLower())
         {
             da=Encoding.Default.GetBytes(attItem.RawAttachment);
         }
         else
         {
             this.GetMessageBody(attItem.DecodeAsText());
             da=Encoding.Default.GetBytes((string)this.MessageBody[this.MessageBody.Count-1]);
         }
         return Utility.SaveByteContentToFile(strFileName,da);
     }
     catch
     {
         /*Utility.LogError("SaveAttachment():"+e.Message);
         return false;*/
         da=Encoding.Default.GetBytes(attItem.RawAttachment);
         return Utility.SaveByteContentToFile(strFileName,da);
     }
 }
Esempio n. 2
0
 /// <summary>
 /// verify if the attachment is MIME Email file
 /// </summary>
 /// <param name="attItem">attachment</param>
 /// <returns>if MIME Email file, return true, else, false</returns>
 public bool IsMIMEMailFile(Attachment attItem)
 {
     try
     {
         return (attItem.ContentFileName.ToLower().EndsWith(".eml".ToLower()) || attItem.ContentType.ToLower()=="message/rfc822".ToLower());
     }
     catch(Exception e)
     {
         Utility.LogError("IsMIMEMailFile():"+e.Message);
         return false;
     }
 }
Esempio n. 3
0
        /// <summary>
        /// set attachments
        /// </summary>
        private void set_attachments()
        {
            int indexOf_attachmentstart=0;
            int indexOfAttachmentEnd=0;
            bool processed=false;

            Attachment att=null;

            SetAttachmentBoundry2(_rawMessageBody);

            while(!processed)
            {
                if(Utility.IsNotNullText(_attachmentboundry))
                {
                    indexOf_attachmentstart=_rawMessageBody.IndexOf(_attachmentboundry,indexOf_attachmentstart)+_attachmentboundry.Length;
                    if(_rawMessageBody==""||indexOf_attachmentstart<0)return;

                    indexOfAttachmentEnd=_rawMessageBody.IndexOf(_attachmentboundry,indexOf_attachmentstart+1);
                }
                else
                {
                    indexOfAttachmentEnd=-1;
                }

                //if(indexOfAttachmentEnd<0)return;
                if(indexOfAttachmentEnd!=-1)
                {
                }
                else if(indexOfAttachmentEnd==-1&&!processed&&_attachmentCount==0)
                {
                    processed=true;
                    indexOfAttachmentEnd=_rawMessageBody.Length;
                }
                else
                    return;

                if(indexOf_attachmentstart==indexOfAttachmentEnd-9)
                {
                    indexOf_attachmentstart=0;
                    processed=true;
                }

                string strLine=_rawMessageBody.Substring(indexOf_attachmentstart,(indexOfAttachmentEnd-indexOf_attachmentstart-2));
                bool isMSTNEF;
                isMSTNEF=MIMETypes.IsMSTNEF(_contentType);
                att=new Attachment(strLine.Trim(),_contentType,!isMSTNEF);

                //ms-tnef format might contain multiple attachments
                if(MIMETypes.IsMSTNEF(att.ContentType) && AutoDecodeMSTNEF && !isMSTNEF)
                {
                    Utility.LogError("set_attachments():found ms-tnef file");
                    TNEFParser tnef=new TNEFParser();
                    TNEFAttachment tatt=new TNEFAttachment();
                    Attachment attNew=null;

                    tnef.Verbose=false;
                    tnef.BasePath=this.BasePath;
                    //tnef.LogFilePath=this.BasePath + "OpenPOP.TNEF.log";
                    if (tnef.OpenTNEFStream(att.DecodedAsBytes()))
                    {
                        if(tnef.Parse())
                        {
                            for (IDictionaryEnumerator i = tnef.Attachments().GetEnumerator(); i.MoveNext();)
                            {
                                tatt=(TNEFAttachment)i.Value;
                                attNew=new Attachment(tatt.FileContent,tatt.FileLength ,tatt.FileName,MIMETypes.GetMimeType(tatt.FileName));
                                _attachmentCount++;
                                _attachments.Add(attNew);
                            }
                        }
                        else
                            Utility.LogError("set_attachments():ms-tnef file parse failed");
                    }
                    else
                        Utility.LogError("set_attachments():ms-tnef file open failed");
                }
                else
                {
                    _attachmentCount++;
                    _attachments.Add(att);
                }

                indexOf_attachmentstart++;
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Get the proper attachment file name
        /// </summary>
        /// <param name="attItem">attachment</param>
        /// <returns>propery attachment file name</returns>
        public string GetAttachmentFileName(Attachment attItem)
        {
            int items=0;

            //return unique body file names
            for(int i=0;i<_attachments.Count;i++)
            {
                if(attItem.ContentFileName==attItem.DefaultFileName)
                {
                    items++;
                    attItem.ContentFileName=attItem.DefaultFileName2.Replace("*",items.ToString());
                }
            }
            string name=attItem.ContentFileName;

            return (name==null||name==""?(IsReport()==true?(this.IsMIMEMailFile(attItem)==true?attItem.DefaultMIMEFileName:attItem.DefaultReportFileName):(attItem.ContentID!=null?attItem.ContentID:attItem.DefaultFileName)):name);
        }
Esempio n. 5
0
        private void receiveMails()
        {
            /*Spartan.Net.Mail.POP3 pop3 = new Spartan.Net.Mail.POP3();
             * pop3.Connect(host, port, user, pass);*/
            receiverDone = false;
            int          count = -1;
            string       dirname;
            string       filename;
            BinaryWriter bw;
            FileStream   fs;
            BinaryReader br;

            //fs = new FileStream("E:\\redirector\\count.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
            //br = new BinaryReader(fs);
            //count = Int32.Parse(br.ReadString());
            //br.Close();
            //fs.Close();
            lock (this)
            {
                count = Int32.Parse(File.ReadAllText("E:\\redirector\\count.txt"));
            }
            Console.WriteLine("count is " + count);

            while (true)
            {
                // Thread.Sleep(0);
                isReceiverAsleep = false;
                int newMessages  = 0;
                int readMessages = 0;

                //pop3.ListMessages(out readMessages, out newMessages);

                OpenPOP.POP3.POPClient popclient = new OpenPOP.POP3.POPClient();
                popclient.Connect(host, port);
                popclient.Authenticate(user, pass);

                newMessages = popclient.GetMessageCount();

                for (int i = 1; i <= newMessages; i++)
                {
                    dirname = "E:\\redirector\\sent_mails\\" + (count + i);
                    Directory.CreateDirectory(dirname);
                    string message = "";
                    //pop3.RetrieveMessage(i, out message);
                    //pop3.DeleteMessage(i);
                    //bool fnsh = true;
                    //OpenPOP.MIMEParser.Message msg = new OpenPOP.MIMEParser.Message(ref fnsh, message);
                    OpenPOP.MIMEParser.Message msg = popclient.GetMessage(i, false);
                    message = msg.From + "\n" + msg.FromEmail + "\n" + msg.Subject + "\n" + msg.DateTimeInfo + "\n" + msg.AttachmentCount + "\n";
                    // Write to approproiate file instead of printing

                    //string show_msg = string.Format(sndr + "::" + subject);
                    //MessageBox.Show(show_msg);
                    for (int j = 1; j < msg.AttachmentCount; j++)
                    {
                        OpenPOP.MIMEParser.Attachment attachment = (OpenPOP.MIMEParser.Attachment)msg.Attachments[j];
                        message  = message + attachment.ContentFileName + "\n";
                        filename = dirname + "\\" + attachment.ContentFileName;
                        msg.SaveAttachment(attachment, filename);

                        /* byte[] bytes = attachment.DecodedAsBytes();
                         * filename = dirname + "\\" + attachment.ContentFileName;
                         * fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Read);
                         * bw = new BinaryWriter(fs);
                         * bw.Flush();
                         * bw.Close();
                         * bw.Write(bytes);
                         * fs.Close();*/
                        //MessageBox.Show(string.Format("attachment: {0}", attachment.ContentFileName));
                        //MessageBox.Show(attachment_txt);
                    }
                    //msg.SaveAttachments(dirname);
                    if (msg.AttachmentCount > 0)
                    {
                        OpenPOP.MIMEParser.Attachment attachment2 = (OpenPOP.MIMEParser.Attachment)msg.Attachments[0];
                        message = message + attachment2.DecodeAsText();
                    }
                    else
                    {
                        message = message + msg.MessageBody[msg.MessageBody.Count - 1];
                    }
                    filename = dirname + "\\mail.txt";

                    /*fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Read);
                     * bw = new BinaryWriter(fs);
                     * bw.Write(message);
                     * bw.Flush();
                     * bw.Close();
                     * fs.Close();*/
                    File.WriteAllText(filename, message);
                    popclient.DeleteMessage(i);
                    //popclient.DeleteAllMessages();
                    // MessageBox.Show(message);
                }
                popclient.Disconnect();
                //pop3.Disconnect();
                //fs = new FileStream("E:\\redirector\\count.txt", FileMode.Open, FileAccess.Write, FileShare.Read);
                //bw = new BinaryWriter(fs);
                count = count + newMessages;
                string ncount = "" + count;
                lock (this)
                {
                    File.WriteAllText("E:\\redirector\\count.txt", ncount);
                }
                //bw.Write((int)count);
                //bw.Flush();
                //bw.Close();
                //fs.Close();
                isReceiverAsleep = true;

                /*if (closeForm)
                 *  break;*/
                Thread.Sleep(10000);

                //if (closeForm)
                //  break;
            }
            receiverDone = true;
        }