To convert an MSG file, specifically an Outlook Calendar appointment, to an EML file while preserving the date data, you can utilize the Aspose.Email API effectively. Here’s a step-by-step approach to ensure that the date information is retained during the conversion process.
Load the MSG File: Use the MapiMessage class to load your MSG file. This class allows you to access the properties of the message, including appointment details.
Convert to MailMessage: Use the toMailMessage method to convert the MapiMessage to a MailMessage. This method is designed to handle various message types, including appointments.
Preserve Date Information: Ensure that you are using the MapiConversionOptions class to set the option to preserve empty dates. This is crucial for maintaining the integrity of the date data during the conversion.
Save as EML: Finally, save the MailMessage as an EML file.
Here is a code example demonstrating this process:
using Aspose.Email;
using Aspose.Email.Outlook;
using Aspose.Email.Mail;
// Load the MSG file
MapiMessage mapiMessage = MapiMessage.FromFile("path_to_your_file.msg");
// Create conversion options
MapiConversionOptions options = new MapiConversionOptions();
options.PreserveEmptyDates = true; // Preserve empty dates
// Convert to MailMessage
MailMessage mailMessage = mapiMessage.ToMailMessage(options);
// Save as EML
mailMessage.Save("output_file.eml", SaveOptions.DefaultEml);
Make sure to replace "path_to_your_file.msg" and "output_file.eml" with the actual paths for your files.
By following these steps, you should be able to convert your appointment MSG files to EML format while retaining the necessary date information. If you continue to experience issues, please ensure that you are using the latest version of the Aspose.Email library, as improvements and bug fixes are regularly released.
If you have any further questions or need additional assistance, feel free to ask!
ToMailMessage requires MeilConversationOption not MapiConverstaionOption;
but firs one have no property PreserveOriginalDates;
So, following sample still does not gives results:
Dim mapiMessage As MapiMessage = MapiMessage.FromFile(“c:\temp\meet.msg”)
Dim options As MailConversionOptions = New MailConversionOptions()
'options…PreserveOriginalDates = True
Dim mailMessage As Aspose.Email.MailMessage = mapiMessage.ToMailMessage(options)
mailMessage.Save(“c:\temp\meet.eml”, Aspose.Email.SaveOptions.DefaultEml)
Using the latest version, please try the following code:
// Load the MSG file
var mapiMessage = MapiMessage.Load("appointment.msg");
// Create conversion options
var options = new MailConversionOptions
{
ConvertAsTnef = true
};
// Convert to MailMessage
var mailMessage = mapiMessage.ToMailMessage(options);
// Save as EML
mailMessage.Save("appointment.eml", SaveOptions.DefaultEml);
If the problem persists, please share the msg file for further investigation.
Code above generates some attachment which I actually dont need;
I just changed options by this: {.PreserveEmbeddedMessageFormat = True, .PreserveRtfContent = True}
But anyway, there are no any Date (start time, end time) data in generated EML file.
meanwhile you can see these data in MSG file.
EML format(MIME - Wikipedia) does not assume the presence of such properties as start and end. When converting the msg format to eml, the meeting data is converted to ICS format(iCalendar - Wikipedia) and attached to the message as an alternative view. Below is the code on how to get the desired values ​​using Aspose.Email.
If you want see these dates in Outllok you can save appointment to ics file various methods, as show in code below, and then open it in Outlook.
Using eml As Email.MailMessage = Email.MailMessage.Load("meet.msg")
For Each av In eml.AlternateViews
If Equals(av.ContentType.MediaType, "text/calendar") Then
Dim appointment As Email.Calendar.Appointment = Email.Calendar.Appointment.Load(av.ContentStream)
Dim start As Date = appointment.StartDate
Dim [end] As Date = appointment.EndDate
appointment.Save("FileForOutlook1.ics", Email.Calendar.AppointmentSaveFormat.Ics)
End If
Next
End Using
Using msg As Email.Mapi.MapiMessage = Email.Mapi.MapiMessage.Load("meet.msg")
Dim cal As Email.Mapi.MapiCalendar = CType(msg.ToMapiMessageItem(), Email.Mapi.MapiCalendar)
Dim ms As MemoryStream = New MemoryStream()
cal.Save("FileForOutlook2.ics", Email.Calendar.AppointmentSaveFormat.Ics)
cal.Save(ms, Email.Calendar.AppointmentSaveFormat.Ics)
Dim app As Email.Calendar.Appointment = Email.Calendar.Appointment.Load(ms)
Dim start As Date = app.StartDate
Dim [end] As Date = app.EndDate
app.Save("FileForOutlook3.ics", Email.Calendar.AppointmentSaveFormat.Ics)
End Using
I used that one and also code for build EML file with body and Alternative View:
Dim ics = Calendar.Appointment.Load("FileForOutlook1.ics")
Dim eml1 = MailMessage.Load("meet.eml")
eml1.AlternateViews.Add(ics.RequestApointment())
eml1.Save("result.eml", New EmlSaveOptions(MailMessageSaveType.EmlFormat))
As a result, EML file can open from Classic Outlook with all data, but New Outlook still unable to show Appointment data.
May be alternative view should be added by some specific way ?
You are using the correct method to add an alternative view
We could not find any problems in the resulting eml file. As described in many articles, New Outlook is not a replacement of the Outlook desktop program, so I’m afraid this is a issue with the New Outlook with opening eml files with the alternative view text/calendar type.