I am using C# to process a message in my Outlook inbox that contains attachments. One of the attachments is of type olEmbeddeditem. I need to be able to process the contents of that attachment. From what I can tell I need to save the attachment to disk and use CreateItemFromTemplate which would return an object.
The issue I have is that an olEmbeddeditem can be any of the Outlook object types MailItem, ContactItem, MeetingItem, etc. How do you know which object type a particular olEmbeddeditem attachment is going to be so that you know the object that will be returned by CreateItemFromTemplate?
Alternatively, if there is a better way to get olEmbeddeditem attachment contents into an object for processing I'd be open to that too.
I found the following code on Google Groups for determining the type of an Outlook object:
Type t = SomeOutlookObject.GetType();
string messageClass = t.InvokeMember("MessageClass",
BindingFlags.Public |
BindingFlags.GetField |
BindingFlags.GetProperty,
null,
SomeOutlookObject,
new object[]{}).ToString();
Console.WriteLine("\tType: " + messageClass);
I don't know if that helps with an olEmbedded item, but it seems to identify regular messages, calendar items, etc.
Working with email attachments that are also emails which in turn contains user defined properties that I want to access, then I perform the following steps:
Outlook.Application mailApplication = new Outlook.Application();
Outlook.NameSpace mailNameSpace = mailApplication.GetNamespace(“mapi”);
// make sure it is an embedded item
If(myAttachment.Type == Outlook.OlAttachmentType.olEmbeddeditem)
{
myAttachment.Type.SaveAsFile(“temp.msg”);
Outlook.MailItem attachedEmail = (Outlook.MailItem)mailNameSpace.OpenSharedItem(“temp.msg”);
String customProperty = attachedEmail.PropertyAccessor.GetProperty(
“http://schemas.microsoft.com/mapi/string/{00020329-0000-0000-c000-000000000046}/myProp
}
If you open the MailItem using, then I will not have access to the properties as mentioned above:
Outlook.MailItem attachedEmail = (Outlook.MailItem)mailApplication.CreateFromTemplate(“temp.msg”);
Related
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 to parse a few .msg files which have trail mails. Is there any way to get the sub messages and identify the initiated and the responded emails.
I do not want to use any third party tools. I am allowed to use the Outlook interop.
Below is the code that I have used to read the msg file.I am able to get the Body ,HTMLBody and other details.But I actually need all the trailing messages.
outlook._Application app = null;
outlook.MailItem item = null;
outlook.NameSpace session = null;
try
{
app = new outlook.Application();
session = app.Session;
item = session.OpenSharedItem(file) as outlook.MailItem;
}
catch(Exception ex)
{ }
If you are limited to OOM only, the only way to do that is to save each embedded message attachment as an MSG file (Attachment.SaveAsFile), then open it using Namespace.OpenSharedItem.
If using Redemption (I am its author) is an option, an MSG file can be opened using RDOSession.GetMessageFromMsgFile (similar to Namespace.OpenSharedItem in OOM), and the embedded message attachment can be accessed using the RDOAttachment.EmbeddedMsg property (returns RDOMail object) - no need to save the attachment first.
In Outlook 2013, I want the content of the mail body in a new mail programmatically.
Below is my code:
void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
{
Outlook.MailItem mailItem = Inspector.CurrentItem as Outlook.MailItem;
Outlook.Application oApp = new Outlook.Application();
Outlook.Explorer oExplorer = oApp.ActiveExplorer();
Outlook.Selection oSelection = oExplorer.Selection;
foreach (object item in oSelection)
{
Outlook.MailItem mi = (Outlook.MailItem)item;
mailItem.HTMLBody = mi.HTMLBody;
}
}
Everything works fine, but the image present in the original mail is not displayed. Instead it shows something like cid:image002.png.
Not sure what is the reason.
Also I want to give it to the client, so I can't save mail content locally.
If you see cid:image002.png statements in the HTML markup of the message body you need to attach embedded items to new emails as well.
The basic principle of adding an embedded image is to attach the image to the item and then using the HTMLBody to write HTML to add the attachment cid as a reference in the HTML.
Attachment attachment = newMail.Attachments.Add(
#"E:\Pictures\image001.jpg"
, OlAttachmentType.olEmbeddeditem
, null
, "Some image display name"
);
string imageCid = "image001.jpg#123";
attachment.PropertyAccessor.SetProperty(
"http://schemas.microsoft.com/mapi/proptag/0x3712001E"
, imageCid
);
newMail.HTMLBody = String.Format(
"<body><img src=\"cid:{0}\"></body>"
, imageCid
);
Be aware, you will need to save the file on disk and the re-attach it to the new email. The Add method of the Attachment class accepts a file (represented by the full file system path with a file name) or an Outlook item that constitutes the attachment.
Also I'd recommend avoiding the foreach loop in the code with OOM objects. Use the for loop instead. It allows to release underlying COM objects instantly. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. You can read more about that in the Systematically Releasing Objects article.
As i understand you want to copy the attachments from existing mailitem to new one... Then you can try this
foreach( var x in mailItem.Attachments)
{
mi.Attachments.Add(x);
}
I am using COM API to compose an e-mail.
objOutlook = new Microsoft.Office.Interop.Outlook.Application();
mic = (Microsoft.Office.Interop.Outlook.MailItem)(objOutlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem));
mic.To = ccase.Email;
mic.CC = ccase.CC;
mic.Subject = ccase.Subject;
mic.Body = bodyBuffer.ToString();
// below line throws exception ?? Shouldn't it just use what is defined in outlook.
mic.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatUnspecified;
Question:
I want to define body to be plain Text, HTML, or Rich Text based upon what is defined in Outlook by the customer.
How can I retrieve the e-mail signature from Outlook and add it to the end of body ?
Any pointers will be appreciated.
Karephul
We decided not to do this as users might have configured multiple signatures. So, let users add signature in the end.
I am displaing one particular mail message. I wonder if i can change display preferences. I want to change options to display pictures embedded in mail body. I access to mail in that way:
Outlook.Application outLookApp = new Outlook.Application();
Outlook.Inspector inspector = outLookApp.ActiveInspector();
Outlook.NameSpace nameSpace = outLookApp.GetNamespace("MAPI");
Outlook.MAPIFolder inbox = nameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
Outlook.MailItem item = inbox.Items[1];
item.Display();
There might be a workaround to create something you are talking about with this email display. Like create your own displayer of the MailItem. But there is no way that I can find to turn it on and off. You can dig here to find out deeper features of the Outlook libraries. You might try to find the settings here and see if they give a way to change them.