none
MailItem send from specific account RRS feed

  • Question

  • Hi,

    I have a profile with several different emails. One account is the target account if the body of the email contains an identifier. So if I start composing an email from account A, and the email contains an identifier that signals the email must be sent from account B, how do I send the email using account B, and have it show up in the sent items folder of account B? I tried getting the account and setting the MailItem account (put_SendUsingAccount(spAccount)), but this does not work totally. If I look using Outlook Spy, the account is correct (account B) in the sent items folder of the wrong account (account A). But the sender properties are still referring to account A. Do I have to set all of the sender properties to get this to show up in the proper account for Sent Items?

    So here is what should happen.

    1. Compose email in account A

    2. Body contains item that specifies Account B.

    3. Send email from account B.

    4. Email should be in Sent Items for Account B and have all the proper account information.

    I know I could easily copy the email to the proper folder and set all the properties. But I was looking for a way to tell Outlook to do all that junk, :-)

    Any thoughts?

    Thanks,
    Tom

    Tuesday, June 19, 2018 10:03 PM

Answers

  • Does it work outside of the ItemSend event handler? Did you try to run the sample code in the button's Click event?


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    • Marked as answer by Thomas Lee3 Wednesday, June 20, 2018 5:52 PM
    Wednesday, June 20, 2018 8:28 AM

All replies

  • Hello Tom,

    What code do you have now?

    You need to set the SendUsingAccount property of the MailItem class to an Account object that represents the account under which the MailItem is to be sent. 

    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 ("someone@example.com") 
     oMail.Recipients.ResolveAll 
     oMail.SendUsingAccount = oAccount 
     oMail.Send 
     End If 
     Next 
    End Sub

    Also you may find the How to specify account for an outgoing Outlook message article with a sample code helpful. 


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Tuesday, June 19, 2018 10:48 PM
  • Hi Eugene,

    Thanks for the reply. I am currently handling the email in the _MailItem  send event. Perhaps I have to handle that before the send event?

    	// Get MAPI session
    	CComPtr<Outlook::_NameSpace> spNS;
    	m_spMailItem->get_Session(&spNS);
    
    	CComPtr<Outlook::_Accounts> spAccounts;
    	HRESULT hr = spNS->get_Accounts(&spAccounts);
    	if (SUCCEEDED(hr) && spAccounts)
    	{
    		long lNumberOfAccounts = 0L;
    		spAccounts->get_Count(&lNumberOfAccounts);
    		for (long i = 1L; i <= lNumberOfAccounts; i++)
    		{
    			CComPtr<Outlook::_Account> spAccount;
    			hr = spAccounts->Item(_variant_t(i), &spAccount);
    			if (SUCCEEDED(hr) && spAccount)
    			{
    				CComPtr<Outlook::_Store> spStore;
    				hr = spAccount->get_DeliveryStore(&spStore);
    				if (SUCCEEDED(hr) && spStore)
    				{
    					BSTR bstrStoreId = nullptr;
    					spStore->get_StoreID(&bstrStoreId);
    					if(bstrStoreId)
    					{
    						std::wstring wsStoreEntryId(bstrStoreId, ::SysStringLen(bstrStoreId));
    						::SysFreeString(bstrStoreId);
    
    						CAccountInfo acct;
    						if (g_profileAccounts.GetAccountFromStoreId(wsStoreEntryId, acct))
    						{
    							if (acct.m_lIncomingPort == lConfiguredPort)
    							{
    								hr  = m_spMailItem->put_SendUsingAccount(spAccount);
    			
    								return;
    							}
    						}
    					}
    				}
    			}
    

    Wednesday, June 20, 2018 12:36 AM
  • Does it work outside of the ItemSend event handler? Did you try to run the sample code in the button's Click event?


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    • Marked as answer by Thomas Lee3 Wednesday, June 20, 2018 5:52 PM
    Wednesday, June 20, 2018 8:28 AM
  • Hi Eugene,

    I finally got a chance to move things around in my code. After I moved the logic out of the send event it worked.

    Doing this in the send event did cause the account to change, but the SenderEmailAddress, SenderEmailType and SenderName did not change. 

    So I guess it is too late in the process to try to change accounts in the send event. To bad we do not have a before send event.

    Anyway, thanks for your input on this.

    Wednesday, June 20, 2018 5:52 PM
  • Any chance you can share "how" you fixed it?   I have been working this for probably over 40 hours of time for this one function (SendUsingAccount).  No matter what I do it does not work.  Any insight would be great.

    Bryan

    Friday, November 30, 2018 8:08 PM
  • Hi Bryan,

    Happy holidays to you!

    Yep, I wasted a lot of time with this function too. The particular add-in was pretty complicated and had several modes of operation

    One way I got around it was to capture the property change event (sendusingaccount) when in simple mapi mode. The other way was I captured the address selection from the dropdown (from).

    I am not sure what type of add-in software you are using, but your best bet would be to look at the property change events. I cannot remember if I got the "sendusingaccount" in the property change  event for non-simple mapi.

    The key is to do something before the send event. I would definitely check the property change events, because there are a lot of notifications that come through that specific event.

    Friday, November 30, 2018 9:30 PM