Trabajando con Propiedades MAPI usando la Biblioteca de Correo C++
Accediendo y Estableciendo Propiedades MAPI de Outlook
La clase MapiProperty representa una propiedad MAPI, que contiene:
- Nombre: una cadena que representa el nombre de la propiedad.
- Etiqueta: un largo que representa la etiqueta de la propiedad.
- Datos: un arreglo de bytes que representa los datos de la propiedad.
Obteniendo la Propiedad MAPI usando la Etiqueta de Propiedad MAPI
Para obtener propiedades MAPI:
- Crea una instancia de MapiMessage cargando desde archivos o flujo.
- Obtén el MapiProperty de MapiMessage.Properties mediante las claves MapiPropertyTag.
- Obtén los Datos del MapiProperty mediante el método GetX.
El siguiente fragmento de código te muestra cómo obtener la propiedad MAPI usando la etiqueta de propiedad MAPI con la Biblioteca de Análisis de Correo C++.
For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C | |
// The path to the File directory. | |
System::String dataDir = RunExamples::GetDataDir_Outlook(); | |
// Load from file | |
System::SharedPtr<MapiMessage> msg = MapiMessage::FromFile(dataDir + L"message.msg"); | |
System::String subject; | |
// Access the MapiPropertyTag.PR_SUBJECT property | |
System::SharedPtr<MapiProperty> prop = msg->get_Properties()->idx_get(MapiPropertyTag::PR_SUBJECT); | |
// If the property is not found, check the MapiPropertyTag.PR_SUBJECT_W (which is a // Unicode peer of the MapiPropertyTag.PR_SUBJECT) | |
if (prop == nullptr) | |
{ | |
prop = msg->get_Properties()->idx_get(MapiPropertyTag::PR_SUBJECT_W); | |
} | |
// Cannot found | |
if (prop == nullptr) | |
{ | |
System::Console::WriteLine(L"No property found!"); | |
return; | |
} | |
// Get the property data as string | |
subject = prop->GetString(); | |
System::Console::WriteLine(System::String(L"Subject:") + subject); | |
// Read internet code page property | |
prop = msg->get_Properties()->idx_get(MapiPropertyTag::PR_INTERNET_CPID); | |
if (prop != nullptr) | |
{ | |
System::Console::WriteLine(System::String(L"CodePage:") + prop->GetLong()); | |
} |
Estableciendo Propiedades MAPI
El siguiente fragmento de código te muestra cómo establecer propiedades MAPI.
For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C | |
// The path to the File directory. | |
System::String dataDir = RunExamples::GetDataDir_Outlook(); | |
// Create a sample Message | |
System::SharedPtr<MapiMessage> mapiMsg = System::MakeObject<MapiMessage>(L"user1@gmail.com", L"user2@gmail.com", L"This is subject", L"This is body"); | |
// Set multiple properties | |
System::StaticCast<MapiMessageItemBase>(mapiMsg)->SetProperty(System::MakeObject<MapiProperty>(MapiPropertyTag::PR_SENDER_ADDRTYPE_W, System::Text::Encoding::get_Unicode()->GetBytes(L"EX"))); | |
System::SharedPtr<MapiRecipient> recipientTo = mapiMsg->get_Recipients()->idx_get(0); | |
System::SharedPtr<MapiProperty> propAddressType = System::MakeObject<MapiProperty>(MapiPropertyTag::PR_RECEIVED_BY_ADDRTYPE_W, System::Text::Encoding::get_UTF8()->GetBytes(L"MYFAX")); | |
recipientTo->SetProperty(propAddressType); | |
System::String faxAddress = L"My Fax User@/FN=fax#/VN=voice#/CO=My Company/CI=Local"; | |
System::SharedPtr<MapiProperty> propEmailAddress = System::MakeObject<MapiProperty>(MapiPropertyTag::PR_RECEIVED_BY_EMAIL_ADDRESS_W, System::Text::Encoding::get_UTF8()->GetBytes(faxAddress)); | |
recipientTo->SetProperty(propEmailAddress); | |
mapiMsg->SetMessageFlags(Aspose::Email::Outlook::MapiMessageFlags::MSGFLAG_UNSENT | Aspose::Email::Outlook::MapiMessageFlags::MSGFLAG_FROMME); | |
System::StaticCast<MapiMessageItemBase>(mapiMsg)->SetProperty(System::MakeObject<MapiProperty>(MapiPropertyTag::PR_RTF_IN_SYNC, System::BitConverter::GetBytes((int64_t)1))); | |
// Set DateTime property | |
System::SharedPtr<MapiProperty> modificationTime = System::MakeObject<MapiProperty>(MapiPropertyTag::PR_LAST_MODIFICATION_TIME, ConvertDateTime(System::DateTime(2013, 9, 11))); | |
System::StaticCast<MapiMessageItemBase>(mapiMsg)->SetProperty(modificationTime); | |
mapiMsg->Save(dataDir + L"MapiProp_out.msg"); |
donde la definición del método convertDateTime es la siguiente:
int64_t filetime = t.ToFileTime();
System::ArrayPtr<uint8_t> d = System::MakeArray<uint8_t>(8, 0);
d[0] = (uint8_t)(filetime & 0xFF);
d[1] = (uint8_t)((filetime & 0xFF00) >> 8);
d[2] = (uint8_t)((filetime & 0xFF0000) >> 16);
d[3] = (uint8_t)((filetime & 0xFF000000) >> 24);
d[4] = (uint8_t)((filetime & 0xFF00000000) >> 32);
d[5] = (uint8_t)((filetime & 0xFF0000000000) >> 40);
d[6] = (uint8_t)((filetime & 0xFF000000000000) >> 48);
d[7] = (uint8_t)(((uint64_t)filetime & 0xFF00000000000000) >> 56);
Leyendo Propiedades MAPI Nombradas de Archivos MSG de Outlook
Microsoft Outlook admite la adición de propiedades MAPI nombradas a un archivo MSG. Estas propiedades MAPI nombradas son agregadas por el usuario. Puedes agregar una propiedad nombrada, por ejemplo “MyProp”, a un archivo MSG usando Aspose.Email. Este artículo ilustra las capacidades de Aspose.Email para:
- Leer Propiedades MAPI Nombradas de un archivo MSG de Outlook
- Leer Propiedades de Archivos Adjuntos
- Eliminar Propiedades de MSGs y Archivos Adjuntos
Leer Propiedades MAPI Nombradas de un archivo MSG
El siguiente fragmento de código te muestra cómo leer propiedades MAPI nombradas de un archivo MSG.
For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C | |
// The path to the File directory. | |
System::String dataDir = RunExamples::GetDataDir_Outlook(); | |
// Load from file | |
System::SharedPtr<MapiMessage> message = MapiMessage::FromFile(dataDir + L"message.msg"); | |
// Get all named MAPI properties | |
System::SharedPtr<MapiPropertyCollection> properties = message->get_NamedProperties(); | |
// Read all properties in foreach loop | |
{ | |
auto mapiNamedProp_enumerator = (System::DynamicCastEnumerableTo<System::SharedPtr<MapiNamedProperty>>(properties->get_Values()))->GetEnumerator(); | |
decltype(mapiNamedProp_enumerator->get_Current()) mapiNamedProp; | |
while (mapiNamedProp_enumerator->MoveNext() && (mapiNamedProp = mapiNamedProp_enumerator->get_Current(), true)) | |
{ | |
// Read any specific property | |
{ | |
const System::String switch_value_0 = mapiNamedProp->get_NameId(); | |
do { | |
if (switch_value_0 == L"TEST") | |
{ | |
System::Console::WriteLine(L"{0} = {1}", System::ObjectExt::Box<System::String>(mapiNamedProp->get_NameId()), System::ObjectExt::Box<System::String>(mapiNamedProp->GetString())); | |
break; | |
} | |
if (switch_value_0 == L"MYPROP") | |
{ | |
System::Console::WriteLine(L"{0} = {1}", System::ObjectExt::Box<System::String>(mapiNamedProp->get_NameId()), System::ObjectExt::Box<System::String>(mapiNamedProp->GetString())); | |
break; | |
} | |
if (true) | |
{ | |
break; | |
} | |
} while (false); | |
} | |
} | |
} |
Leyendo la Propiedad MAPI Nombrada de un Archivo Adjunto
Aspose.Email también te permite recorrer las propiedades de un MapiAttachment y buscar una propiedad nombrada, de manera similar al ejemplo anterior, para MapiMessage. El siguiente fragmento de código te muestra cómo buscar una propiedad nombrada a través de la colección de propiedades del adjunto.
For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C | |
// The path to the File directory. | |
System::String dataDir = RunExamples::GetDataDir_Outlook(); | |
// Load from file | |
System::SharedPtr<MailMessage> mail = MailMessage::Load(dataDir + L"outputAttachments.msg"); | |
auto mapi = MapiMessage::FromMailMessage(mail); | |
{ | |
auto namedProperty_enumerator = (System::DynamicCastEnumerableTo<System::SharedPtr<MapiNamedProperty>>(mapi->get_Attachments()->idx_get(0)->get_NamedProperties()->get_Values()))->GetEnumerator(); | |
decltype(namedProperty_enumerator->get_Current()) namedProperty; | |
while (namedProperty_enumerator->MoveNext() && (namedProperty = namedProperty_enumerator->get_Current(), true)) | |
{ | |
if (System::String::Compare(namedProperty->get_NameId(), L"CustomAttGuid", System::StringComparison::OrdinalIgnoreCase) == 0) | |
{ | |
return namedProperty->GetString(); | |
} | |
} | |
} | |
return System::String::Empty; |
Eliminar Propiedades de MSGs y Archivos Adjuntos
El siguiente fragmento de código te muestra cómo eliminar propiedades de MSGs y adjuntos.
For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C | |
// The path to the File directory. | |
System::String dataDir = RunExamples::GetDataDir_Outlook(); | |
System::SharedPtr<MapiMessage> mapi = System::MakeObject<MapiMessage>(L"from@doamin.com", L"to@domain.com", L"subject", L"body"); | |
mapi->SetBodyContent(L"<html><body><h1>This is the body content</h1></body></html>", Aspose::Email::Outlook::BodyContentType::Html); | |
System::SharedPtr<MapiMessage> attachment = MapiMessage::FromFile(dataDir + L"message.msg"); | |
mapi->get_Attachments()->Add(L"Outlook2 Test subject.msg", attachment); | |
System::Console::WriteLine(System::String(L"Before removal = ") + mapi->get_Attachments()->idx_get(mapi->get_Attachments()->get_Count() - 1)->get_Properties()->get_Count()); | |
mapi->get_Attachments()->idx_get(mapi->get_Attachments()->get_Count() - 1)->RemoveProperty(923467779); | |
// Delete anyone property | |
System::Console::WriteLine(System::String(L"After removal = ") + mapi->get_Attachments()->idx_get(mapi->get_Attachments()->get_Count() - 1)->get_Properties()->get_Count()); | |
mapi->Save(L"EMAIL_589265.msg"); | |
System::SharedPtr<MapiMessage> mapi2 = MapiMessage::FromFile(L"EMAIL_589265.msg"); | |
System::Console::WriteLine(System::String(L"Reloaded = ") + mapi2->get_Attachments()->idx_get(mapi2->get_Attachments()->get_Count() - 1)->get_Properties()->get_Count()); |