【问题标题】:Get Outlook email address through c#通过 c# 获取 Outlook 电子邮件地址
【发布时间】:2013-09-07 12:04:17
【问题描述】:

我需要能够使用 C# 代码获取当前登录用户的电子邮件地址。

我需要完整的地址,而不仅仅是假定的电子邮件帐户(例如 user@localdomain.com.au),尽管这适用于大多数客户。

任何帮助将不胜感激。

【问题讨论】:

  • 恐怕我目前还没有尝试,我能找到的唯一参考是 vb,需要指出正确的方向
  • 几乎所有 VB 结构都可以很容易地转换为 C# - 也可以看看 SO,stackoverflow.com/questions/4761521/… 之类的问题可能适用于您

标签: c# outlook


【解决方案1】:

试试这个,来自http://msdn.microsoft.com/en-us/library/ff462091.aspx

using System;
using System.Text;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace OutlookAddIn1
{
    class Sample
    {
        public static void DisplayAccountInformation(Outlook.Application application)
        {

            // The Namespace Object (Session) has a collection of accounts.
            Outlook.Accounts accounts = application.Session.Accounts;

            // Concatenate a message with information about all accounts.
            StringBuilder builder = new StringBuilder();

            // Loop over all accounts and print detail account information.
            // All properties of the Account object are read-only.
            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();
            }

            // Display the account information.
            System.Windows.Forms.MessageBox.Show(builder.ToString());
        }
    }
}

【讨论】:

  • 非常感谢我使用了正确的脚本,这个参考帮助了很多简单的问题,不包括正确的参考不会忘记一个 =)
【解决方案2】:

从 Outlook 获取电子邮件 ID

createdby = mailItem.UserProperties.Session.CurrentUser.Address;

或者,我们可以使用这个

if (app.Session.CurrentUser.AddressEntry.Type == "EX") {
    createdby = app.Session.CurrentUser.AddressEntry.GetExchangeUser().PrimarySmtpAddress;
} else {
    createdby = app.Session.CurrentUser.AddressEntry.Address;
}

【讨论】:

    【解决方案3】:

    使用 C# 代码,从 Outlook 2013、2016 中获取 From Email Id。

    using System;
    using System.Net;
    using Outlook = Microsoft.Office.Interop.Outlook;
    using System.IO;
    using System.Net.Mail;
    
    public string CreateByEmail_ID()
    
      {
            Microsoft.Office.Interop.Outlook._NameSpace ns = null;
            // Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
            Outlook._Application oApp = new Outlook.Application();
            Object selObject = oApp.ActiveExplorer().Selection[1];
            Microsoft.Office.Interop.Outlook.Application app = null;
            app = new Microsoft.Office.Interop.Outlook.Application();
            LogWriter.LogWrite1("Application :");
            ns = app.GetNamespace("MAPI");
            Outlook.MailItem mailItem = (selObject as Outlook.MailItem);
            createdbyEmail = mailItem.UserProperties.Session.CurrentUser.Address;
    
            if (app.Session.CurrentUser.AddressEntry.Type == "EX")
            {
                createdbyEmail = app.Session.CurrentUser.AddressEntry.GetExchangeUser().PrimarySmtpAddress;
            }
            else
            {
                createdbyEmail = app.Session.CurrentUser.AddressEntry.Address;
            }
    
            return createdbyEmail;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-09
      相关资源
      最近更新 更多