如何从smtp地址或samaccountname获取Outlook.Account?

时间:2014-08-25 00:20:20

标签: c# outlook outlook-addin

环境: Outlook 2010(32位),Exchange 2010,Server 2008R2(64位)
开发环境: Windows 7(64位)上的Visual Studio 2010

我正在为Outlook 2010编写C#加载项,我在指定发送电子邮件的帐户/电子邮件地址时遇到问题。

我想从共享邮箱发送电子邮件;我对邮箱有适当的许可。

以编程方式,我有邮箱的SMTP地址(sharedacc@company.com)和邮箱的SAMAccountName(sharedacc)。

目前,我的代码如下所示:

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

但是,我似乎无法找到从SAMAccountName或SMTP地址创建Outlook.Account对象的任何方法。 有办法吗?

我以为我可能会改用:

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

但同样地,我找不到从SAMAccountName或SMTP地址创建Outlook.AddressEntry的方法。 有人知道吗?

非常感谢任何提示,链接或猜测。

2 个答案:

答案 0 :(得分:1)

如果您代表另一个Exchange邮箱发送邮件,那么您需要做的就是在调用Send之前设置MailItem.SentOnBehalfOfName属性。

答案 1 :(得分:0)

您可以使用Application.Session.Accounts

获取帐户
Outlook.Account account = Application.Session.Accounts["sharedacc"];
response.SendUsingAccount = account;

选中link

如果您需要查看其他可用帐户,可以使用this(从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();
}