Link to home
Start Free TrialLog in
Avatar of elmbrook
elmbrook

asked on

Interop Outlook Sending Mail from another mailbox

Hi

I have a number of mailboxes setup in Outlook.

I would like to use Interop to send an email but not from the default outlook account.

Lets say I have the following email boxes
1. Main email mailbox (default)
2. support mailbox
3. info mailbox
4. accounts mailbox

How do I go about sending an email using the accounts mailbox.

The below code send the email from the mail email box.  

// Create the Outlook application.
                Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();

                // Get the NameSpace and Logon information.
                Microsoft.Office.Interop.Outlook.NameSpace oNS = oApp.GetNamespace("mapi");

                // Log on by using a dialog box to choose the profile.
                oNS.Logon(Missing.Value, Missing.Value, true, true);

                // Create a new mail item.
                Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);

                // Set the subject.
                oMsg.Subject = _subject;

                // Set HTMLBody.
                oMsg.HTMLBody = _message;

                oMsg.To = _recipient;
                oMsg.CC = _cc;
                oMsg.BCC = _bcc;
                

                // Add a recipient.
                //Microsoft.Office.Interop.Outlook.Recipients oRecips = (Microsoft.Office.Interop.Outlook.Recipients)oMsg.Recipients;

                // TODO: Change the recipient in the next line if necessary.
                //Microsoft.Office.Interop.Outlook.Recipient oRecip = (Microsoft.Office.Interop.Outlook.Recipient)oRecips.Add(_recipient);
                //oRecip.Resolve();

                // Send.
                (oMsg as Microsoft.Office.Interop.Outlook._MailItem).Send();

                // Log off.
                oNS.Logoff();

                // Clean up.
                //oRecip = null;
                //oRecips = null;
                oMsg = null;
                oNS = null;
                oApp = null;

Open in new window


thanks
Avatar of teebon
teebon
Flag of Singapore image

Avatar of elmbrook
elmbrook

ASKER

Hi Teebon

I was just reviewing this article.

It works if it is another account e.g myname@hotmail.com however, if I want another mailbox (from exchange) they are not shown.

I have a number of exchange mailboxes (support, info, accounts) that I would like to send from.

Any other suggestions?
Hi Elmbrook,

1. Have you added the additional exchange mailbox into your outlook profile?
2. Are your main concern is on what email address the recipient sees in the "From" field? There is a different method to achieve this.
try using
.SentOnBehalfOfName = "Emailadress"
Hi Teebon/Fryezz

Yes I have added the exchange mailbox to my profile.

I am not concerned about what address the recipient sees in the from field although I do want it to be sent from accounts@mydomain.com.

The important aspect is that the email that I send is saved in the correct sent folder as I am not the only one that has the accounts mailbox. Any employee that has the accounts mailbox setup on their Outlook should be able to view the messages that are sent out.

Hence, I would like to send using the Accounts mailbox and have the emails stored in the Accounts mailbox Sent Items, not my personal one.
Hi elmbrook,

SendUsingAccount is not limited to hotmail. Can you try the following code?

// Create the Outlook application.
                Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();

Outlook.Account account = Application.Session.Accounts["accounts"];

                // Get the NameSpace and Logon information.
                Microsoft.Office.Interop.Outlook.NameSpace oNS = oApp.GetNamespace("mapi");

                // Log on by using a dialog box to choose the profile.
                oNS.Logon(Missing.Value, Missing.Value, true, true);

                // Create a new mail item.
                Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);

                // Set the subject.
                oMsg.Subject = _subject;

                // Set HTMLBody.
                oMsg.HTMLBody = _message;

                oMsg.To = _recipient;
                oMsg.CC = _cc;
                oMsg.BCC = _bcc;
                

                oMsg.SendUsingAccount = account;


                // Add a recipient.
                //Microsoft.Office.Interop.Outlook.Recipients oRecips = (Microsoft.Office.Interop.Outlook.Recipients)oMsg.Recipients;

                // TODO: Change the recipient in the next line if necessary.
                //Microsoft.Office.Interop.Outlook.Recipient oRecip = (Microsoft.Office.Interop.Outlook.Recipient)oRecips.Add(_recipient);
                //oRecip.Resolve();

                // Send.
                (oMsg as Microsoft.Office.Interop.Outlook._MailItem).Send();

                // Log off.
                oNS.Logoff();

                // Clean up.
                //oRecip = null;
                //oRecips = null;
                oMsg = null;
                oNS = null;
                oApp = null;
                                

Open in new window

Hi Teebon

Outlook.Account account = Application.Session.Accounts["accounts"]; = null

That account is not found.

The code below has all the accounts available to me:

this includes mypersonal mailbox, and my other pop email accounts.
It does NOT include my accounts, support and info mailboxes that I have added.

 if (accountname.Trim().Length != 0)
                {
                    Microsoft.Office.Interop.Outlook.Accounts accounts = oMsg.Session.Accounts;
                    for (int i = 1; i <= accounts.Count; i++)
                    {
                        account = accounts[i];
                        string accountfound = account.DisplayName.ToLower();
                        MessageBox.Show(accountfound);
                    }
                }

Open in new window


The accounts, support and info mail accounts I added to outlook through account settings, select my mailbox, more settings, Advanced tab, click the add button (under open these additional mailboxes)
Hi elmbrook,

May I know which version of MS Outlook are you using?
2013
ok, I am getting somewhere.

Rather than adding the mailbox through add accounts in Outlook, I added it using Mail in control panel and that must create a separate mailbox or something as I can now see the account.

Now when I send the email it gets stuck in my mailbox outbox as the sender is still my personal account and not my accounts account.

Any more tips?
Hi elmbrook,

Instead of adding additional mailbox, please add the exchange account following here
Hi elmbrook,

Once you added, please check if the "Accounts" pst appears in your outlook left panel. If not, you may open it with File > Open > Open Outlook Data File
From the Technet link "Individual users can add more Exchange accounts to their Outlook profiles within Outlook 2013. To do so, users can click the File tab, click Account Settings, and then click Add Account, or use the Windows Control Panel Mail module." I created my account using the Windows Control Panel Mail module

When opening Outlook, there is an account in the left hand panel called accounts@mydomain.com

when I right click and open file location i opens C:\Users\myprofile\AppData\Local\Microsoft\Outlook\accounts@mydomain.com.ost which is an ost not a pst file.

I can send an email from Outlook using the account without any problems.

Using the C# code I can send an email through that mailbox but it still defaults to my exchange credentials.

If I send one using a hotmail/gmail pop account it works.
Hi elmbrook,

ost is correct for exchange, excuse me for typo.
Can I understand that now once it is sent the mail resides in "Accounts" Sent Items?
Hi Teebon

That is incorrect.

It is using the "Accounts" mailbox to send, however, the message gets stuck in the Outbox.

The email sender address is my mailbox credentials (myname@mydomain.com) not the accounts@mydomain.com email address.
ASKER CERTIFIED SOLUTION
Avatar of teebon
teebon
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The reason it was not working was that the mailbox was added as an Exchange mailbox, but I also had permissions to add it to my profile which was adding it to Outlook automatically.

This was then giving me the problem of sending it using myprofile@mydomain.com.

In the end I had to remove the permissions from exchange to access that mailbox and add the mailbox using exchange.

Thank you for the perseverance Teebon. Well worth the 500 points!
Glad that it works for you!