Outlook Mailitem作为C#中的Zip附件

时间:2019-02-14 08:24:58

标签: c# outlook ziparchive

我想压缩Outlook Mailitem并将其作为附件添加到新邮件中。这就是我到目前为止所得到的:

newmail.Attachments.Add(mailItem, Outlook.OlAttachmentType.olEmbeddeditem);

经过测试,到目前为止,如果我将普通电子邮件添加为附件,则可以正常工作。但是如何从MailItem创建新的ZipArchive?

1 个答案:

答案 0 :(得分:0)

尝试保存:

 String savepath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\" + filename + ".msg";
    mailItem.SaveAs(savepath);
 FileInfo mailItemToCompress= new FileInfo(savepath);

压缩:

public void Compress(FileInfo mailItemToCompress, string compressedMailItem)
{
    using (FileStream originalFileStream = mailItemToCompress.OpenRead())
    {
       using (FileStream compressedMailItem= File.Create(compressedMailItem)
            {
                using (GZipStream compressionStream = new GZipStream(compressedFileStream, CompressionMode.Compress))
                {
                    originalFileStream.CopyTo(compressionStream);
                }
            }
    }
}

然后附加您的mailItem:

mailItem.Attachments.Add(compressedMailItem);