# javax.activation

***

**1. Get MIME Type for a File**

```java
import javax.activation.*;

public class GetMimeType {

    public static void main(String[] args) {
        try {
            // Create an ActivationDataFlavor with the file's MIME type
            ActivationDataFlavor flavor = new ActivationDataFlavor(MimetypesFileTypeMap.getDefaultFileTypeMap());

            // Get the file's MIME type from the data flavor
            String mimeType = flavor.getMimeType();

            System.out.println("MIME type: " + mimeType);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
```

**2. Get MIME Type for a File Extension**

```java
import javax.activation.*;

public class GetMimeTypeByExtension {

    public static void main(String[] args) {
        try {
            // Get the MIME type for the given file extension
            String mimeType = MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType("file.ext");

            System.out.println("MIME type: " + mimeType);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
```

**3. Set MIME Type for a File**

```java
import javax.activation.*;

public class SetMimeType {

    public static void main(String[] args) {
        try {
            // Create an ActivationDataFlavor with the desired MIME type
            ActivationDataFlavor flavor = new ActivationDataFlavor("text/plain", "Plain Text");

            // Set the MIME type for the given file
            MimetypesFileTypeMap.getDefaultFileTypeMap().addMimeTypes("file.ext," + flavor.getMimeType());

            System.out.println("MIME type set successfully.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
```

**4. Create a MimeMultipart**

```java
import javax.activation.*;

public class CreateMimeMultipart {

    public static void main(String[] args) {
        try {
            // Create a new MimeMultipart object
            MimeMultipart multipart = new MimeMultipart();

            // Add a body part to the multipart object
            BodyPart bodyPart = new MimeBodyPart();
            bodyPart.setText("This is the body of the message.");
            multipart.addBodyPart(bodyPart);

            // Get the content type of the multipart object
            String contentType = multipart.getContentType();

            System.out.println("Content type: " + contentType);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
```

**5. Add an Attachment to a MimeMultipart**

```java
import javax.activation.*;

public class AddAttachmentToMimeMultipart {

    public static void main(String[] args) {
        try {
            // Create a new MimeMultipart object
            MimeMultipart multipart = new MimeMultipart();

            // Add a body part to the multipart object
            BodyPart bodyPart = new MimeBodyPart();
            bodyPart.setText("This is the body of the message.");
            multipart.addBodyPart(bodyPart);

            // Add an attachment to the multipart object
            DataSource attachment = new FileDataSource("attachment.txt");
            BodyPart attachmentPart = new MimeBodyPart();
            attachmentPart.setDataHandler(new DataHandler(attachment));
            attachmentPart.setFileName("attachment.txt");
            multipart.addBodyPart(attachmentPart);

            // Get the content type of the multipart object
            String contentType = multipart.getContentType();

            System.out.println("Content type: " + contentType);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
```

**6. Get Attachment From MimeMultipart**

```java
import javax.activation.*;

public class GetAttachmentFromMimeMultipart {

    public static void main(String[] args) {
        try {
            // Create a new MimeMultipart object
            MimeMultipart multipart = new MimeMultipart();

            // Add a body part to the multipart object
            BodyPart bodyPart = new MimeBodyPart();
            bodyPart.setText("This is the body of the message.");
            multipart.addBodyPart(bodyPart);

            // Add an attachment to the multipart object
            DataSource attachment = new FileDataSource("attachment.txt");
            BodyPart attachmentPart = new MimeBodyPart();
            attachmentPart.setDataHandler(new DataHandler(attachment));
            attachmentPart.setFileName("attachment.txt");
            multipart.addBodyPart(attachmentPart);

            // Get the attachment from the multipart object
            BodyPart attachmentBodyPart = multipart.getBodyPart(1);
            DataSource attachmentDataSource = attachmentBodyPart.getDataHandler().getDataSource();

            System.out.println("Attachment file name: " + attachmentDataSource.getName());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
```

