Solved

Interop Outlook Sending Mail from another mailbox

Posted on 2014-11-24
18
1,089 Views
Last Modified: 2014-11-30
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
Comment
Question by:elmbrook
  • 9
  • 8
18 Comments
 
LVL 9

Expert Comment

by:teebon
Comment Utility
0
 

Author Comment

by:elmbrook
Comment Utility
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
 
LVL 9

Expert Comment

by:teebon
Comment Utility
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
 
LVL 12

Expert Comment

by:FarWest
Comment Utility
try using
.SentOnBehalfOfName = "Emailadress"
0
 

Author Comment

by:elmbrook
Comment Utility
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
 
LVL 9

Expert Comment

by:teebon
Comment Utility
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
 

Author Comment

by:elmbrook
Comment Utility
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
 
LVL 9

Expert Comment

by:teebon
Comment Utility
Hi elmbrook,

May I know which version of MS Outlook are you using?
0
 

Author Comment

by:elmbrook
Comment Utility
2013
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:elmbrook
Comment Utility
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
 
LVL 9

Expert Comment

by:teebon
Comment Utility
Hi elmbrook,

Instead of adding additional mailbox, please add the exchange account following here
0
 
LVL 9

Expert Comment

by:teebon
Comment Utility
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
 

Author Comment

by:elmbrook
Comment Utility
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
 
LVL 9

Expert Comment

by:teebon
Comment Utility
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
 

Author Comment

by:elmbrook
Comment Utility
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
 
LVL 9

Accepted Solution

by:
teebon earned 500 total points
Comment Utility
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
 

Author Closing Comment

by:elmbrook
Comment Utility
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
 
LVL 9

Expert Comment

by:teebon
Comment Utility
Glad that it works for you!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now