title: Create a SOAP message that contains an attachment in java
body: <p>My goal is to create a soap client that attaches a pdf file to the request.</p>
  
  <p>Steps:</p>
  
  <ol>
  <li>I created a soap message.</li>
  <li>I added content to SOAPPart.</li>
  <li>I've attached an attachment to the message.</li>
  <li>I sent the message.</li>
  </ol>
  
  <p>The SoapUI (5.5.0) mock returns the following error:</p>
  
  <blockquote>
    <p>ERROR:An error occurred [Missing operation for soapAction [<a href="http://.../..]" rel="nofollow noreferrer">http://.../..]</a> and body element [null] with SOAP Version [SOAP 1.1]]</p>
  </blockquote>
  
  <p>The code is very simple. This is what I put together: <a href="http://java.boot.by/wsd-guide/ch02s04.html" rel="nofollow noreferrer">http://java.boot.by/wsd-guide/ch02s04.html</a></p>
  
  <p>I compared a request I sent and a request from SoapUi. I found a difference with "content-type" in the start section.</p>
  
  <p>My client: </p>
  
  <blockquote>
    <p>Content-Type: multipart/related; boundary="----=_Part_1_611437735.1568631410174"; type="text/xml"</p>
  </blockquote>
  
  <p>SoapUi:</p>
  
  <blockquote>
    <p>Content-Type: multipart/related; type="text/xml"; start=&quot;&lt;rootpart@soapui.org&gt;&quot;; boundary="----=_Part_26_15392417.1568631333842"</p>
  </blockquote>
  
  <p>I tried to add it manually but it will be removed when saving the SAAJ message.</p>
  
  <pre><code>String xml;
          InputStream is;
          File file = new File(filename);
          String id = String.valueOf(System.currentTimeMillis());
          xml =   "&lt;soapenv:Envelope ................................."&gt;\n" +
                  "   &lt;soapenv:Header/&gt;\n" +
                  "   &lt;soapenv:Body&gt;\n" +
  .......................
                  "   &lt;/soapenv:Body&gt;\n" +
                  "&lt;/soapenv:Envelope&gt;\n";
  
          is = new ByteArrayInputStream(xml.getBytes("UTF-8"));
  
          SOAPConnectionFactory factory = SOAPConnectionFactory.newInstance();
          SOAPConnection connection = factory.createConnection();
          MessageFactory messageFactory = MessageFactory.newInstance();
          SOAPMessage message = messageFactory.createMessage();
  
          SOAPPart soapPart = message.getSOAPPart();
          DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
          DocumentBuilder builder = dbFactory.newDocumentBuilder();
          Document document = builder.parse(is);
          DOMSource domSource = new DOMSource(document);
          soapPart.setContent(domSource);
          soapPart.setContentId("&lt;root.message@cxf.apache.org&gt;");
  
  
          URL url = new URL("https://../.");
          DataHandler handler = new DataHandler(url);
          AttachmentPart attachPart = message.createAttachmentPart(handler);
          attachPart.setContentId("&lt;root.message@cxf.apache.org&gt;");
          message.addAttachmentPart(attachPart);
  
          message.getMimeHeaders().addHeader("Content-Type", "multipart/related; type=\"text/xml\"; start=\"&lt;rootpart@soapui.org&gt;\"");
          message.getMimeHeaders().setHeader("SOAPAction", "\"http://..../...\"");
          SOAPMessage response = connection.call(message, endpoint);
  </code></pre>