簡體   English   中英

如何使用 C# 將當前用戶的電子郵件地址轉換為 Outlook 中的字符串?

[英]How do I convert the current user email address to a string in Outlook using C#?

我在 Outlook 2016 中創建了一個自定義 VSTO 插件,它要求用戶附加一個文件,然后將該文件添加到電子郵件回復中並自動發送。

public void Attachment_Click(Office.IRibbonControl control)
        {
            Outlook.Explorer explorer = Globals.ThisAddIn.Application.ActiveExplorer();
            if (explorer != null)
            {
                Outlook.Selection selection = explorer.Selection;
                if (selection.Count >= 1)
                {
                    Outlook.MailItem mailItem = selection[1] as Outlook.MailItem;
                    OpenFileDialog attachment = new OpenFileDialog();
                    attachment.Title = "Add your file";
                    attachment.ShowDialog();

                    if (mailItem != null) //could be something other than MailItem
                    {
                        Outlook.MailItem response = mailItem.ReplyAll();
                        bool retValue = false;
                        response.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
                        response.HTMLBody = "<p>MESSAGE</p>" + response.HTMLBody;
                        response.Attachments.Add(attachment.FileName, Outlook.OlAttachmentType.olByValue, 1,"Attachment.pdf");
                        response.Send();
                        mailItem.Delete();
                    }
                }
            }
        }

我正在嘗試調整代碼,以便它根據登錄到 Outlook 的用戶自動添加不同的附件,例如,如果 user=user1@company.com 添加 '\\fileshare\\file1.pdf',如果 user=user2@company.com添加'\\fileshare\\file2.pdf'

這可能嗎?

我正在嘗試使用以下內容將當前用戶地址轉換為字符串,但沒有任何成功:

{
 return 
Globals.ThisAddIn.Application.ActiveExplorer().Session.CurrentUser.AddressEntry.GetExchangeUser().PrimarySmtpAddress.ToString();
}

如果沒有活動的資源管理器,您的代碼將失敗,但您確實不需要它。 在我的頭頂:

AddressEntry cu = Globals.ThisAddIn.Application.Session.CurrentUser.AddressEntry;
ExchangeUser eu = cu.GetExchangeUser();
return (eu == null) ? cu.Address : eu.PrimarySmtpAddress;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM