Solved

Interop Outlook Sending Mail from another mailbox

Posted on 2014-11-24
18
1,378 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
 
LVL 9

Expert Comment

by:teebon
ID: 40463729
0
 

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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 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