Search code examples
outlooklate-bindingsender

How to change Sender in late binding Outlook Mail from c#


I want to send an eMail through Outlook by using late binding. It works fine so far but i want to use an other "FROM" adress, not the one my outlook-account uses. Does anyone know how to do this?

By trying early binding i found some properties "Sender", "SenderEmailAdress" ... but nothing works for me or i'm doing it wrong ... (i think so)

This is my code for creating the mail and open outlook.

        try
        {
            CreateObject co = new CreateObject("Outlook.Application");
            object[] parms = new object[1];
            parms[0] = 0;
            object mailitem = co.Execute("CreateItem", parms);

            // define TO
            object recipients = mailitem.GetType().InvokeMember("Recipients", BindingFlags.GetProperty, null, mailitem, null);
            object[] address = new object[1];
            address[0] = toAdress;
            recipients.GetType().InvokeMember("Add", BindingFlags.InvokeMethod, null, recipients, address);

            // define subject
            parms[0] = subject;
            mailitem.GetType().InvokeMember("Subject", BindingFlags.SetProperty, null, mailitem, parms);

            // define MailText
            parms[0] = mailText;
            mailitem.GetType().InvokeMember("Body", BindingFlags.SetProperty, null, mailitem, parms);

            // open Outlook to send manually
            mailitem.GetType().InvokeMember("Display", BindingFlags.InvokeMethod, null, mailitem, null);
        }
        catch (Exception ex)
        {

            throw;
        }

Solution

  • You can only alter the SentOnBehalfOfName property; Sender and SenderEmailAddress are read-only properties. If you need to send using a different sender than you need to set the SendUsingAccount property to an Account object representing the account configured for that sender.