**7. Get Content-Type of a MimeMessage**

```java
import javax.activation.*;

public class GetContentType {

    public static void main(String[] args) {
        try {
            // Create a new MimeMessage object
            MimeMessage message = new MimeMessage(null);

            // Set the content type of the message
            message.setContent("This is the body of the message.", "text/plain");

            // Get the content type of the message
            String contentType = message.getContentType();

            System.out.println("Content type: " + contentType);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
```

**8. Set Content-Type of a MimeMessage**

```java
import javax.activation.*;

public class SetContentType {

    public static void main(String[] args) {
        try {
            // Create a new MimeMessage object
            MimeMessage message = new MimeMessage(null);

            // Set the content type of the message
            message.setContent("This is the body of the message.", "text/html");

            // Get the content type of the message
            String contentType = message.getContentType();

            System.out.println("Content type: " + contentType);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
```

**9. Get Content-Disposition of a MimeBodyPart**

```java
import javax.activation.*;

public class GetContentDisposition {

    public static void main(String[] args) {
        try {
            // Create a new MimeBodyPart object
            MimeBodyPart bodyPart = new MimeBodyPart();

            // Set the content disposition of the body part
            bodyPart.setDisposition(Part.ATTACHMENT);

            // Get the content disposition of the body part
            String contentDisposition = bodyPart.getDisposition();

            System.out.println("Content disposition: " + contentDisposition);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
```

**10. Set Content-Disposition of a MimeBodyPart**

```java
import javax.activation.*;

public class SetContentDisposition {

    public static void main(String[] args) {
        try {
            // Create a new MimeBodyPart object
            MimeBodyPart bodyPart = new MimeBodyPart();

            // Set the content disposition of the body part
            bodyPart.setDisposition(Part.INLINE);

            // Get the content disposition of the body part
            String contentDisposition = bodyPart.getDisposition();

            System.out.println("Content disposition: " + contentDisposition);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
```

**11. Create a DataHandler**

```java
import javax.activation.*;

public class CreateDataHandler {

    public static void main(String[] args) {
        try {
            // Create a new DataHandler object
            DataHandler dataHandler = new DataHandler(new FileDataSource("file.txt"));

            // Get the content type of the data handler
            String contentType = dataHandler.getContentType();

            System.out.println("Content type: " + contentType);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
```

**12. Get Content-Type of a DataHandler**

```java
import javax.activation.*;

public class GetContentType {

    public static void main(String[] args) {
        try {
            // Create a new DataHandler object
            DataHandler dataHandler = new DataHandler(new FileDataSource("file.txt"));

            // Get the content type of the data handler
            String contentType = dataHandler.getContentType();

            System.out.println("Content type: " + contentType);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
```

**13. Get Data Source from a DataHandler**

```java
import javax.activation.*;

public class GetDataSource {

    public static void main(String[] args) {
        try {
            // Create a new DataHandler object
            DataHandler dataHandler = new DataHandler(new FileDataSource("file.txt"));

            // Get the data source from the data handler
            DataSource dataSource = dataHandler.getDataSource();

            System.out.println("Data source: " + dataSource.getName());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
```

**14. Create a FileDataSource**

```java
import javax.activation.*;

public class CreateFileDataSource {

    public static void main(String[] args) {
        try {
            // Create a new FileDataSource object
            FileDataSource fileDataSource = new FileDataSource("file.txt");

            // Get the content type of the data source
            String contentType = fileDataSource.getContentType();

            System.out.println("Content type: " + contentType);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
```

**15. Get Content-Type of a FileDataSource**

```java
import javax.activation.*;

public class GetContentType {

    public static void main(String[] args) {
        try {
            // Create a new FileDataSource object
            FileDataSource fileDataSource = new FileDataSource("file.txt");

            // Get the content type of the data source
            String contentType = fileDataSource.getContentType();

            System.out.println("Content type: " + contentType);

```
