Import Outlook MSG with Thousands of Recipients to PST - c#

I have a number of Outlook MSGs that I need to save to alternate formats, such as MHT. The MSGs each have thousands of recipients, and I am aware that the MSG structure is not robust enough to handle large numbers of recipients. I believe that my first step is to import the MSGs into a PST, which can handle large numbers of recipients.
I am using Outlook 2010 and Redemption 5.4 (full). I am prepared to use either the Outlook object model or Redemption to accomplish my goals. Platform: Windows 7 SP1, 64-bit.
I have tried these methods:
//Establish Session
RDOSession rdoSession = new RDOSession();
rdoSession.Logon(null, null, false, true, null, true);
//Create empty mail item in PST.
RDOMail rdoMail = rdoSession.GetDefaultFolder(rdoDefaultFolders.olFolderInbox).Items.Add(null);
//Merge MSG into new, empty mail
//fi.FullName = C:\<subdirectories>\009.msg
rdoMail.Import(fi.FullName, rdoSaveAsType.olMSGUnicode);
rdoMail.Save();
//Save as MHT
rdoMail.SaveAs(diMht.FullName + #"\" + strNormalizedSubject + ".mht", Redemption.rdoSaveAsType.olMHTML);
This code fails to import the MSG into the PST. I receive this exception:
{"Error importing: 0x8004011B"}
I have also tried this method, which does not rely on a PST.
RDOMail rdoMail = rdoSession.GetMessageFromMsgFile(fi.FullName, false);
string strNormalizedSubject = NormalizeSubject(rdoMail.Subject);
rdoMail.SaveAs(diMht.FullName + #"\" + strNormalizedSubject + ".mht", Redemption.rdoSaveAsType.olMHTML);
This code results in this exception: Error in OpenIMsgOnIStg: MAPI_E_CORRUPT_DATA.
I believe that Transend Migrator can convert MSGs with large numbers or recipients. However, that is not an option due to high licensing costs.
How can I save the MSG to another format?

0x8004011B is MAPI_E_CORRUPT_DATA. Looks like you have a corrupt PST store. Did you try to repair it using scanpst.exe?

Dmitry suggested that I treat MSG files as OLE storage files. That idea led me to this site: Reading an Outlook MSG File in C# CodeProject.
I have confirmed that the project is able to open MSGs with large numbers of recipients. I tested it with an MSG having 2,499 recipients.
That project may make it possible to extract the data required to build an MHT file.

Related

Convert EML to MSG c# Using EWS

I need help with converting EML to MSG, using the Exchange web service (EWS) in a Outlook Web Add-In. When i create an EML file from the MimeContent (EmailMessage.MimeContent.Content), the file output looks bad, some tags are not convert currently.
The files open good just from Windows mail app, but from Ooutlook(2016) looks bad.
I tried to find some solution from Microsoft and found this Independentsoft, a third party solution, and it is work great. the file looks good while the format is MSG. but it is to expansive licence solution for the customer (used 30 days demo).
This is what i used and work well, and try to found something like this:
//1.This code use the EWS mimeContent(the message on bytes - eml format)
//2.Create Independentsoft message object
//Independentsoft.Msg.Message constractor do the convert by
// making an msg object from an eml object.
//3. save the msg file.
Independentsoft.Email.Mime.Message mimeMessage = new Independentsoft.Email.Mime.Message(emailMessage.MimeContent.Content);
Independentsoft.Msg.Message msgMessage = Independentsoft.Msg.Message(mimeMessage);
using (MemoryStream memStream = new MemoryStream(emailMessage.MimeContent.Content.Length))
{
Directory.CreateDirectory(TempMsgDirectory);
msgMessage.Save(TempMsgDirectory + "mail.msg", true);
}
I am not aware of any Outlook problems with displaying EML files. It uses the same EML parser used to parse incoming POP3/IMAP4 messages. Please post a specific EML file that Outlook does not display correctly.
As for converting EML files to MSG, you can also use Redemption (I am its author) and its RDOSession.CreateMessageFromMsgFIle and RDOMail.Import methods. Just keep in mind that it requires the MAPI system to be present to function properly, which means Outlook must be installed locally.
Off the top of my head:
RDOSession session = new RDOSession();
RDOMail msg = session.CreateMessageFromMsgFile(TempMsgDirectory + "mail.msg");
msg.Import(TempMsgDirectory + "YouEmlFile.eml", rdoSaveAsType.olRFC822);
msg.Save();
Also keep in mind that retrieving MIME content from Exchange might not be the best idea - you will lose all MAPI specific properties and will end up with a corrupted message if the original was in the RTF format (with or without embedded OLE objects). In that case, you can use ExportItems EWS operation. Its format is not documented, but it is very close to the MSG format, and you can convert it to MSG using code similar to that above, but specifying olFTS format instead of olRFC822.

Converting .MSG files to .TXT; Should I use Microsoft.Interop Outlook?

I'm trying to do a conversion from .msg files into .txt. I have two questions.
1)I've been investigating and found the Microsoft.Interop Outlook package and there is a way where I can extract the bodyHTML, To, Sent Date, and a few other properties but I feel as if this is a very manual process because I have to trim out all the html tags such as < br>, &nbsp, a href etc...
Here is my current code...
MailItem mailItem = outlookApp.Session.OpenSharedItem(item) as MailItem;
TextFile textFile = new TextFile(); //collection of properties I am interested in
textFile.To = mailItem.To;
textFile.Subject = mailItem.Subject;
textFile.Sent = mailItem.SentOn.ToString();
textFile.Name = Path.GetFileNameWithoutExtension(item);
var atttach = mailItem.Attachments; //Really just want the names
textFile.Body = RemoveStuff(mailItem.HTMLBody); //manually removing all html tags
textFiles.Add(textFile);
Marshal.ReleaseComObject(mailItem);
Does anyone know if there is a more effective way to do this in C# or a way using Interop that I am not aware of?
2)If I go the interop route, is there a way I can bypass the popup in Outlook asking if I can allow access to Outlook? Seems inefficient if my goal is to create a converter.
Any help is greatly appreciated.
Thanks!
Firstly, why are you using HTMLBody property instead of the plain text Body?
Secondly, you can use MailItem.SaveAs(..., olTxt) to save the message as a text file. Or do you mean something else by txt file?
The security prompt is raised by Outlook if your antivirus app is not up to date. If you cannot control the environment where your code runs, Extended MAPI (C++ or Delphi only) or a wrapper like Redemption (any language - I am its author) are pretty much your only option. See http://www.outlookcode.com/article.aspx?id=52 for more details.
In Redemption, you can have something like the following:
using Redemption;
...
RDOSession session = new RDOSession();
RDOMail msg = session.GetMessageFromMsgFile(TheFileName);
msg.SaveAs(TxtFileName, rdoSaveAsType.olTXT);

