0
votes

I just started to dabble into using Microsoft.Office.Interop.Outlook. I was able to successfully send an email using the bit of code below.

public void Send()
{
    try
    {
        Outlook._Application _app = new Outlook.ApplicationClass();
        var test = _app.CreateItem(Outlook.OlItemType.olMailItem);
        Outlook.MailItem mail = (Outlook.MailItem) _app.CreateItem(Outlook.OlItemType.olMailItem);
        mail.To = "[email protected]";
        mail.Subject = "Test Outlook Subject";
        mail.Body = "Test Outlook Body";
        mail.Importance = Outlook.OlImportance.olImportanceNormal;
        ((Outlook.MailItem) mail).Send();
    }
    catch
    {
        Notification.Notice("Error");
    }
}

I would like to have a Validate() function before the try/catch such that it'll check to see if there's a valid outlook account enabled. May I ask does anyone know how I can check if any outlook accounts are setup?

I tried this

public bool validate()
{
    Outlook._Application _app = new Outlook.ApplicationClass();
    Outlook.Accounts accounts = _app.Session.Accounts;
    return accounts.Count > 0;
}

But accounts.Count returned 1 even after I removed my outlook account.

1

1 Answers

1
votes

There will always be at least one account - the store. Otherwise Outlook won't run. But even if there are mail accounts, how would you know whether they are configured appropriately? Unless you take over the message submission, there is no way for you to know ahead of time.

UPDATE: Loop through the Namespace.Accounts collection and look for accounts with Account.AccountType == olExchange ,olImap,olPop3, olHttp. If you were using Extended MAPI (C++ or Delphi), you could use IOlkAccountManager::EnumerateAccounts(CLSID_OlkMail, ...) (you can play with that interface in OutlookSpy - click IOlkAccountManager button). If Extended MAPI is not an option, Redemption exposes the RDOAccounts object; its GetOrder(acMail) method will return all mail accounts. You'll just need to check if the returned collection has any elements.