# javax.xml.bind.attachment

***

**1. Generating SOAP Messages with Attachments**

```java
import javax.xml.bind.annotation.*;
import javax.xml.bind.attachment.*;

@XmlRootElement(name = "Mail")
public class Mail {
    private String from;
    private String to;
    @XmlAttachmentRef
    private Attachment attachment;
}
```

**2. Consuming SOAP Messages with Attachments**

```java
AttachmentPart part = attachment.getAttachmentParts().get(0);
part.getDataHandler().getInputStream(); // Read attachment content
```

**3. Sending Attachments with SOAP Web Services**

```java
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.bind.annotation.*;
import javax.xml.bind.attachment.*;

@WebService
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
public class AttachmentWebService {
    public void sendAttachment(@Param("mail") Mail mail) {
        // Save attachment
    }
}
```

**4. Receiving Attachments with SOAP Web Services**

```java
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.bind.annotation.*;
import javax.xml.bind.attachment.*;

@WebService
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE)
public class AttachmentWebService {
    @WebMethod(operationName = "getAttachment")
    public Mail getAttachment() {
        // Prepare mail object with attachment
        return mail;
    }
}
```

**5. Using @XmlAttachmentRef Annotation**

```java
@XmlAttachmentRef(value = "/myAttachment")
private Attachment attachment;
```

**6. Using AttachmentMarshaller**

```java
AttachmentMarshaller marshaller = ctx.createAttachmentMarshaller();
marshaller.marshall(attachment);
```

**7. Using AttachmentUnmarshaller**

```java
AttachmentUnmarshaller unmarshaller = ctx.createAttachmentUnmarshaller();
Attachment attachment = unmarshaller.unmarshal(part);
```

**8. Creating Attachment**

```java
AttachmentBuilder builder = ctx.createAttachmentBuilder();
builder.addContent(DataHandler);
Attachment attachment = builder.build();
```

**9. Using MTOM Policy**

```xml
<soap:binding use="literal" transport="http://schemas.xmlsoap.org/soap/http" />

<soap12:policy>
  <soap12:messageTransmissionOptimization>
    <soap12:enabled>true</soap12:enabled>
  </soap12:messageTransmissionOptimization>
</soap12:policy>
```

**10. Using SOAP with Attachments (MTOM)**

```java
SOAPConnectionFactory factory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = factory.createConnection();
SOAPMessage request = MessageFactory.newInstance().createMessage();
```

**11. Using SOAP with Attachments (SWA)**

```java
SOAPConnectionFactory factory = SOAPConnectionFactory.newInstance();
SOAPConnection connection = factory.createConnection();
SOAPMessage request = MessageFactory.newInstance().createMessage();
```

**12. Using Attachment Type**

```java
public enum AttachmentType {
    TEXT_PLAIN,
    TEXT_HTML,
    IMAGE_JPG,
    IMAGE_PNG
}
```

**13. Using MimeHeaders**

```java
import java.util.HashMap;
import java.util.Map;

public class MimeHeaders {
    private Map<String, String> headers = new HashMap<>();
}
```

**14. Using DataHandler**

```java
import javax.activation.DataHandler;

public class DataHandler {
    private Object content;
    private MimeType contentType;
}
```

**15. Using MimeType**

```java
import javax.activation.MimeType;

public class MimeType {
    private String type;
    private String subtype;
}
```

**16. Using JAXBContext**

```java
JAXBContext context = JAXBContext.newInstance(Mail.class);
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(mail, System.out);
```

**17. Using Unmarshaller**

```java
JAXBContext context = JAXBContext.newInstance(Mail.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Mail mail = unmarshaller.unmarshal(inputStream);
```

**18. Using @XmlRootElement Annotation**

```java
@XmlRootElement(name = "Mail", namespace = "http://example.org")
public class Mail {
    private String from;
    private String to;
}
```

**19. Using @XmlElement Annotation**

```java
@XmlElement(name = "from")
public String getFrom() {
    return from;
}

@XmlElement(name = "to")
public String getTo() {
    return to;
}
```

**20. Using @XmlAttribute Annotation**

```java
@XmlAttribute(name = "id")
private String id;
```

