Cargar, guardar y convertir diferentes formatos de mensajes de correo electrónico en C++
Cargando un Mensaje con Opciones de Carga
El siguiente fragmento de código te muestra cómo cargar un mensaje con opciones de carga.
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_Email(); | |
// Load Eml, html, mhtml, msg and dat file | |
System::SharedPtr<MailMessage> mailMessage = MailMessage::Load(dataDir + L"Message.eml", System::MakeObject<EmlLoadOptions>()); | |
MailMessage::Load(dataDir + L"description.html", System::MakeObject<HtmlLoadOptions>()); | |
MailMessage::Load(dataDir + L"Message.mhtml", System::MakeObject<MhtmlLoadOptions>()); | |
MailMessage::Load(dataDir + L"Message.msg", System::MakeObject<MsgLoadOptions>()); | |
// loading with custom options | |
System::SharedPtr<EmlLoadOptions> emlLoadOptions = System::MakeObject<EmlLoadOptions>(); | |
emlLoadOptions->set_PrefferedTextEncoding(System::Text::Encoding::get_UTF8()); | |
emlLoadOptions->set_PreserveTnefAttachments(true); | |
MailMessage::Load(dataDir + L"description.html", emlLoadOptions); | |
System::SharedPtr<HtmlLoadOptions> htmlLoadOptions = System::MakeObject<HtmlLoadOptions>(); | |
htmlLoadOptions->set_PrefferedTextEncoding(System::Text::Encoding::get_UTF8()); | |
htmlLoadOptions->set_ShouldAddPlainTextView(true); | |
htmlLoadOptions->set_PathToResources(dataDir); | |
MailMessage::Load(dataDir + L"description.html", emlLoadOptions); |
Guardando y Convirtiendo Mensajes
Aspose.Email facilita la conversión de cualquier tipo de mensaje a otro formato. Para demostrar esta característica, el código en este artículo carga tres tipos de mensajes desde el disco y los guarda de nuevo en otros formatos. La clase base SaveOptions y las clases EmlSaveOptions, MsgSaveOptions, MhtSaveOptions, HtmlSaveOptions para configuraciones adicionales al guardar MailMessage se pueden utilizar para guardar mensajes en otros formatos. El artículo muestra cómo usar estas clases para guardar un correo electrónico de muestra como:
- Formato EML.
- Outlook MSG.
- Formato MHTML.
- Formato HTML.
Cargando EML y Guardando como EML
El siguiente fragmento de código te muestra cómo cargar un mensaje EML y guardarlo en el disco en el mismo formato.
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_Email(); | |
// Initialize and Load an existing EML file by specifying the MessageFormat | |
System::SharedPtr<MailMessage> mailMessage = MailMessage::Load(dataDir + L"Attachments.eml"); | |
mailMessage->Save(dataDir + L"LoadAndSaveFileAsEML_out.eml", SaveOptions::get_DefaultEml()); |
Cargando EML y Guardando como EML Preservando los Limites Originales
El siguiente fragmento de código te muestra cómo cargar EML y guardarlo como EML preservando los límites originales.
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_Email(); | |
System::SharedPtr<MailMessage> mailMessage = MailMessage::Load(dataDir + L"Attachments.eml"); | |
// Save as eml with preserved original boundares | |
System::SharedPtr<EmlSaveOptions> emlSaveOptions = System::MakeObject<EmlSaveOptions>(MailMessageSaveType::get_EmlFormat()); | |
emlSaveOptions->set_PreserveOriginalBoundaries(true); | |
mailMessage->Save(dataDir + L"PreserveOriginalBoundaries_out.eml", emlSaveOptions); |
Guardando como EML Preservando los Archivos Adjuntos TNEF
El siguiente fragmento de código te muestra cómo guardar como EML preservando los archivos adjuntos TNEF.
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_Email(); | |
System::SharedPtr<MailMessage> mailMessage = MailMessage::Load(dataDir + L"PreserveOriginalBoundaries.eml"); | |
// Save as eml with preserved attachment | |
System::SharedPtr<EmlSaveOptions> emlSaveOptions = System::MakeObject<EmlSaveOptions>(MailMessageSaveType::get_EmlFormat()); | |
emlSaveOptions->set_FileCompatibilityMode(Aspose::Email::Mail::FileCompatibilityMode::PreserveTnefAttachments); | |
mailMessage->Save(dataDir + L"PreserveTNEFAttachment_out.eml", emlSaveOptions); |
Cargando EML, Guardando como MSG
El siguiente fragmento de código te muestra cómo cargar un mensaje EML y convertirlo a MSG utilizando la opción adecuada de SaveOptions.
For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C | |
// Data directory for reading and writing files | |
System::String dataDir = RunExamples::GetDataDir_Email(); | |
// Initialize and Load an existing EML file by specifying the MessageFormat | |
System::SharedPtr<MailMessage> eml = MailMessage::Load(dataDir + L"Message.eml"); | |
// Save the Email message to disk in ASCII format and Unicode format | |
eml->Save(dataDir + L"AnEmail_out.msg", SaveOptions::get_DefaultMsgUnicode()); |
Guardando MailMessage como MHTML
Diferentes opciones de MHTML se pueden utilizar para obtener los resultados deseados. El siguiente fragmento de código te muestra cómo cargar un mensaje EML en MailMessage y convertirlo a MHTML.
For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C | |
// Initialize and Load an existing EML file by specifying the MessageFormat | |
System::SharedPtr<MailMessage> eml = MailMessage::Load(dataDir + L"Message.eml"); | |
eml->Save(dataDir + L"AnEmail_out.mthml", SaveOptions::get_DefaultMhtml()); |
Exportando Correo Electrónico a MHT con Zona Horaria Personalizada
La clase MailMessage proporciona la propiedad TimeZoneOffset para establecer la zona horaria personalizada al exportar a MHT. El siguiente fragmento de código te muestra cómo exportar un correo electrónico a MHT con zona horaria personalizada.
For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C | |
System::SharedPtr<MailMessage> eml = MailMessage::Load(dataDir + L"Message.eml"); | |
// Set the local time for message date. | |
eml->set_TimeZoneOffset(System::TimeZone::get_CurrentTimeZone()->GetUtcOffset(System::DateTime::get_Now())); | |
// or set custom time zone offset for message date (-0800) | |
// eml.TimeZoneOffset = new TimeSpan(-8,0,0); | |
// The dates will be rendered by local system time zone. | |
System::SharedPtr<MhtSaveOptions> so = System::MakeObject<MhtSaveOptions>(); | |
so->set_MhtFormatOptions(Aspose::Email::Mail::MhtFormatOptions::WriteHeader); | |
eml->Save(dataDir + L"ExportEmailToMHTWithCustomTimezone_out.mhtml", so); |
Exportando Correo Electrónico a EML
El siguiente fragmento de código te muestra cómo exportar correo electrónico a eml.
For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C | |
System::String dataDir = RunExamples::GetDataDir_Email(); | |
System::SharedPtr<MailMessage> msg = MailMessage::Load(dataDir + L"Message.eml"); | |
msg->Save(dataDir + L"ExporttoEml_out.eml", SaveOptions::get_DefaultEml()); |