Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
I am writing an application that will automate the sending of E-Mails from a specific mailbox within the account available on Outlook. Currently I am able to get the accounts available by doing:


C#
Outlook.Application application = new Outlook.Application();
Outlook.Accounts accounts = application.Session.Accounts;



C#
I can then iterate through the available accounts by doing:

foreach (Outlook.Account account in accounts)
{
    Console.WriteLine(account.DisplayName);
}

My question is: How do I access the mailboxes within an Exchange Account in the list. Lets say the first element in the accounts list.

I have read a few other questions on accessing the Inbox folder contents of a mailbox, but I am looking to send an email item I later create using the chosen mailbox in the "From" field.

Thanks for any help.


What I have tried:

Outlook.Application application = new Outlook.Application();
Outlook.Accounts accounts = application.Session.Accounts;

I can then iterate through the available accounts by doing:

foreach (Outlook.Account account in accounts)
{
Console.WriteLine(account.DisplayName);
}
Posted
Updated 28-Mar-23 5:24am

The code you have is only working because you are developing on your local machine, when you publish to a server it will stop working. Outlook is a desktop application that is not suitable for server-side automation due to numerous reasons, and its automation isn't supported by Microsoft. If you want to access mail data then use POP3 or IMAP and SMTP to send. If the account is held in Exchange then use the Exchange Web Services to interact with it.

You will not solve your problems by trying to automate Outlook.
 
Share this answer
 
I created a method that examples how you would use each account to pull separate info

public static void SaveEmails(Account account)
{
    Microsoft.Office.Interop.Outlook.Application outlookApplication = account.Application;
    NameSpace outlookNamespace = outlookApplication.GetNamespace("MAPI");
    Accounts accounts = outlookNamespace.Accounts;
    MAPIFolder inboxFolder = outlookNamespace.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
    Items Items = inboxFolder.Items;
    List<MailItem> mailItems = new List<MailItem>();

    foreach (Object email in Items)
    {
        if (email is MailItem)
        {
            mailItems.Add((MailItem)email);

        }

    }

    foreach (MailItem email in mailItems)
    {
        string name = email.Subject;
        Console.WriteLine(name);


    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900