How to add attachments to .oft file with Aspose

I want to create a .oft file with C# .
The oft file needs to have a html body and attachments.
I found that i should be able to do this using Aspose.
However when i run my code the attachments never get added.
( They are added to the AttachmentCollection but aren't shown when the .oft file is opened in outlook. The oft file does get 20x as large so i presume the raw attachment data is added. )
MailMessage message = new MailMessage();
var attachmentGif = new Attachment(#"C:\inetpub\wwwroot\IntranetKbs\Website\Email logos\" + language + #"\KBSFRB_logo_" + language + #".gif");
var attachmentEps = new Attachment(#"C:\inetpub\wwwroot\IntranetKbs\Website\Email logos\" + language + #"\KBSFRB_logo_" + language + #".eps");
message.Attachments.Add(attachmentGif);
message.Attachments.Add(attachmentEps);
Below you can see some information about the attachments after they are added to the AttachmentList.
If you know what could cause this, or know an other way to create .oft files you help and comments would be greatly appreciated!
If you have to display the images in message's HTML body, you need to include them as linked resource rather than regular attachments. This should serve your requirements of adding image to the message body and display it in MS Outlook.
Note: I work with Aspose as Developer Evangelist.

Sending emails with signatures via C#

I have a the following setup to send emails from my c# Application :
SmtpClient (under System.Net.Mail namespace) to do the actual sending once everything is in place and set the 'IsBodyHtml' property of the Message object to True
Using the dll from Sautinsoft I convert a simple rtf file which contains the formatting of the email and convert it to a HTML string which I then use as the body of the mail.
It works great just as it is and I have sent a few test emails to myself and all the appropriate formatting is retained. However i am having a problem with images - The dll converts images to a img tag and uses the base 64 format of the image as the data source, this works fine if you view it as a html page, but sending it as the body of you email produces problems. Email clients such as Yahoo don't mind embedded images but Gmail does not play nice with this methodology. The only image that should appear in the emails I'm sending is the signature image located at the bottom of each email. Using signatures in the native Gmail client in your browser poses no problems since the image has a link to a actual file on a server somewhere, but sending emails with signatures via a C# Application seems to be a different story. Any suggestions?
Thank you for your time.
You may consider automating Outlook from a C# application. See How to automate Outlook and Word by using Visual C# .NET to create a pre-populated e-mail message that can be edited. Also you can find a sample code - C# app automates Outlook (CSAutomateOutlook).
If you are talking about RTF2HTML, you may add any images in a separate folder (no include in body of HTML):
string inpFile = #"..\..\..\..\example.docx";
string outFile = Path.GetFullPath(#"Result2.html");
string imgDir = Path.GetDirectoryName(outFile);
RtfToHtml r = new RtfToHtml();
// Set images directory
HtmlFixedSaveOptions opt = new HtmlFixedSaveOptions()
{
ImagesDirectoryPath = Path.Combine(imgDir, "Result_images"),
ImagesDirectorySrcPath = "Result_images",
// Change to store images as physical files on local drive.
EmbedImages = false
};
try
{
r.Convert(inpFile, outFile, opt);
}
catch (Exception ex)
{
Console.WriteLine($"Conversion failed! {ex.Message}");
}
As the result you will see HTML file with linked images in folder.

Lotus Notes - Saving Corrupt Attachment Programmatically - NotesEmbeddedObject

I have an NSF that contains an email message with two attachments. One of the attachments is corrupt, and if I attempt to save it, Notes displays this message The attachment may be corrupted. Would you like to continue with the available data?
If I click Yes, Notes saves the corrupt attachment to the directory I specify. This is good.
I would like to do the same thing using the object model in C#. If I run NotesEmbeddedObject.ExtractFile(), I receive this exception message: Notes error: Encoded Data Checksum Mismatch - Attachment may be corrupted. No version of the file is written to the directory I specify.
I would like for the code to write the corrupted version to a directory. How can I do this?
Existing Code:
//BEGIN Extract Attachment
//nItem is a NotesItem
if (nItem.type == IT_TYPE.ATTACHMENT)
{
try
{
string pAttachment = ((object[])nItem.Values)[0].ToString();
NotesDocument NDoc = NotesConnectionDatabase.AllDocuments.GetNthDocument(i);
NotesEmbeddedObject Neo = NDoc.GetAttachment(pAttachment);
NDoc.GetAttachment(pAttachment).ExtractFile(#"D:\projects\xxx\Attach\" + pAttachment);
}
catch (Exception e)
{
string eMessage = e.Message;
Console.WriteLine(eMessage);
}
}
//END Extract Attachment
I'm afraid not.
The NotesEmbeddedObject.ExtractFile method attempts to extract the attachment, but there's a checksum mismatch, and as soon as it gets that error, it throws an exception.
I don't know of any other Notes back-end classes that deal with attachments (maybe someone else does...)

Categories

Resources