ทำงานกับคุณสมบัติ MAPI

การตั้งค่าและเข้าถึงคุณสมบัติ Outlook MAPI

Aspose.Email for Java มี MapiProperty คลาสที่แสดงถึงคุณสมบัติ MAPI:

  • Name: ชื่อของคุณสมบัติ.
  • Tag: แท็กของคุณสมบัติ.
  • Data: ข้อมูลของคุณสมบัติ.

หัวข้อนี้ยังอธิบายวิธีตั้งค่าและเข้าถึงคุณสมบัติ MAPI ของไฟล์ Outlook Message (MSG) ด้วย Aspose.Email for Java. นอกจากนี้ยังมีตัวอย่างโค้ดเกี่ยวกับวิธีลบคุณสมบัติจาก MSG และไฟล์แนบ.

อ่านคุณสมบัติ

เพื่ออ่านข้อมูลของคุณสมบัติ MAPI จากไฟล์ MSG:

  1. สร้างอินสแตนซ์ของ MapiMessage คลาสเพื่อโหลดไฟล์ MSG ด้วย Load() เมธอดสเตติก.
  2. ตั้งค่าอ้างอิงไปยัง MapiMessage อ็อบเจ็กต์ getProperties() เมธอดเพื่อดึง MapiPropertyCollection.
  3. รับ MapiProperty อ็อบเจ็กต์จาก MapiPropertyCollection โดย แท็กคุณสมบัติ Mapi คีย์.
  4. รับข้อมูลคุณสมบัติด้วยเมธอด getXXX() ที่เหมาะสม.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";

// Load an MSG file from disk
MapiMessage outlookMessageFile = MapiMessage.load(dataDir + "messageMapi.msg");

// Get the MapiProperty collection
MapiPropertyCollection coll = outlookMessageFile.getProperties();

// Access the MapiPropertyTag.PR_SUBJECT property
MapiProperty prop = coll.get_Item(MapiPropertyTag.PR_SUBJECT);

// If the property is not found, check MapiPropertyTag.PR_SUBJECT_W,
// which is the Unicode peer of MapiPropertyTag.PR_SUBJECT
if (prop == null) {
    prop = coll.get_Item(MapiPropertyTag.PR_SUBJECT_W);
}

if (prop == null) {
    System.out.println("MAPI property could not be found.");
} else {
    // Get the property data as a string
    System.out.println("Subject: " + prop.getString());
}

// Read the internet code page property
prop = coll.get_Item(MapiPropertyTag.PR_INTERNET_CPID);
if (prop != null) {
    System.out.println("Code page: " + prop.getLong());
}

ตั้งค่าคุณสมบัติเพิ่มเติม

ตัวอย่างโค้ดต่อไปนี้สามารถใช้ตั้งค่าคุณสมบัติเพิ่มเติมของ Outlook MapiMessage.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
MapiMessage msg = new MapiMessage();

// A multi-valued property takes any java.lang.Iterable of boxed values.
// PT_MV_FLOAT, PT_MV_R4, mv.float
List<Object> values = new ArrayList<Object>();
values.add(1f);
values.add(2f);
msg.setProperty(new MapiProperty(0x23901004L, values));

// PT_MV_DOUBLE, PT_MV_R8
values = new ArrayList<Object>();
values.add(1d);
values.add(2d);
msg.setProperty(new MapiProperty(0x23901005L, values));

// PT_MV_APPTIME
values = new ArrayList<Object>();
values.add(30456.34);
values.add(40655.45);
msg.setProperty(new MapiProperty(0x23901007L, values));

// PT_MV_I8, PT_MV_LONGLONG
values = new ArrayList<Object>();
values.add(30456L);
values.add(40655L);
msg.setProperty(new MapiProperty(0x23901014L, values));

// PT_MV_SHORT, PT_MV_I2, mv.i2
values = new ArrayList<Object>();
values.add((short) 1);
values.add((short) 2);
msg.setProperty(new MapiProperty(0x23901002L, values));

// PT_MV_BOOLEAN
values = new ArrayList<Object>();
values.add(true);
values.add(false);
msg.setProperty(new MapiProperty(0x2390100bL, values));

// PT_NULL
msg.setProperty(new MapiProperty(0x67400001L, new byte[1]));

// PT_MV_LONG, as a named property
values = new ArrayList<Object>();
values.add(4);

UUID uuid = UUID.randomUUID();
MapiProperty property = new MapiProperty(
        msg.getNamedPropertyMapping().getNextAvailablePropertyId(MapiPropertyType.PT_MV_LONG), values);
