简体   繁体   中英

Adding to Outlook Email Recipient using VSTO before sending email

Looking a bit of advise or direction in regards to VSTO Ribbons with Outlook in C#.

So far I've built an Outlook 2010 Ribbon (using TabMail), this Ribbon opens a WinForms window which allows my users to select contacts from a Custom Built Address Book from a SQL database, via a DataGridView.

The users basically select from a datagridview who they want to email, which then gets added to a toLine list.

Application app = new Microsoft.Office.Interop.Outlook.Application();
Mail item item = app.CreateItem((OlItemType.olMailItem));
item.To = toLine;
Item.Display();
This.close();

The downside of using this approach is the user has to build their To List before they actually compose their email.

I'm now trying to make use of TabMailNewMessage. This should allow the user to compose their email, then click the Ribbon icon within the new message and add to their To List from there.

I've got the icon showing okay in the TabMailNewMessage, and I've got it to open a 2nd Win Form [currently as a test].

I'm a little unsure how to add to the To List of an already open existing mailItem.

At present all I have on the 2nd Win Form is a button, can someone explain how I can click that button and simply add someone to the To List [of this already composed email]. (I don't have any code behind the button click as I'm not sure what to do)

I also need to make sure that it doesn't send the email, but simply adds the user to the To List.

Currently using Office 2010, and VS 2013 (with C#).

Hopefully I'm making some sense here.

Thanks

EDIT: Not sure if its a simple as

Application app = Globals.ThisAddIn.Application;
MailItem mi = (Outlook.MailItem)app.ActiveInspector().CurrentItem;
Mi.Recipients.Add(“joe@email.com”);
This.Close();

The MailItem.Recipients.Add method allows creating a new recipient in the Recipients collection. The name of the recipient can be a string representing the display name, the alias, or the full SMTP email address of the recipient.

using System.Runtime.InteropServices;
// ...
private bool AddRecipients(Outlook.MailItem mail)
{
    bool retValue = false;
    Outlook.Recipients recipients = null;
    Outlook.Recipient recipientTo = null;
    Outlook.Recipient recipientCC = null;
    Outlook.Recipient recipientBCC = null;
    try
    {
        recipients = mail.Recipients;
        // first, we remove all the recipients of the e-mail
        while(recipients.Count != 0)
        {
            recipients.Remove(1);                    
        }
        // now we add new recipietns to the e-mail
        recipientTo = recipients.Add("Eugene Astafiev");
        recipientTo.Type = (int)Outlook.OlMailRecipientType.olTo;
        recipientCC = recipients.Add("Somebody");
        recipientCC.Type = (int)Outlook.OlMailRecipientType.olCC;
        recipientBCC = recipients.Add("eugene.astafiev@somedomain.com");
        recipientBCC.Type = (int)Outlook.OlMailRecipientType.olBCC;
        retValue = recipients.ResolveAll();
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
    }
    finally
    {
        if (recipientBCC != null) Marshal.ReleaseComObject(recipientBCC);
        if (recipientCC != null) Marshal.ReleaseComObject(recipientCC);
        if (recipientTo != null) Marshal.ReleaseComObject(recipientTo);
        if (recipients != null) Marshal.ReleaseComObject(recipients);
    }
    return retValue;
}

You may find the following articles helpful:

Also, I'd suggest developing an Outlook form region which can be displayed on the same window instead of using a standalone window, see Create Outlook form regions for more information.

Yes, it is as simple as using Application.ActiveInspector.CurrentItem . But you can do better than that - your ribbon button event handler takes IRibbonControl object as a parameter. Cast the IRibbonControl.Context property to Inspector or Explorer (depending on where the button is hosted).

Also keep in mind that in a COM addin there is no reason to create a new instance of the Outlook.Application object (it will be crippled with the security prompts anyway) - use Globals.ThisAddIn.Application

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM