Bridgey
Bridgey

Reputation: 539

How do I get an Outlook.Account from smtp address or samaccountname?

Environment: Outlook 2010 (32-bit), Exchange 2010, Server 2008R2 (64-bit)
Dev Environment: Visual Studio 2010 on Windows 7 (64-bit)

I'm writing a C# Add-In for Outlook 2010 and I'm having a problem specifying the account/email address from which the email is sent.

I want to send the email from a Shared Mailbox; I have the appropriate permission on the mailbox.

Programatically, I have the SMTP address of the mailbox ([email protected]) and the SAMAccountName of the mailbox (sharedacc).

Currently, my code looks like this:

Outlook.MailItem response = app.CreateItemFromTemplate(template.Path, folder) as Outlook.MailItem;
response.SendUsingAccount = ???<Outlook.Account>;

However, I can't seem to find any way of creating an Outlook.Account object from either the SAMAccountName or the SMTP address. Is there a way?

I thought I might be able to instead use:

response.Sender = ???<Outlook.AddressEntry>

But similarly, I can't find a way to create an Outlook.AddressEntry from either the SAMAccountName or the SMTP address. Anybody know how?

Any hints, links, or wild guesses greatly appreciated.

Upvotes: 2

Views: 1234

Answers (2)

JleruOHeP
JleruOHeP

Reputation: 10376

You can get the account using Application.Session.Accounts:

Outlook.Account account = Application.Session.Accounts["sharedacc"];
response.SendUsingAccount = account;

Check this link.

And if you need to check other accounts available you can use this (copy paste from msdn):

StringBuilder builder = new StringBuilder();
foreach (Outlook.Account account in accounts)
{
   // The DisplayName property represents the friendly name of the account.
   builder.AppendFormat("DisplayName: {0}\n", account.DisplayName);

   // The UserName property provides an account-based context to determine identity.
   builder.AppendFormat("UserName: {0}\n", account.UserName);

   // The SmtpAddress property provides the SMTP address for the account.
   builder.AppendFormat("SmtpAddress: {0}\n", account.SmtpAddress);

   // The AccountType property indicates the type of the account.
   builder.Append("AccountType: ");
   switch (account.AccountType)
   {
      case Outlook.OlAccountType.olExchange:
           builder.AppendLine("Exchange");
           break;
      case Outlook.OlAccountType.olHttp:
           builder.AppendLine("Http");
           break;
      case Outlook.OlAccountType.olImap:
           builder.AppendLine("Imap");
           break;
      case Outlook.OlAccountType.olOtherAccount:
           builder.AppendLine("Other");
           break;
      case Outlook.OlAccountType.olPop3:
           builder.AppendLine("Pop3");
           break;
   }

   builder.AppendLine();
}

Upvotes: 0

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66215

If you are sending on behalf of another Exchange mailbox, all you need to do is set the MailItem.SentOnBehalfOfName property beforecalling Send.

Upvotes: 1

Related Questions