I wonder if it's possible with the .net framework or Microsoft.Office.Interop.Outlook to load an email message (*.msg), do a search and replace and send it from C#.
It's all happening on the server so Outlook cannot be installed.
What I've tried
the Redemption library but somehow it loses the images inlined in the template and can't figure out to remedy this
Using Microsoft.Office.Interop.Outlook
Application objOutlook = new Application();
objOutlook.CreateItemFromTemplate("c:\temp\..",)
But it expects as it second parameter an outlook folder, I can't give it a file path where it will save to
I'm thinking to switch to regular txt files instead of C# but maybe someone did this already
Update 1
This is the redemption code I tried. The problem is that the formatting and image (of a signature is not preserved)
using Interop.Redemption;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Replace(#"mailnonunicode.msg");
Replace(#"mailunicode.msg");
Replace(#"mailtemplate.oft");
}
static void Replace(string cTestharnessKmailMsg)
{
RDOSession rdoSession = new RDOSession();
RDOMail messageFromMsgFile = rdoSession.GetMessageFromMsgFile(cTestharnessKmailMsg);
messageFromMsgFile.Body = messageFromMsgFile.Body.Replace("abc",
"xyz");
messageFromMsgFile.Save();
}
}
}
Update 2 / Solution
If you want to preserve the formatting, you need to work with HTMLBody or RTFBody properties, not with the plain text Body.
What is your existing Redemption code?
If the message needs to be sent, it must be created in one of the Outlook folders - a standalone MSG file cannot be sent.
Related
I've a problem with my VSTO application for Outlook. I want to process the email body from a selected e-mail.
For selected e-mails out of the "default" list this code works fine:
Object selItem = Globals.ThisAddIn.Application.ActiveExplorer().Selection[1];
Outlook.MailItem mailItem = (Outlook.MailItem)selItem;
return mailItem.Body;
But if a user opens an email from the list with a double click, the email is displayed in a new window. If the addin is executed in this window (over the ribbon), the email from the list is still used (which is now in the background).
Is there a way to find out if the plugin was started in a separate window and then get the email body from it?
Regards,
Florian
Coincidentally, I just dealt with something similar to this. My situation isn't identical, but since I could easily piece together what it seems like you're looking for see below. I haven't tested this, and obviously you'll have to handle passing the correct reference to your Outlook Application, but since i had this immediately available I figured it would pass it along with the hope that you'll find it helpful.
private static void ribbonButton_Click(object sender, RibbonControlEventArgs e)
{
Outlook.Application application = new Outlook.Application();
Outlook.Inspector inspector = application.ActiveInspector();
if (application.ActiveExplorer().Selection[1] is Outlook.MailItem explorerMailItem)
{
// Write code to handle message if sourced from explorer (i.e., Reading Pane)
}
else if (inspector.CurrentItem is Outlook.MailItem inspectorMailItem)
{
// Write code to hanlde message if sourced from inspector
// (i.e., openened (double-clicked) message
}
}
When you double click on email item you open an inspector window and you can access it by using Application.ActiveInspector() method. The Inspector object has CurrentItem property which represents the opened item.
Also, you should avoid using multiple dots in expressions and properly release COM objects.
I'm trying to develop a snippet in C # code that enables the "voting option" function of Outlook.
This code will be used by a platform called Blue Prism.
The "vote" function of Outlook is in the Microsoft.Office.Interop.Outlook namespace, so I need to import it using C#, but I dont have enough knowledge to develop this.
I tried to do something like this but it is giving an error.
Here is the code:
public class program {
[DllImport(#"C:\Program Files\Blue Prism Limited\Blue Prism Automate\Microsoft.Office.Interop.Outlook.dll", EntryPoint = "VotingOptions")]
public static extern string Outlook(uint type);
static void Main()
{
// Create the Outlook application.
Outlook.Application oApp = new Outlook.Application();
oApp.VotingOption = "Yes; No";
}
}
So, can someone help me?
The VotingOptions property belongs to the MailItem class , not Outlook Application. Voting options on messages are used to give message recipients a list of choices and to track their responses. To create voting options programmatically, set a string that is a semicolon-delimited list of values for the VotingOptions property of a MailItem object. The values for the VotingOptions property will appear under the Vote command in the Respond group in the ribbon of the received message.
private void OrderPizza()
{
Outlook.MailItem mail = (Outlook.MailItem)Application.CreateItem(
Outlook.OlItemType.olMailItem);
mail.VotingOptions = “Cheese; Mushroom; Sausage; Combo; Veg Combo;”
mail.Subject = “Pizza Order”;
mail.Display(false);
}
Also you may find the C# app automates Outlook (CSAutomateOutlook) sample project helpful, it shows how to automate Outlook in C#.
Com objects aren't accessed through DLLImport. They're accessed using references. From the sample Eugene linked:
Create a Console application and reference the Outlook Primary Interop Assembly (PIA). To reference the Outlook PIA, right-click the project file and click the "Add Reference..." button. In the Add Reference dialog, navigate to the .NET tab, find Microsoft.Office.Interop.Outlook 12.0.0.0 and click OK.
Now you'll have access to the Microsoft.Office.Interop.Outlook object.
If you are using Blue Prism then rather than having to specify DLL references you may also chose to go the GetObject or CreateObject way, you will be able to interact with Outlook just like Blue Prism does with Excel. The drawback of this approach is that you have to use VB.NET (unless I am mistaken) and that you will not be able to use text representation of enum values (so for OlItemType you will not be able to use olMailItem but only its numeric value, which is 0).
Please note that Blue Prism has released a new version recently (6.3) and with it a new VBO for interaction with Outlook. It's nothing revolutionary, but it may provide some insight.
I am making a C# application (using Microsoft Visual Studio 2015, using WinForms) that needs to open a password protected .docx file in C#. I know you can do:
Process.Start("WINWORD.EXE", "filename.docx");
To start word (I'm using Microsoft Word 2016) with that file loaded in. But I also want to submit the password to Word so it opens without me doing anything. I know it's not this simple, but this is what imagining right now:
public void submitPassword(string password)
{
Process.Start("WINWORD.EXE", "filename.docx" //something like this: System.Password.Sumbit("WINWORD.EXE", password));
}
There is a COM type library called Microsoft Word Object Library. In Visual Studio, you can add a reference to it from the solution explorer. Right click References, click Add Reference, go to the COM tab, and search for Word.
With the package Microsoft.Office.Interop.Word, you can create an Application instance and tell it to open a document using a password.
using Microsoft.Office.Interop.Word;
...
public void SubmitPassword(string password)
{
Application app = new Application();
app.Documents.Open(FileName: #"filepath", PasswordDocument: password);
}
The answer provided to me by Andrew Piliser got me close, but not there. This is what works:
using Word = Microsoft.Office.Interop.Word;
...
public void submitPassword()
{
var wordApp = new Word.Application();
wordApp.Visible = true;
wordApp.Documents.Open(FileName: #"filepath", PasswordDocument: "filepassword");
}
following this link - change outlook MailItem icon
I managed to change my inbox icons.
Here's what I did step by step.
1) Created a custom message class for new mail that arrives from the Internet
The class is IPM.Note.Internet
Outlook.NameSpace outlookNameSpace;
Outlook.MAPIFolder inbox;
Outlook.Items items;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
outlookNameSpace = this.Application.GetNamespace("MAPI");
inbox = outlookNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
items = inbox.Items;
items.ItemAdd += new Outlook.ItemsEvents_ItemAddEventHandler(items_ItemAdd);
}
void items_ItemAdd(object Item)
{
Outlook.MailItem mailitem = (Outlook.MailItem)Item;
String EmailHeader = mailitem.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001E");
if (Item != null && EmailHeader.Contains("Look for a string in the headers here that we set for incomming mail") == true)
{
if (mailitem.MessageClass == "IPM.Note")
{
mailitem.MessageClass = "IPM.Note.Internet";
mailitem.Save();
}
}
}
2) Created a replacement Outlook Form Region matching the MessageClass. In this case I used IPM.Note.Internet
3) Assign the Icons in the Properties Pane of the Form Region Designer.
4) Debugged project and the next message that arrived from the internet was stamped with my custom icons after the message class was updated.
My issue now is that I can't preview or open the messages where I changed the message class. Similar to this post that's unanswered - Change Inbox-icons in Outlook at runtime
I think the issue is that my replacement Outlook Form Region is blank so the message is not able to be previewed.
If this is true than here's my question. What is the best way to export the standard IPM.Note message class template into visual Studio. I thing I need to overwrite my IPM.Note.Internet Outlook Form Region design.
There is an option when creating an Outlook Form Region-
To import an ".OFS" file. I was attempting to figure out how to export the file from the Outlook 2010 Client (Developer Tools) but I can't find a way to save the templates to that specific format. I can save to OFT (office template) but not .OFS
Thanks in advance for any help!
Rather then adding a form region and changing the message class I just ended up adding the PR_ICON_INDEX property and setting it's value. As outlined here in option #2 by Dmitry Link
There are many icons to choose from here. I couldn't locate a list with the integer values so I just entered random numbers for the PR_ICON_INDEX property in Outlook Spy changing the value till located the icon I wanted. There are many icons to choose from. Many from the 600-700 and 1000 and up range.
Here's the line I used to set the PR_ICON_INDEX property on the message-
mailitem.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x10800003", 4); // change the 4 to something like 600,601...etc to experiment
I'm having problems trying to send an XMPP message to a 'Room' in our OpenFire instance. The end result is for our CruiseControl.NET build server to be able to send success/failure messages to the appropriate 'Rooms' as an additional means of notification.
I'm using the Matrix XMPP library to create a Console Application in C# using VS2010. The idea was to create a simple .exe that I can wire up to CCNet and pass a few arguments into as required.
The code below is basically the sample code from the Matrix site/documentation which I have updated to point to a room.
static void Main(string[] args)
{
var xmppClient = new XmppClient
{
XmppDomain = "SERVER",
Username = "davidc",
Password = "*********"
};
xmppClient.OnRosterEnd += delegate
{
xmppClient.Send(new Message
{
To = "roomname#conference.SERVER",
From = "davidc#SERVER",
Type = MessageType.groupchat,
Body = "Just Testing the XMPP SDK"
});
};
xmppClient.Open();
Console.WriteLine("Press return key to exit the application");
Console.ReadLine();
xmppClient.Close();
}
I can send to an individual user (changing the To and Type accordingly) without any problems but changing the code to point to a room ends in silence! Is there some additional 'handshaking' that needs to be done to address a room?
I don't really have to use C# for the solution as long as it will run on a Windows Server.
You'll want to read XEP-0045, "Multi-User Chat". You need to enter the room before sending a message to it. For a quick fix, see section 7.1.1, which shows how to join a room using a simplified (older) protocol:
<presence
to='darkcave#chat.shakespeare.lit/thirdwitch'/>
For the newer protocol, include an extra x tag from section 7.1.2:
<presence
to='darkcave#chat.shakespeare.lit/thirdwitch'>
<x xmlns='http://jabber.org/protocol/muc'/>
</presence>
I don't know your library, but you'll want code something like:
xmppClient.Send(new Presence
{
To = "roomname#conference.SERVER/mynick",
});