Creating a MailItem and setting a different Sender/From than the default Account before displaying it RRS feed

  • Question

  • Hello,

    We are using OLE / MAPI to connect a custom application with outlook to create customized emails.

    Until then (until outlook 2007), we were always using the SentOnBehalfOf property of MailItem to "set" the sender to a group e-mail adress, instead of the e-mail adress of the user. However, this cause the sent e-mail to be then put in the default Sent Items folder, of the default personal user account.

    Please note, we are creating a new email, populating various fields like the TO, CC, etc... fields, and then displaying it so that the user can add more text to the body. We tried setting the from field using the SendUsingAccount property in Outlook 2007, without success, but the MSDN seems to say this only works when calling the Send method and not Display:

    The SendUsingAccount property can be used to specify the account that should be used to send the MailItem when the Send method is called. This property returns Null (Nothing in Visual Basic) if the account specified for the MailItem no longer exists.

    (And as I said, we are using Display to let the user edit the body before sending)

    Now, in Outlook 2010 there is the new Sender property that can be set. There is even a code sample on MSDN:

    In the first method below, CreateMailItemFromAccount first identifies the appropriate account by matching the store of the current folder (obtained from the Store property) with the default delivery store of each account (obtained with the DeliveryStore property) that is defined in the Accounts collection for the session. CreateMailItemFromAccount then creates the MailItem. To associate the item with the account, CreateMailItemFromAccount assigns the user of the account as the sender of the item by setting the account.CurrentUser.AddressEntry property to the Sender property of the MailItem. Assigning the Sender property is the important step; if you do not specify the sender, the MailItem is created for the primary account by default. At the end of the method, CreateMailItemFromAccount displays the MailItem. Note that if the current folder is not on a delivery store, CreateMailItemFromAccount creates the MailItem for the primary account for the session.

    But this example is not working for us. For some reason, setting the Sender to the group account User Address Entry has no effect, and it is still the personal email of the user that is show in the From field of the created e-mail.

    It seems to be the same as a thread on here described here:

    But I must say I don't understand the last posts, and am not sure if this can be solved using this method, or that the problem really is the same.

    Here is our code fwiw (slightly edited but you should get the idea):

          Variant m_session;
          Variant m_application;
          Variant m_namespace;
          m_application = Variant::CreateObject("Outlook.Application");
          m_namespace = m_application.OleFunction("GetNameSpace" , "MAPI");
          m_session = m_application.OlePropertyGet("Session");      
          //0 = MailItem
          Variant doc = m_application.OleFunction("CreateItem", "0");
          doc.OlePropertySet("To", "example_AT_example_DOT_com");
          doc.OlePropertySet("CC", "example_AT_example_DOT_com");
          doc.OlePropertySet("BCC", "example_AT_example_DOT_com");
          doc.OlePropertySet("Subject", "example");
          AnsiString emailFrom = "groupEmail_AT_example_DOT_com";
          Variant accounts = m_session.OlePropertyGet("Accounts");
          int accountNb = accounts.OlePropertyGet("Count");
          Variant currentAccount;
          AnsiString currentEmail;
          Variant idx;
          bool accFound = false;
          for(int i=1; i<=accountNb; i++)
              idx = i;  
              currentAccount = accounts.OleFunction("Item", idx);
              currentEmail = currentAccount.OlePropertyGet("SmtpAddress");
              if(currentEmail.LowerCase() == emailFrom.LowerCase())
                  accFound = true;
          //if a matching account is found, try to send the e-mail using another e-mail address
              Variant currentRecipient;
              Variant senderAccountAddressEntry;
              currentRecipient = currentAccount.OlePropertyGet("CurrentUser");
              senderAccountAddressEntry = currentRecipient.OlePropertyGet("AddressEntry");
              doc.OlePropertySet("Sender", senderAccountAddressEntry);
              //doesn't work either:
              //doc.OlePropertySet("SendUsingAccount", currentAccount);
          //clean up
          doc = Unassigned;
          m_session     = Unassigned;
          m_namespace   = Unassigned;
          m_application = Unassigned;

    Any help warmly welcome!



    Wednesday, June 19, 2013 8:07 AM

All replies

  • What doesn't work with SendUsingAccount? It should work if you display an item and then the user sends that item.

    Here is some VBA sample code designed for the Outlook VBA project. I only changed the sample from the Object Browser Help for SendUsingAccount to display and not send the items. As written this code opens a mail item for each POP3 account the user has configured.

    Sub SendUsingAccount()
        Dim oAccount As Outlook.Account
        For Each oAccount In Application.Session.Accounts
            If oAccount.AccountType = olPop3 Then
                Dim oMail As Outlook.MailItem
                Set oMail = Application.CreateItem(olMailItem)
                oMail.Subject = "Sent using POP3 Account"
                oMail.Recipients.Add ("")
                oMail.SendUsingAccount = oAccount
            End If
    End Sub

    Ken Slovak MVP - Outlook

    Wednesday, June 19, 2013 1:58 PM
  • Hi Ken,

    thanks a lot for your reply.

    SendUsingAccount seems to have no effect, as when the email is display to the user, it's still the user own personal e-mail/name that is shown in the from field. Instead I would expect to see the e-mail/name of the account set with SendUsingAccount. So for example '' instead of ''. I would also like that this email appears in the sent items of the sales account instead of the employee's own account sent item.

    After you posted your example, I checked something I hadn't thought of before - the accounts used are not of type olPop3, but are exchange accounts (olExchange). Does this play any role here?

    Also after posting this morning, I kept on searching the forums, and found an older threads I hadn't yesterday, where you say that the only way to update the window would be to save the item. Is that what's missing here?

    And what do you mean (in another thread) by "outlook caches the stale data and only refreshes after an object is completely released and reopened". Is there a way to release the mailItem using the object model through OLE? (by saving it or something else?)



    Wednesday, June 19, 2013 2:25 PM
  • The accounts being Exchange accounts is a major piece of information. You need to have SendAs permissions to send as another mailbox than the logged in mailbox.

    What I do when I need to send an email from my sales or support mailboxes (as examples) instead of from my own mailbox is to open the other mailboxes as part of my Outlook profile and set SendAs permissions for my Exchange account on those other mailboxes.

    In your setup, can the user send using any other accounts from the user interface?

    What was meant in those other threads applies mostly to using MAPI or Redmption code. In those cases if you update or change an existing item opened by Outlook, Outlook doesn't know about any changes you made, so stale data is displayed.

    For that sort of thing you somehow need to notify Outlook that something has changed. So for example in Redemption code we often write something like this, where oMail is a changed mail item:

    oMail.Subject = oMail.Subject


    That fools Outlook into marking the item as dirty and when the item is saved Outlook updates it's cache of the item.

    Ken Slovak MVP - Outlook

    Wednesday, June 19, 2013 3:05 PM
  • Well I suppose the SendAs permissions are set, because sending an e-mail manually using another that other account works without a flaw. It only doesn't using Ole to pre-generate the e-mail and display it :( Once it is displayed, we can change the "From" manually using the drop-down, and then it does do what we want.

    So for now the only workaround is using SentOnBehalfOfName + ReplyTo, but this unfortunately doesn't put the sent e-mail in the correct Sent Items folder.



    • Edited by Karim_Ben Thursday, June 20, 2013 7:55 AM new text
    Thursday, June 20, 2013 7:49 AM