Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1983
  • Last Modified:

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
0
elmbrook
Asked:
elmbrook
  • 9
  • 8
1 Solution
 
elmbrookAuthor Commented:
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?
0
 
teebonProduct ManagerCommented:
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
FarWestCommented:
try using
.SentOnBehalfOfName = "Emailadress"
0
 
elmbrookAuthor Commented:
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.
0
 
teebonProduct ManagerCommented:
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

0
 
elmbrookAuthor Commented:
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)
0
 
teebonProduct ManagerCommented:
Hi elmbrook,

May I know which version of MS Outlook are you using?
0
 
elmbrookAuthor Commented:
2013
0
 
elmbrookAuthor Commented:
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?
0
 
teebonProduct ManagerCommented:
Hi elmbrook,

Instead of adding additional mailbox, please add the exchange account following here
0
 
teebonProduct ManagerCommented:
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
0
 
elmbrookAuthor Commented:
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.
0
 
teebonProduct ManagerCommented:
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?
0
 
elmbrookAuthor Commented:
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.
0
 
teebonProduct ManagerCommented:
Hi elmbrook,

Did you remove the additional "accounts" mailbox from your personal mailbox? Is your credentials for "accounts" setup correctly?

If it still not working, I would suggest you recreate your profile and adding your exchange accounts again from the Control Panel Mail setup.

I have just tested the following code with 2 Exchange accounts in my outlook and was able to send it out thru my second Exchange account without problem.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Outlook;
using System.Reflection;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

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

            Account account = oApp.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 = "test";

            // Set HTMLBody.
            oMsg.HTMLBody = "test";

            oMsg.To = "test@gmail.com";
            //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

0
 
elmbrookAuthor Commented:
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!
0
 
teebonProduct ManagerCommented:
Glad that it works for you!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now