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>,  , 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);
Related
we are trying to get the content of the attachment's of the in the rtf mail but I have tried to search using different terms but have not found any reliable solution . can someone please help me to get the source of the attachment's as we get them in the html format.
The Outlook object model doesn't provide any property or method for getting the attachment content. To get the file attached you need to save it to the disk and then read the content from the there.
Also you may consider using a low-level API on which Outlook is based on - Extended MAPI. It allows getting the binary data of the attached file. Try using the Attachment.PropertyAccessor.GetProperty method while passing in the value "http://schemas.microsoft.com/mapi/proptag/0x37010102" (PR_ATTACH_DATA_BIN).
set msg = Application.ActiveExplorer.Selection(1)
set attach = msg.Attachments(1)
set ps = attach.PropertyAccessor
v = ps.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x37010102")
debug.print ps.BinaryToString(v)
On the low level (Extended MAPI in C++ or Delphi), you need to open the PR_ATTACH_DATA_OBJ property as IStorage and extract the data from there (it depends on the actual type of the attachment). You can see the data in the streams in OutlookSpy (I am its author) - select the message, click IMessage button on the OutlookSpy ribbon, go to the GetAttachmentTable tab, double click on the attachment to open it, select the PR_ATTACH_DATA_OBJ property, right click, select IMAPIProp::OpenProperty, then IStorage. Raw data will be there as well as an image representing the attachment (so that Outlook won't have to start the host app when rendering the message).
If using Redemption is an option (I am also its author, it can be used from any language including C# and VBA), its version of RDOAttachment.SaveAsFile method handles OLE attachments for most popular formats (Word, Excel, bitmap, Power Point, Adobe PDF, etc.) - create an instance of the RDOSession object (using either CrealeOleObject or RedemptionLoader) and use RDOSession.GetRDOObjectFromOutlookObject method (pass either MailItem or Attachment object) to get back RDOMail or RDOAttachment object respectively.
i have been found a solution for getting the images content from the rtf mail directly whiteout hitting the low level api or anything.
the solution is not a straight forward one
save the mail to the disk using oDoc.SaveAs2(filepath, WdSaveFormat.wdFormatFilteredHTML);
after saving the mail you will get the folder which you save a .htm doc
now read the .htm doc
get the all the image nodes of the .htm doc
using the image nods you can get src attribute of the image node
using the src value of the image you get the image directly from the disk itself and you can use that image
I need to open mail file attachment directly from mail. Let say I have .txt file. I have already attached it to my mail. But now I need to open it, change some words and save it (it is manual part). How can I do this? My code is:
private void button2_Click(object sender, EventArgs e)
{
Outlook.Inspector currInspector = null;
Outlook.MailItem mail = null;
Outlook.Attachments attachments = null;
currInspector = Globals.ThisAddIn.Application.ActiveInspector();
if (currInspector != null) {
mail = (Outlook.MailItem)currInspector.CurrentItem;
attachments = mail.Attachments;
attachments.Add(#"C:\install\CSharp\tulemus.txt", Outlook.OlAttachmentType.olByValue);
}
The Outlook object model doesn't provide any property or method for that. You can try to read the attached files from the cache folder maintained by Outlook. See Finding Outlook temporary folder for email attachments for more information.
Also, you can use a low-level API (Extended MAPI) where you can access the PR_ATTACH_DATA_BIN property, read more about the algorithm in the Opening an attachment article.
In addition to what Eugene suggested - use Attachment.PropertyAccessor or MAPI (C++ or Delphi) to access the PR_ATTACH_DATA_BIN property (DASL name "http://schemas.microsoft.com/mapi/proptag/0x37010102") - you can also use Redemption (I am its author): its RDOAttach.AsText / AsArray / AsStream properties allow to modify attachment contents on the fly
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.
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.
Fair warning: I'm a little bit of a rookie to C# and to Outlook, so bear with me on this.
I've been experimenting with emails in Outlook for a quick and dirty addin I'm building, but the addin requires me to send attached files to a different system.
Long story short; in order to do this I need to convert an Outlook item's mail attachment into a byte array.
What I have thus far (and the complete code is obviously miles longer than this, but I'm sure we all have better things to do than to sit and read page up and page down of code):
Outlook.Selection sel = control.Context as Outlook.Selection;
Outlook.MailItem mail = sel[1];
Outlook.Attachment a = mail.Attachments[0];
Problem is, that I have no idea how to convert a to a byte array.
PS: I know there are about a billion answers as to how to convert a byte array to a mail, but none to explain how to get it running the other way around.
EDIT 1: I would rather not have to save the file.
The second method proposed by Dmitry (open attachment as binary stream) is also achievable in managed code. It uses the PropertyAccessor interface, which is available for Attachment objects in C#. Here is some sample code I've used successfully in my own project:
const string PR_ATTACH_DATA_BIN = "http://schemas.microsoft.com/mapi/proptag/0x37010102";
Outlook.Attachment attachment = mail.Attachments[0];
// Retrieve the attachment as a byte array
var attachmentData =
attachment.PropertyAccessor.GetProperty(PR_ATTACH_DATA_BIN);
My example code is based on the How to: Modify an Attachment of an Outlook Email Message topic provided by Ken Getz, MCW Technologies, LLC as part of the MSDN documentation.
You can either
Save the attachment (Attachment.SaveAsFile) to a file, then open the file as a byte stream.
If you were using C++ or Delphi, you could use IAttach::OpenProperty(PR_ATTACH_DATA_BIN, IID_IStream, ..) to open the attachment as IStream COM object.
If using Redemption is an option (I am its author), it exposes the AsArray property on the Attachment and RDOAttachment objects.