-4

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

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45

1 Answers1

0

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:


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.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Hi Eugene, thank you very much for your answer. When I tried applying the script I got a null reference, my code is as follows: Outlook._Application OutlookApp = null; Outlook.MailItem mail = null; Outlook.Recipients MailRec = null; mail = OutlookApp.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem; mail.Subject = "Test"; MailRec.Add("test@test.com"); mail.Send(); After debugging the Nullreference was raised on the following line: mail = OutlookApp.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem; Thank you very much for your support – NA_AbuAdam Apr 27 '22 at 06:49