Creating Outlook appointment using ICal - c#

I have written some code in c# that creates an ical file and attaches the file to an Outlook email. Is there a way to create an appointment that appears directly in Outlook calendar as accepted instead of a user having to open the attachment and accept it?
Thank you for help.

If you're talking about sending an appointment to someone else and getting it added to their calendar, I'm pretty sure that's not possible. At least, I hope that's not possible.
If you're talking about adding an appointment to your own calendar, you can use the Outlook automation objects for this. Add a reference to the Microsoft Outlook Object Library, and then do something like this:
using System;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Outlook.Application ol= new Outlook.Application();
Outlook.AppointmentItem cal = ol.CreateItem(Outlook.OlItemType.olAppointmentItem);
cal.Start = DateTime.Now;
cal.End = DateTime.Now.AddMinutes(30);
cal.Subject = "Test";
cal.Save();
}
}
}

Related

Saving html message in Outlook 2019 with .NET C#

Using .NET C# i want to load an Outlook html message from *.msg file, add a recipient and save it to standard draft folder.
I cannot do it properly with Outlook 2019 (not 2016 or 2013), because after saved, it turns the message body format to plain text. This happen with 2019 version only.
In the code example i first create the email and save it to draft. Until the COM Application object is instantiated the format remain html. Right after i had manually opened Outlook.exe the message has been turned to plain text. I check this with the function PrintBodyFormat. Please note this happen with Office 2019 only.
using Debug = System.Diagnostics.Debug;
using Outlook = Microsoft.Office.Interop.Outlook;
static void CreateMail()
{
Outlook.Application app = new Outlook.Application();
Outlook.MailItem mail = app.CreateItemFromTemplate(#"C:\html_message.msg");
mail.Recipients.Add("johndoe#foobar.com");
Debug.WriteLine(mail.BodyFormat.ToString());
//OUTPUT WITH ALL OUTLOOK VERSION: "olFormatHTML"
mail.Save();
mail.Close(Outlook.OlInspectorClose.olDiscard);
System.Runtime.InteropServices.Marshal.ReleaseComObject(mail);
mail = null;
Outlook.NameSpace nms = app.GetNamespace("MAPI");
Outlook.MAPIFolder DraftFolder = nms.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderDrafts);
mail = DraftFolder.Items[1];
Debug.WriteLine(mail.BodyFormat.ToString());
//OUTPUT WITH ALL OUTLOOK VERSION: "olFormatHTML"
mail.Close(Outlook.OlInspectorClose.olDiscard);
System.Runtime.InteropServices.Marshal.ReleaseComObject(mail);
mail = null;
app.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(DraftFolder);
System.Runtime.InteropServices.Marshal.ReleaseComObject(nms);
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
}
//Run this after manually opened Outlook.exe
static void PrintBodyFormat()
{
Outlook.Application app = new Outlook.Application();
Outlook.NameSpace nms = app.GetNamespace("MAPI");
Outlook.MAPIFolder DraftFolder = nms.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderDrafts);
Outlook.MailItem mail = DraftFolder.Items[1];
Debug.WriteLine(mail.BodyFormat.ToString());
//OUTPUT WITH OUTLOOK 2016 OR EARLIER: "olFormatHTML"
//OUTPUT WITH OUTLOOK 2019: "olFormatPlain"
app.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(mail);
System.Runtime.InteropServices.Marshal.ReleaseComObject(DraftFolder);
System.Runtime.InteropServices.Marshal.ReleaseComObject(nms);
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
}
Do not use mail.Close(Outlook.OlInspectorClose.olDiscard); - you never showed the inspector, so there is no reason to close it.
Also, Marshal.ReleaseComObject won't do much since you never release the Recipients collection and the Recipient object returned by Recipients.Add - both of them keep a reference to the parent message, and you end up with two implicit variables that you never release.
Do not use DraftFolder.Items[1] - save the value of MailItem.EntryID in a variable after calling Save, then use it to reopen the message using Namespace.GetItemFromID.

DLLImportAttribute C# - Outlook

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.

Including original message in VSTO Outlook addin reply

Is there an easy way to programatically create a reply message in a VSTO Outlook addin that includes the original message, with the same look that occurs when clicking on the built-in reply button in Outlook? Or does one need to write code to retrieve the original email properties & text, and format it such that it looks as if the reply was composed by Outlook?
How about:
using System.Runtime.InteropServices;
using Outlook = Microsoft.Office.Interop.Outlook;
private void Reply(Outlook._MailItem mailItem)
{
Outlook.Actions actions = mailItem.Actions;
Outlook.Action action = actions["Reply"];
Marshal.ReleaseComObject(actions);
action.ReplyStyle = Outlook.OlActionReplyStyle.olIncludeOriginalText;
Outlook._MailItem response = action.Execute() as Outlook.MailItem;
Marshal.ReleaseComObject(action);
response.Display();
Marshal.ReleaseComObject(response);
}

Outlook Add-In and Windows Form collaboration

I'm developing an outlook add in that collaborates with SharePoint. I've added a new item to the Outlook add in: a Windows Form. When a button is being clicked in the Windows Form, I want to perform an action with Outlook, like this:
Outlook.NameSpace outlookNameSpace = this.Application.GetNamespace("MAPI");
This isn't working, because I'm in the Windows Form instead of in the Outlook Add In. I'm not sure how to let those though collaborate.
The Outlook Application object passed to the add-in during initialization is already intrinsically declared in the ThisAddIn class within your VSTO project. You can access it from any other item in the project using Globals.ThisAddIn.Application. You must use this object and not a new Outloook Application object because it won't be fully trusted as it wasn't passed to the add-in initialization by Outlook.
Add a new parameter to your WinForm constructor, to receive a reference to the Outlook Application object:
using Outlook = Microsoft.Office.Interop.Outlook;
.
.
.
public partial class MyWinForm : Form
{
private Outlook.Application m_OutlookApp;
public MyWinForm(Outlook.Application OutlookApp)
{
m_OutlookApp = OutlookApp;
InitializeComponent();
}
Your add-in code to launch the form then becomes something like:
MyWinForm myWinForm = new MyWinForm(Application);
myWinForm.Show();
You can then use the Outlook Application within the WinForm:
private void button1_Click(object sender, EventArgs e)
{
Outlook.NameSpace outlookNameSpace = m_OutlookApp.GetNamespace("MAPI");
.
.
.
}
I think you can use something like this below,
var output = Globals.YourAddin.Application.GetNamespace("MAPI");
Hope this helps.

Use an Outlook message file (msg) as a template

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.

Categories

Resources