Solved

Interop Outlook Sending Mail from another mailbox

Posted on 2014-11-24
18
1,509 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
18 Comments
 

Author Comment

by:elmbrook
ID: 40463810
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
ID: 40463835
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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 12

Expert Comment

by:FarWest
ID: 40463962
try using
.SentOnBehalfOfName = "Emailadress"
0
 

Author Comment

by:elmbrook
ID: 40465423
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
ID: 40465981
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
ID: 40466029
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
ID: 40466040
Hi elmbrook,

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

Author Comment

by:elmbrook
ID: 40466051
2013
0
 

Author Comment

by:elmbrook
ID: 40466054
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
ID: 40466056
Hi elmbrook,

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

Expert Comment

by:teebon
ID: 40466060
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
ID: 40466079
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
ID: 40466082
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
ID: 40468115
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
ID: 40468386
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
ID: 40472782
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
ID: 40472927
Glad that it works for you!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

707 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