Trabalhando com Propriedades MAPI usando a Biblioteca Email C++

Acessando e Definindo Propriedade MAPI do Outlook

A classe MapiProperty representa uma propriedade MAPI, que contém:

  • Nome: uma string que representa o nome da propriedade.
  • Tag: um long que representa a tag da propriedade.
  • Dados: um array de bytes que representa os dados da propriedade.

Obter Propriedade MAPI usando a Tag de Propriedade MAPI

Para obter propriedades MAPI:

  1. Crie uma instância de MapiMessage carregando de arquivos ou stream.
  2. Obtenha a MapiProperty de MapiMessage.Properties pelas chaves MapiPropertyTag.
  3. Obtenha os Dados da MapiProperty pelo método GetX.

O seguinte trecho de código mostra como obter a propriedade MAPI usando a tag de propriedade MAPI com a Biblioteca de Parser de Email 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());
}

Definindo Propriedades MAPI

O seguinte trecho de código mostra como definir propriedades 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");

onde a definição do método convertDateTime é a seguinte:


 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);

Lendo Propriedades MAPI Nomeadas de Arquivos MSG do Outlook

O Microsoft Outlook suporta a adição de propriedades MAPI nomeadas a um arquivo MSG. Essas propriedades MAPI nomeadas são adicionadas pelo usuário. Você pode adicionar uma propriedade nomeada, por exemplo “MyProp”, a um arquivo MSG usando Aspose.Email. Este artigo ilustra as capacidades do Aspose.Email para:

  • Ler Propriedades MAPI Nomeadas de um arquivo MSG do Outlook
  • Ler Propriedades de Anexos
  • Remover Propriedades de MSGs e Anexos

Ler Propriedades MAPI Nomeadas de arquivo MSG

O seguinte trecho de código mostra como ler propriedades MAPI nomeadas de um arquivo 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);
}
}
}

Lendo Propriedade MAPI Nomeada de Anexo

Aspose.Email também permite que você navegue pelas propriedades de um MapiAttachment e busque por uma propriedade nomeada, de forma similar ao exemplo acima, para MapiMessage. O seguinte trecho de código mostra como buscar por uma propriedade nomeada através da coleção de propriedades do anexo.

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;

Remover Propriedades de MSGs e Anexos

O seguinte trecho de código mostra como remover propriedades de MSGs e anexos.

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());