Display options for email - c#

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.

Related

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);

Copying mail body to new mail in outlook by coding in c#

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);
}

C# - Mail confirmation Outlook

I am developing an app that checks an Outlook mail account, finds all the attachments and then print them out. At this point, the mails analyzed are moved to another folder.
I have only one problem: sometimes, I receive some mails with reading confirmation. The app checks the attachment, and when it has to move the mail, it freezes. Then a popup appear in Outlook, about sending or not sending the reading confirmation.
Now, I want to make this programmatically, I always want to send a reading confirmation when it is requested.
I found a property (ReadReceiptRequested), set to true if there is a reading confirmation to send, but I don't know how to send it.
Here a piece of the code I use:
//I store all the emails in a List<Outlook.MailItem> named emails
Outlook.Application myApp = new Outlook.Application();
Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
//Check if the mail has read confirmation
if (emails[right_index].ReadReceiptRequested)
{
//How to send read confirmation?
}
//I read the mail, then I move it to another folder
emails[indice_scelto].UnRead = false;
emails[indice_scelto].Move(mapiNameSpace.Folders["New folder"]);
Could you help me?
Thanks in advance!
You can do that on the Extended MAPI level (C++ or Delphi) - call IMessage::SetReadFlag() - pass 0 to send read receipts or SUPPRESS_RECEIPT otherwise.
If Redemption is an option (I am its author), it exposes the RDOMail.MarkRead method that takes a SuppressReceipt Boolean parameter.
The Outlook object model doesn't provide any property or method. All you can is to set the UnRead property to false and Save the item.
Then you can begin synchronizing a user's folders using the specified Send\Receive group using the Start method of the SyncObject class. The Namespace class provides the SyncObjects property which returns a SyncObjects collection containing all Send\Receive groups. For example:
Public Sub Sync()
Dim nsp As Outlook.NameSpace
Dim sycs As Outlook.SyncObjects
Dim syc As Outlook.SyncObject
Dim i As Integer
Dim strPrompt As Integer
Set nsp = Application.GetNamespace("MAPI")
Set sycs = nsp.SyncObjects
For i = 1 To sycs.Count
Set syc = sycs.Item(i)
strPrompt = MsgBox( _
"Do you wish to synchronize " & syc.Name &"?", vbYesNo)
If strPrompt = vbYes Then
syc.Start
End If
Next
End Sub
I suppose then you can move the item wherever you need.

How to open a Outlook mail item in a composing state?

How to open a MailItem using Outlook Interop dll.
This is the code I am using:
Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook._MailItem mailItm = (Microsoft.Office.Interop.Outlook._MailItem)app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
mailItm.To = "xxx#xxx.com";
mailItm.CC = "yyy#yyy.com";
mailItm.Subject = "Some Subject";
mailItem.Send() is used to send a mail. But I want to open a mail item in a composed state. How to do that?
You only have to Display the item:
mailItem.Display(false);
(the false here indicates that the Inspector shouldn't be modal)

Using CreateItemFromTemplate to process an olEmbeddeditem Outlook attachment

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”);

Categories

Resources