Solved

C# attaching a zip file inside a MIME Soap File

Posted on 2011-03-17
6
1,892 Views
Last Modified: 2013-11-18
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.
--MIMEBoundary
Content-Type: application/octet-steam
Content-Transfer-Encoding: Binary
Content-Location: SubmissionZip
... (attachment ZIP file containing submissions goes here)
--MIMEBoundary--

Open in new window

0
Comment
Question by:coreb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35165532
Not particularly familiar with MIME or SOAP, so I probably shouldn't be chiming in here, but it's been nearly a day and no one else has responded yet so I'll give it a shot. ;)

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:
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="MIMEBoundary"

This section is ignored by mail clients, dunno about your SOAP stuff
--MIMEBoundary
Content-Type: text/plain

Hello there.
--MIMEBoundary
Content-Type: application/octet-stream
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="file.zip"

PGh0bWw+CiAgPGhlYWQ+CiAgPC9oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUg
Ym9keSBvZiB0aGUgbWVzc2FnZS48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg==
--MIMEBoundary--

Open in new window

0
 

Author Comment

by:coreb
ID: 35166217
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
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 35166486
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#:
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();
}

Open in new window


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--

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:coreb
ID: 35166628
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.
0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 500 total points
ID: 35166768
The method in my example above uses binary encoding, so the bytes of the zip file are just stuck as they are into the middle of the MIME stuff; without encoding of some sort (i.e. Base64) there's no way to represent this data, except as a series of bytes.  Therefore that method just returns a byte[] array.  You can do whatever you want with it from there - e.g. File.WriteAllBytes(CreateZipMimeMessage("C:\\test.zip"")); to write it to a file.

You won't be able to print it, show it or put it in an XML document though, because it contains binary data that can't be represented as text.


This version below is basically the same thing, except uses base64 encoding.  This method returns a string, so you can save it to a text file, send it to a printer, show it in a MessageBox, etc.
static string 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 mimeMessage = new StringBuilder();

	mimeMessage.AppendLine("MIME-Version: 1.0");
	mimeMessage.AppendFormat("Content-Type: multipart/mixed; boundary=\"{0}\"\r\n\r\n", boundary);
	mimeMessage.AppendLine("This is a BINARY MIME compliant message.");
	mimeMessage.AppendFormat("--{0}", boundary);

	mimeMessage.AppendLine("Content-Type: application/octet-stream");
	mimeMessage.AppendLine("Content-Location: SubmissionZip");
	mimeMessage.AppendLine("Content-Transfer-Encoding: base64");
	mimeMessage.AppendFormat("Content-Disposition: attachment; filename=\"{0}\"\r\n\r\n", Path.GetFileName(zipFile));

	byte[] zipFileBytes = File.ReadAllBytes(zipFile);
	string zipFileAsBase64 = Convert.ToBase64String(zipFileBytes);
	mimeMessage.AppendFormat("{0}\r\n--{1}--\r\n", zipFileAsBase64, boundary);

	return mimeMessage.ToString();
}

Open in new window

0
 

Author Closing Comment

by:coreb
ID: 35167793
Thanks so much for all your help.  You helped me through this.
0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question