title: Adding an attachment to SOAP request
body: <p>I am at a loose end as to how to add an attachment in my SOAP request. We have to consume a thrid party web service, built in java, which is the most convoluted thing I have ever come across. Any other web services we have used, that required attachments, have a method or property to add the attachment. Simple. However, this one provides no such method.</p>
  
  <p>We have got a version of the SOAP message together that is exactly as we want the XML, however it is the MIME part of the file that we cannot add.</p>
  
  <p>Example:</p>
  
  <pre><code>&lt;soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"&gt;
  &lt;soap:Header&gt;
  &lt;payloadManifest xmlns="http://&lt;examplePayload&gt;"&gt;
  &lt;manifest contentID="Content0" namespaceURI="http://&lt;exampleManifest&gt;" element="ProcessRepairOrder" version="2.01" /&gt;
  &lt;/payloadManifest&gt;
  &lt;wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"&gt;
  &lt;wsu:Timestamp xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"&gt;
  &lt;wsu:Created&gt;2011-12-19T15:25:13Z&lt;/wsu:Created&gt;
  &lt;wsu:Expires&gt;2011-12-19T15:30:00Z&lt;/wsu:Expires&gt;
  &lt;/wsu:Timestamp&gt;
  &lt;wsse:UsernameToken&gt;&lt;wsse:Username&gt;username&lt;/wsse:Username&gt;&lt;wsse:Password&gt;password&lt;/wsse:Password&gt;&lt;/wsse:UsernameToken&gt;&lt;/wsse:Security&gt;&lt;/soap:Header&gt;&lt;soap:Body&gt;&lt;ProcessMessage xmlns="&lt;examplePayload"&gt;&lt;payload&gt;&lt;content id="Content0"&gt;
  
  &lt;s:ProcessRepairOrder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://example.xsd" xmlns:s="http://&lt;exampleManifest&gt;" xmlns:gwm="http://example"&gt;
      &lt;s:ApplicationArea&gt;
          &lt;s:Sender&gt;
              &lt;s:Component&gt;Test&lt;/s:Component&gt;
              &lt;s:Task&gt;ProcessAttachment&lt;/s:Task&gt;
              &lt;s:CreatorNameCode&gt;Test&lt;/s:CreatorNameCode&gt;
              &lt;s:SenderNameCode&gt;XX&lt;/s:SenderNameCode&gt;
              &lt;s:DealerNumber&gt;111111&lt;/s:DealerNumber&gt;
              &lt;s:DealerCountry&gt;GB&lt;/s:DealerCountry&gt;
          &lt;/s:Sender&gt;
          &lt;s:CreationDateTime&gt;2010-03-26T13:37:05Z&lt;/s:CreationDateTime&gt;
          &lt;s:Destination&gt;
              &lt;s:DestinationNameCode&gt;GM&lt;/s:DestinationNameCode&gt;
              &lt;s:DestinationURI/&gt;
              &lt;s:DestinationSoftwareCode&gt;GWM&lt;/s:DestinationSoftwareCode&gt;
          &lt;/s:Destination&gt;
      &lt;/s:ApplicationArea&gt;
      &lt;s:DataArea xsi:type="gwm:DataAreaExtended"&gt;
          &lt;s:Process/&gt;
          &lt;s:RepairOrder&gt;
              &lt;s:Header xsi:type="gwm:RepairOrderHeaderExtended"&gt;
                  &lt;s:DocumentId/&gt;
              &lt;/s:Header&gt;
              &lt;s:Job xsi:type="gwm:JobExtended"&gt;
                  &lt;s:JobNumber/&gt;
                  &lt;s:OperationId&gt;Test&lt;/s:OperationId&gt;
                  &lt;s:OperationName/&gt;
                  &lt;s:CodesAndComments/&gt;
                  &lt;s:Diagnostics/&gt;
                  &lt;s:WarrantyClaim xsi:type="gwm:WarrantyClaimExtended"&gt;
                      &lt;s:OEMClaimNumber&gt;00112233445566778899&lt;/s:OEMClaimNumber&gt;
                      &lt;gwm:Attachment&gt;
                          &lt;gwm:File&gt;&lt;xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:test.gif"/&gt;&lt;/gwm:File&gt;
                          &lt;gwm:Filename&gt;test.gif&lt;/gwm:Filename&gt;
                      &lt;/gwm:Attachment&gt;
                  &lt;/s:WarrantyClaim&gt;
                  &lt;s:LaborActualHours&gt;0.0&lt;/s:LaborActualHours&gt;
                  &lt;s:Technician/&gt;
              &lt;/s:Job&gt;
          &lt;/s:RepairOrder&gt;
      &lt;/s:DataArea&gt;
  &lt;/s:ProcessRepairOrder&gt;
  &lt;/content&gt;&lt;/payload&gt;&lt;/ProcessMessage&gt;&lt;/soap:Body&gt;&lt;/soap:Envelope&gt;
  </code></pre>
  
  <p>This is the XML part that we can generate and send off, however it is incorrect as we need a MIME part in there like:</p>
  
  <p>Before XML:</p>
  
  <pre><code>--MIMEBoundary
  Content-Type: application/xop+xml; charset=utf-8; type="text/xml"
  Content-Transfer-Encoding: binary
  Content-ID: &lt;rootpart&gt;
  </code></pre>
  
  <p>After XML</p>
  
  <pre><code>--MIMEBoundary
  Content-Type: image/gif; name=test.gif
  Content-Transfer-Encoding: binary
  Content-ID: &lt;test.gif&gt;
  GIF89a@�
  
  --MIMEBoundary--
  </code></pre>
  
  <p>I have scoured the internet for answers but have come up blank. There doesn't seem to be much documentation around using WSE for this. I must stress that WSE is a requirement on the server side, and there is no way I can change the technology to address this issue.</p>
  
  <p>Is there a way that these MIME sections can be added?</p>
  
  <p>EDIT: I must add that I can get a working XML document sent through SoapUI with attachments, but cannot seem to find a way within our code.</p>
  
  <p>I have added a bounty to try and get a solution to this problem. If anyone has any other ideas please let me know.</p>
  
  <p>EDIT again: I know it has been a week since I was able to check the responses here, but while some give a good idea where to look I am still drawing a blank. The terrible documentation surrounding <code>XopDocument</code> and its methods is a big sticking point, if anyone has any examples of using <code>SaveToXopPackage</code> could they please provide because this is beginning to grate!</p>