简体   繁体   中英

Attach ZIP-File to an email, without Filesystem

I am trying to zip XML files and then attach them to an email.

Neither the XML files nor the zip file should be on the file system during this process, only in the memory. The ZIP file should only exist in the attachment of the email.

I have already searched for solutions, but it is difficult for me to understand it, because I am relatively new in programming. I am not even sure, if it is possible.

These are the steps i currently use:

//Creating the XML-Files
XDocument xmlFileOne = new XDocument();
XDocument xmlFileTwo = new XDocument();

xmlFileOne = new XDocument(
    new XElement("Info",
        new XElement("Test-Nr.", "1")));

xmlFileTwo = new XDocument(
    new XElement("Info",
        new XElement("Test-Nr.", "2")));

xmlFileOne.Save(@"C:\Users\Marc\Test\xmlFileOne.xml");
xmlFileTwo.Save(@"C:\Users\Marc\Test\xmlFileTwo.xml");
//Zipping the XML-Files and saving the Zip-File
using (Ionic.Zip.ZipFile zipFile = new ZipFile())
{
    string fileName = @"C:\Users\TestUser\Test\zippedXML.zip";
    //zipFile.Password = password;
    zipFile.AddDirectory(@"C:\Users\TestUser\Test\");

    zipFile.Save(fileName);
}
//Adding the Zip-File as Attachment on the Email
//Email works with MAPI
SendEmail sendEmail = new SendEmail();
sendEmail.AddRecipientTo("test123@gmail.com");
sendEmail.AddAttachment(@"C:\Users\TestUser\Test\zippedXML.zip");
sendEmail.SendMailPopup("Subject of Email", "Body of Email");

There is no way to do that using Simple MAPI (your current implementation).

Instead, you may consider using Extended MAPI which allows dealing with a binary representation of your attachment and set it at runtime on the fly. You are interested in the PR_ATTACH_DATA_BIN property which contains binary attachment data typically accessed through the Object Linking and Embedding (OLE) IStream interface. Note, you may consider using third-party components (wrappers around this API) such as Redemption if you don't want to deal with a low-level code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM