2017-06-14 169 views
0

我想發送EMail與C#和Microsoft.Office.Interop.Outlook庫。使用Outlook帳戶發送郵件

我被鎖定在我的Outlook帳號[email protected]上,並獲得了交換服務器發送[email protected][email protected]的郵件的權利。

我沒有找到任何方式發送郵件BC

我得到了一切工作我的用戶帳戶,其中有郵件[email protected]

現在我找不到方法獲得[email protected]的帳戶並以B的名義發送郵件。

我的問題:

如何訪問其他帳戶?

這是我的代碼有:

using Outlook = Microsoft.Office.Interop.Outlook; 
     public static Outlook.Account GetAccountForEmailAddress(Outlook.Application application, string smtpAddress) 
    { 

     // Loop over the Accounts collection of the current Outlook session. 
     Outlook.Accounts accounts = application.Session.Accounts; 
     if (_IsDebug) 
      Console.WriteLine($"Anzahl Accounts: {accounts.Count}"); 
     foreach (Outlook.Account account in accounts) 
     { 
      // When the e-mail address matches, return the account. 
      if (_IsDebug) 
       Console.WriteLine($"Account: {account.SmtpAddress}"); 
      if (String.Compare(account.SmtpAddress, smtpAddress, true) == 0) 
      { 
       return account; 
      } 
     } 
     throw new System.Exception(string.Format("No Account with SmtpAddress: {0} exists!", smtpAddress)); 
    } 
     static void SendEMail(string emailadress) 
    { 
     try 
     { 
      var outlookApplication = new Outlook.Application(); 
      var outlookMailItem = (Outlook.MailItem)outlookApplication.CreateItem(Outlook.OlItemType.olMailItem); 

      outlookMailItem.SendUsingAccount = GetAccountForEmailAddress(outlookApplication, ConfigurationManager.AppSettings[SENDER]); 


      if (_IsDebug) 
       Console.WriteLine($"Absender: {outlookMailItem?.SendUsingAccount?.SmtpAddress}"); 

      outlookMailItem.HTMLBody = ConfigurationManager.AppSettings[BODY]; 

      if (_IsDebug) 
       Console.WriteLine($"Body: {outlookMailItem?.HTMLBody}"); 

      var file = GetPDFFile(); 
      if (_IsDebug) 
       Console.WriteLine($"File: {file?.Name}"); 

      if (file == null) 
      { 
       Console.WriteLine("Keine Datei gefunden!"); 
       return; 
      } 

      string attachementDisplayName = file.Name; 

      int attachementPosition = outlookMailItem.HTMLBody.Length + 1; 
      int attachementType = (int)Outlook.OlAttachmentType.olByValue; 

      if (_IsDebug) 
       Console.WriteLine($"Dateianhang: {file.FullName}"); 

      Outlook.Attachment outlookAttachement = outlookMailItem.Attachments.Add(file.FullName, attachementType, attachementPosition, attachementDisplayName); 

      outlookMailItem.Subject = ConfigurationManager.AppSettings[SUBJECT]; 

      Outlook.Recipients outlookRecipients = outlookMailItem.Recipients; 
      Outlook.Recipient outlookRecipient = outlookRecipients.Add(emailadress); 

      outlookRecipient.Resolve(); 

      outlookMailItem.Send(); 

      outlookRecipient = null; 
      outlookRecipients = null; 
      outlookMailItem = null; 
      outlookApplication = null; 

      if (_IsDebug) 
       Console.ReadLine(); 
     } 
     catch (Exception) 
     { 
      throw; 
     } 
    } 

回答

0

您有權授予發送郵箱所有者發送當你想從發送的電子郵件郵箱的權限。代表發送權限將可用,但會說「A代表B」作爲發件人。

+0

你有代碼示例嗎?無法弄清楚如何做到這一點.. – M4s0n

+0

您可以使用PowerShell:https://msdn.microsoft.com/en-us/library/ff852815(v=exchsrvcs.149).aspx –

相關問題