msg.getNamedPropertyMapping().addNamedPropertyMapping(property, 0x00008028L, uuid);
msg.setProperty(property);

// Or set a custom property under a name of your own
MapiMessage message = new MapiMessage("sender@test.com", "recipient@test.com", "subj", "Body of test msg");

values = new ArrayList<Object>();
values.add(4);

property = new MapiProperty(
        message.getNamedPropertyMapping().getNextAvailablePropertyId(MapiPropertyType.PT_MV_LONG), values);
message.addCustomProperty(property, "customProperty");

// PT_FLOAT holds raw bytes, so convert the value with BitConverter
float floatValue = 123.456F;
MapiMessage newMsg = new MapiMessage();
long floatTag = newMsg.getNamedPropertyMapping().getNextAvailablePropertyId(MapiPropertyType.PT_FLOAT);
UUID guid = UUID.randomUUID();

MapiProperty newMapiProperty = new MapiProperty(floatTag,
        com.aspose.email.system.BitConverter.getBytesSingle(floatValue));
newMsg.getNamedPropertyMapping().addNamedPropertyMapping(newMapiProperty, 12L, guid);
newMsg.setProperty(newMapiProperty);

// Read the value back
for (Object value : newMsg.getNamedProperties().getValues()) {
    MapiNamedProperty prop = (MapiNamedProperty) value;
    if (guid.equals(prop.getGuid())) {
        System.out.println("Float property = " + prop.getFloat());
    }
}

ลบคุณสมบัติ

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";

MapiMessage mapi = new MapiMessage("from@domain.com", "to@domain.com", "subject", "body");
mapi.setBodyContent("<html><body><h1>This is the body content</h1></body></html>", BodyContentType.Html);

// Embed another message as an attachment
MapiMessage attachment = MapiMessage.load(dataDir + "message.msg");
mapi.getAttachments().add("Test subject.msg", attachment);

MapiAttachment last = mapi.getAttachments().get_Item(mapi.getAttachments().size() - 1);
System.out.println("Before removal = " + last.getProperties().size());

// Remove one property by its tag
last.removeProperty(923467779);
System.out.println("After removal = " + last.getProperties().size());

mapi.save(dataDir + "WithoutProperty_out.msg");

MapiMessage reloaded = MapiMessage.load(dataDir + "WithoutProperty_out.msg");
System.out.println("Reloaded = "
        + reloaded.getAttachments().get_Item(reloaded.getAttachments().size() - 1).getProperties().size());

อ่านคุณสมบัติ Mapi ที่ตั้งชื่อจากข้อความอีเมล

Microsoft Outlook รองรับการเพิ่มคุณสมบัติ MAPI ที่ตั้งชื่อลงในไฟล์ MSG. คุณสมบัติเหล่านี้เพิ่มโดยผู้ใช้. นักพัฒนาสามารถเพิ่มคุณสมบัติที่ตั้งชื่อ เช่น “MyProp”, ลงในไฟล์ MSG ด้วย Aspose.Email.

บทความนี้แสดงให้เห็น Aspose.Email MapiMessage getNamedProperties() คอลเลกชั่นเพื่ออ่านคุณสมบัติ MAPI ที่ตั้งชื่อจากไฟล์ MSG.

อ่านคุณสมบัติ MAPI ที่ตั้งชื่อ

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";

// Load the MSG file
MapiMessage message = MapiMessage.load(dataDir + "message.msg");

// Get all the named MAPI properties
MapiPropertyCollection properties = message.getNamedProperties();

for (Object value : properties.getValues()) {
    MapiNamedProperty mapiNamedProp = (MapiNamedProperty) value;

    // Read any specific property
    if ("TEST".equals(mapiNamedProp.getNameId()) || "MYPROP".equals(mapiNamedProp.getNameId())) {
        System.out.println(mapiNamedProp.getNameId() + " equals " + mapiNamedProp.getString());
    }
}

อ่านคุณสมบัติ Mapi ที่ตั้งชื่อจากไฟล์แนบ

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";

MailMessage mail = MailMessage.load(dataDir + "test.eml");
MapiMessage mapi = MapiMessage.fromMailMessage(mail);

MapiAttachment attachment = mapi.getAttachments().get_Item(0);

for (Object value : attachment.getNamedProperties().getValues()) {
    MapiNamedProperty namedProperty = (MapiNamedProperty) value;

    if ("CustomAttGuid".equalsIgnoreCase(namedProperty.getNameId())) {
        System.out.println("Found the CustomAttGuid property");
    }
}