/// <summary> /// save a decoded attachment to file /// </summary> /// <param name="attachment">decoded attachment</param> /// <returns>true is succeded, vice versa</returns> public bool SaveAttachment(TNEFAttachment attachment) { try { string strOutFile = BasePath + attachment.FileName; if (File.Exists(strOutFile)) { File.Delete(strOutFile); } FileStream fsData = new FileStream(strOutFile, FileMode.CreateNew, FileAccess.Write); fsData.Write(attachment.FileContent, 0, (int)attachment.FileLength); fsData.Close(); return(true); } catch (Exception e) { Utility.LogError("SaveAttachment():" + e.Message); return(false); } }
private void decode_attachment() { byte[] buf = new byte[4096]; int d; int len; int i, chunk; d = geti32(); switch (d) { case ASUBJECT: len = geti32(); StreamReadBytes(buf, len); byte[] _subjectBuffer = new byte[len - 1]; Array.Copy(buf, _subjectBuffer, (long)len - 1); strSubject = Encoding.Default.GetString(_subjectBuffer); PrintResult("Found subject: {0}", strSubject); geti16(); /* checksum */ break; case AFILENAME: len = geti32(); StreamReadBytes(buf, len); //PrintResult("File-Name: {0}\n", buf); byte[] _fileNameBuffer = new byte[len - 1]; Array.Copy(buf, _fileNameBuffer, (long)len - 1); if (_fileNameBuffer == null) { _fileNameBuffer = Encoding.Default.GetBytes("tnef.dat"); } string strFileName = Encoding.Default.GetString(_fileNameBuffer); PrintResult("{0}: WRITING {1}\n", BasePath, strFileName); //new attachment found because attachment data goes before attachment name _attachment.FileName = strFileName; _attachment.Subject = strSubject; _attachments.Add(_attachment.FileName, _attachment); geti16(); /* checksum */ break; case ATTACHDATA: len = geti32(); PrintResult("ATTACH-DATA: {0} bytes\n", len); _attachment = new TNEFAttachment(); _attachment.FileContent = new byte[len]; _attachment.FileLength = len; for (i = 0; i < len;) { chunk = len - i; if (chunk > buf.Length) { chunk = buf.Length; } StreamReadBytes(buf, chunk); Array.Copy(buf, 0, _attachment.FileContent, i, chunk); i += chunk; } geti16(); /* checksum */ break; default: decode_attribute(d); break; } }
private void decode_attachment() { byte[] buf=new byte[4096]; int d; int len; int i,chunk; d = geti32(); switch (d) { case ASUBJECT: len = geti32(); StreamReadBytes(buf,len); byte[] _subjectBuffer=new byte[len-1]; Array.Copy(buf,_subjectBuffer,(long)len-1); strSubject=Encoding.Default.GetString(_subjectBuffer); PrintResult("Found subject: {0}", strSubject); geti16(); /* checksum */ break; case AFILENAME: len = geti32(); StreamReadBytes(buf,len); //PrintResult("File-Name: {0}\n", buf); byte[] _fileNameBuffer=new byte[len-1]; Array.Copy(buf,_fileNameBuffer,(long)len-1); if (_fileNameBuffer == null) _fileNameBuffer = Encoding.Default.GetBytes("tnef.dat"); string strFileName=Encoding.Default.GetString(_fileNameBuffer); PrintResult("{0}: WRITING {1}\n", BasePath, strFileName); //new attachment found because attachment data goes before attachment name _attachment.FileName=strFileName; _attachment.Subject=strSubject; _attachments.Add(_attachment.FileName,_attachment); geti16(); /* checksum */ break; case ATTACHDATA: len = geti32(); PrintResult("ATTACH-DATA: {0} bytes\n", len); _attachment=new TNEFAttachment(); _attachment.FileContent=new byte[len]; _attachment.FileLength=len; for (i = 0; i < len; ) { chunk = len-i; if (chunk > buf.Length) chunk = buf.Length; StreamReadBytes(buf,chunk); Array.Copy(buf,0,_attachment.FileContent,i,chunk); i += chunk; } geti16(); /* checksum */ break; default: decode_attribute(d); break; } }
/// <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++; } }
/// <summary> /// save a decoded attachment to file /// </summary> /// <param name="attachment">decoded attachment</param> /// <returns>true is succeded, vice versa</returns> public bool SaveAttachment(TNEFAttachment attachment) { try { string strOutFile=BasePath+attachment.FileName; if(File.Exists(strOutFile)) File.Delete(strOutFile); FileStream fsData=new FileStream(strOutFile,FileMode.CreateNew,FileAccess.Write); fsData.Write(attachment.FileContent,0,(int)attachment.FileLength); fsData.Close(); return true; } catch(Exception e) { Utility.LogError("SaveAttachment():"+e.Message); return false; } }