'Construct Outlook Tasks and E-Mail Invites using C#
Our software supports using C# for scripting purposes. We can add Using and Assembly references to the software interface.
Our software can send HTML Body Mails or normal E-Mails.
The scenario as follows:
A scheduled task on the server runs over night, calls our scripts and send e-mails automatically
I have added the assembly reference Microsoft.Office.Interop.Outlook but I am struggling to find a way to send a task or calendar invite.
Appreciate any help.
Thank you
Solution 1:[1]
Typically you create a new mail/task/meeting object, set the Subject
property in order to identify it in my Inbox and then add the recipient to the Recipients
collection of the item. Then you check whether the recipient was resolved or not and, finally, send the message, for example:
private void CreateSendItem(Outlook._Application OutlookApp)
{
Outlook.MailItem mail = null;
Outlook.Recipients mailRecipients = null;
Outlook.Recipient mailRecipient = null;
try
{
mail = OutlookApp.CreateItem(Outlook.OlItemType.olMailItem)
as Outlook.MailItem;
mail.Subject = "A programatically generated e-mail";
mailRecipients = mail.Recipients;
mailRecipient = mailRecipients.Add("Eugene Astafiev");
mailRecipient.Resolve();
if (mailRecipient.Resolved)
{
mail.Send();
}
else
{
System.Windows.Forms.MessageBox.Show(
"There is no such record in your address book.");
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message,
"An exception is occured in the code of add-in.");
}
finally
{
if (mailRecipient != null) Marshal.ReleaseComObject(mailRecipient);
if (mailRecipients != null) Marshal.ReleaseComObject(mailRecipients);
if (mail != null) Marshal.ReleaseComObject(mail);
}
}
You may find the following articles helpful:
- How To: Create and send an Outlook message programmatically
- How To: Fill TO,CC and BCC fields in Outlook programmatically
- How To: Create a new Outlook Appointment item
- How To: Create a new Task item in Outlook
- How To: Create and send an Outlook message programmatically
Be aware, Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.
If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |