/// <summary> /// save a decoded attachment to file /// </summary> /// <param name="attachment">decoded attachment</param> /// <param name="pathToSaveTo">Where to save the attachment to</param> /// <param name="logger">The object to use for logging output</param> /// <returns><see langword="true"/> if succeeded, <see langword="false"/> otherwise</returns> /// <exception cref="ArgumentNullException">If <paramref name="attachment"/>, <paramref name="pathToSaveTo"/> or <paramref name="logger"/> is <see langword="null"/></exception> /// <exception cref="ArgumentException">If <paramref name="pathToSaveTo"/> does not exist</exception> public static bool SaveAttachment(WinMailAttachment attachment, DirectoryInfo pathToSaveTo, ILogger logger) { if (attachment == null) { throw new ArgumentNullException("attachment"); } if (pathToSaveTo == null) { throw new ArgumentNullException("pathToSaveTo"); } if (logger == null) { throw new ArgumentNullException("logger"); } if (!pathToSaveTo.Exists) { throw new ArgumentException("The path " + pathToSaveTo.FullName + " does not exist"); } try { FileInfo outFile = new FileInfo(Path.Combine(pathToSaveTo.FullName, attachment.FileName)); if (outFile.Exists) { outFile.Delete(); } using (FileStream fsData = outFile.Create()) { fsData.Write(attachment.Content, 0, (int)attachment.Length); } return(true); } catch (Exception e) { logger.LogError("SaveAttachment(): " + e.Message); return(false); } }
/// <summary> /// save a decoded attachment to file /// </summary> /// <param name="attachment">decoded attachment</param> /// <param name="pathToSaveTo">Where to save the attachment to</param> /// <returns><see langword="true"/> if succeeded, <see langword="false"/> otherwise</returns> /// <exception cref="ArgumentNullException">If <paramref name="attachment"/> or <paramref name="pathToSaveTo"/> is <see langword="null"/></exception> /// <exception cref="ArgumentException">If <paramref name="pathToSaveTo"/> does not exist</exception> public static bool SaveAttachment(WinMailAttachment attachment, DirectoryInfo pathToSaveTo) { if (attachment == null) { throw new ArgumentNullException("attachment"); } if (pathToSaveTo == null) { throw new ArgumentNullException("pathToSaveTo"); } if (!pathToSaveTo.Exists) { throw new ArgumentException("The path " + pathToSaveTo.FullName + " does not exist"); } return(SaveAttachment(attachment, pathToSaveTo, DefaultLogger.Create())); }
private void decode_attachment() { byte[] buffer = new byte[4096]; int length; int d = geti32(); switch (d) { case ATTACHREND: _attachment = new WinMailAttachment(); _attachments.Add(_attachment); length = geti32(); StreamReadBytes(buffer, length); geti16(); /* checksum */ break; case ASUBJECT: length = geti32(); StreamReadBytes(buffer, length); byte[] _subjectBuffer = new byte[length - 1]; Array.Copy(buffer, _subjectBuffer, (long)length - 1); subject = Encoding.Default.GetString(_subjectBuffer); PrintResult("Found subject: {0}", subject); geti16(); /* checksum */ break; case AFILENAME: length = geti32(); StreamReadBytes(buffer, length); //PrintResult("File-Name: {0}\n", buf); byte[] _fileNameBuffer = new byte[length - 1]; Array.Copy(buffer, _fileNameBuffer, (long)length - 1); string strFileName = Encoding.Default.GetString(_fileNameBuffer); //new attachment found because attachment data goes before attachment name _attachment.FileName = strFileName; _attachment.Subject = subject; geti16(); /* checksum */ break; case ATTACHDATA: length = geti32(); PrintResult("ATTACH-DATA: {0} bytes\n", length); _attachment.Content = new byte[length]; _attachment.Length = length; for (int i = 0; i < length;) { int chunk = length - i; if (chunk > buffer.Length) { chunk = buffer.Length; } StreamReadBytes(buffer, chunk); Array.Copy(buffer, 0, _attachment.Content, i, chunk); i += chunk; } geti16(); /* checksum */ break; default: decode_attribute(d); break; } }