**21. Using @XmlTransient Annotation**

```java
@XmlTransient
private Attachment attachment;
```

**22. Using @XmlAccessorOrder Annotation**

```java
@XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL)
public class Mail {
    private String from;
    private String to;
}
```

**23. Using @XmlElementRef Annotation**

```java
@XmlElementRef
private Attachment attachment;
```

**24. Using @XmlElementWrapper Annotation**

```java
@XmlElementWrapper(name = "attachments")
private Attachment[] attachments;
```

**25. Using @XmlAnyElement Annotation**

```java
@XmlAnyElement
private List<Element> anyElement;
```

**26. Using @XmlSchema** Annotation\*\*

```java
@XmlSchema(elementFormDefault = XmlNsForm.QUALIFIED)
public class AttachmentWebService {
    // ...
}
```

**27. Using @XmlJavaTypeAdapter** Annotation\*\*

```java
@XmlJavaTypeAdapter(MailAdapter.class)
private Mail mail;
```

**28. Using @XmlInlineBinaryData** Annotation\*\*

```java
@XmlInlineBinaryData
private byte[] data;
```

**29. Using @XmlID Annotation**

```java
@XmlID
private String id;
```

**30. Using @XmlIDREF Annotation**

```java
@XmlIDREF
private Attachment attachment;
```

**31. Using @XmlList Annotation**

```java
@XmlList
private List<String> values;
```

**32. Using @XmlName Annotation**

```java
@XmlName("mail")
public class Mail {
    // ...
}
```

**33. Using @XmlNamespace Annotation**

```java
@XmlNamespace(prefix = "mail", namespaceURI = "http://example.org")
public class Mail {
    // ...
}
```

**34. Using @XmlPath Annotation**

```java
@XmlPath("attachments/attachment[1]")
private Attachment firstAttachment;
```

**35. Using @XmlProperty Annotation**

```java
@XmlProperty(name = "mail")
public Mail getMail() {
    return mail;
}
```

**36. Using @XmlRegistry Annotation**

```java
@XmlRegistry
public class ObjectFactory {
    // ...
}
```

**37. Using @XmlRootElementWrapper Annotation**

```java
@XmlRootElementWrapper(name = "mails")
private List<Mail> mails;
```

**38. Using @XmlSchemaType Annotation**

```java
@XmlSchemaType(name = "email")
private String email;
```

**39. Using @XmlSeeAlso Annotation**

```java
@XmlSeeAlso({Mail.class, Attachment.class})
public class MailWebService {
    // ...
}
```

**40. Using @XmlEnumValue Annotation**

```java
public enum Status {
    @XmlEnumValue("new")
    NEW,
    @XmlEnumValue("in_progress")
    IN_PROGRESS,
    @XmlEnumValue("completed")
    COMPLETED
}
```

**41. Using @XmlMixed Annotation**

```java
@XmlMixed
private List<String> values;
```

**42. Using @XmlTransient Annotation**

```java
@XmlTransient
private Attachment attachment;
```

**43. Using @XmlValue Annotation**

```java
@XmlValue
private String value;
```

**44. Using @XmlWrapperType Annotation**

```java
@XmlWrapperType(name = "attachments")
private Attachment[] attachments;
```

**45. Using @XmlAnyElement Annotation**

```java
@XmlAnyElement
private List<Element> anyElement;
```

**46. Using @XmlAttachmentRef Annotation**

```java
@XmlAttachmentRef
private Attachment attachment;
```

**47. Using @XmlElement Annotation**

```java
@XmlElement(name = "from")
public String getFrom() {
    return from;
}
```

**48. Using @XmlElements Annotation**

```java
@XmlElements({
    @XmlElement(name = "attachment", type = Attachment.class),
    @XmlElement(name = "mail", type = Mail.class)
})
private List<Object> objects;
```

**49. Using @XmlElementWrapper Annotation**

```java
@XmlElementWrapper(name = "attachments")
private List<Attachment> attachments;
```

**50. Using @XmlRootElement Annotation**

```java
@XmlRootElement(name = "Mail")
public class Mail {
    // ...
}
```
