2017-06-14 118 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 –

相关问题