coreb
asked on
C# attaching a zip file inside a MIME Soap File
I have an issue that is just puzzling me. I need to attach a zip file in a MIME Soap file. It will be uploaded using a standard browser upload tool. I'm just not figuring this one out.
I've attached what I need to accomplish. I know how to create the soap xml it's not bad but how do i add the MIME parts and the zip file.
Thanks so much.
I've attached what I need to accomplish. I know how to create the soap xml it's not bad but how do i add the MIME parts and the zip file.
Thanks so much.
--MIMEBoundary
Content-Type: application/octet-steam
Content-Transfer-Encoding: Binary
Content-Location: SubmissionZip
... (attachment ZIP file containing submissions goes here)
--MIMEBoundary--
ASKER
Thanks so much for the reply. It makes perfect sense. Only issue is that my documentation states.
The Content-Transfer-Encoding header for the MIME part that contains the ZIP file must be:
Content-Transfer-Encoding: Binary
The Content-Transfer-Encoding header for the MIME part that contains the ZIP file must be:
Content-Transfer-Encoding:
Well, like I said - not really an expert on the subject...my experience with MIME is as it pertains to SMTP & Internet mail, and generally mail servers will only accept text data (though there is a BINARY MIME extension that introduces a BDAT verb to replace the DATA verb) - hence the need to Base64-encode binary data into a string of ASCII text.
I was under the impression SOAP stuff was also strictly a text-based service - don't think you can have raw bytes in the middle of an XML document without some kind of encoding. Maybe it means to base64 encode the binary data and use the Binary header?
Though, to implement binary encoding is easy - it just means attach the bytes as they are, un-changed. So, in C#:
Would give you:
I was under the impression SOAP stuff was also strictly a text-based service - don't think you can have raw bytes in the middle of an XML document without some kind of encoding. Maybe it means to base64 encode the binary data and use the Binary header?
Though, to implement binary encoding is easy - it just means attach the bytes as they are, un-changed. So, in C#:
byte[] mimeMessage = CreateZipMimeMessage("C:\\test.zip");
static byte[] CreateZipMimeMessage(string zipFile)
{
// It's your responsability to make sure the boundary string
// is unique, and doesn't appear anywhere in the data itself
string boundary = "MIMEBoundary";
StringBuilder startText = new StringBuilder();
StringBuilder endText = new StringBuilder();
startText.AppendLine("MIME-Version: 1.0");
startText.AppendFormat("Content-Type: multipart/mixed; boundary=\"{0}\"\r\n\r\n", boundary);
startText.AppendLine("This is a BINARY MIME compliant message.");
startText.AppendFormat("--{0}", boundary);
startText.AppendLine("Content-Type: application/octet-stream");
startText.AppendLine("Content-Location: SubmissionZip");
startText.AppendLine("Content-Transfer-Encoding: binary");
startText.AppendFormat("Content-Disposition: attachment; filename=\"{0}\"\r\n\r\n", Path.GetFileName(zipFile));
byte[] zipBytes = File.ReadAllBytes(zipFile);
endText.AppendFormat("--{0}--\r\n", boundary);
List<byte> returnValue = new List<byte>();
returnValue.AddRange(Encoding.ASCII.GetBytes(startText.ToString()));
returnValue.AddRange(zipBytes);
returnValue.AddRange(Encoding.ASCII.GetBytes(endText.ToString()));
return returnValue.ToArray();
}
Would give you:
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="MIMEBoundary"
This is a BINARY MIME compliant message.
--MIMEBoundary
Content-Type: application/octet-stream
Content-Location: SubmissionZip
Content-Transfer-Encoding: binary
Content-Disposition: attachment; filename="test.zip"
<non-text binary data>
--MIMEBoundary--
ASKER
You have been so very helpful. One last question how do I print out the mimeMessage or save it to a file? This whole area is new and there doesn't seem to much I've found searching.
Thanks again for your help.
Thanks again for your help.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks so much for all your help. You helped me through this.
You're probably going to want to change your transfer encoding to base64, and then use Convert.ToBase64String() to convert the bytes of your zip file to a base64-encoded string.
Then your message should look like:
Open in new window