簡體   English   中英

如何添加多個電子郵件地址?

[英]How do I add multiple email addresses?

下面是我正在使用的代碼。 我無法使用powershell Emailaddresses參數添加多個地址。 如果我只輸入一個電子郵件地址,該代碼就可以正常工作,但是一旦在下面的代碼中添加了兩個地址,它就會返回說明無效的smtp地址的異常。

PSCredential credential = new PSCredential(username, password);
WSManConnectionInfo connectionInfo = new WSManConnectionInfo((new Uri(liveIdconnectionUri)), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Default;

Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);
PowerShell powershell = PowerShell.Create();
runspace.Open();
powershell.Runspace = runspace;

var secure = new SecureString();
foreach (char c in textBox5.Text)
{
    secure.AppendChar(c);
}

PSCommand command2 = new PSCommand();
command2.AddCommand("Set-Mailbox");
command2.AddParameter("Identity", "lferrigno");
command2.AddParameter("EmailAddressPolicyEnabled", 0);
command2.AddParameter("EmailAddresses", "SMTP:lferrigno@sscincorporated.com,lou.ferrigno@altegrahealth.com");

powershell.Commands = command2;
powershell.Invoke();

這是我最終使用的代碼,因為它是一個集合。

     string[] smtp = { "SMTP:" + textBox6.Text, 9 + "smtp:" + textBox4.Text + "@sscincorporated.com" };
     command2.AddParameter("EmailAddresses", smtp);

-EmailAddresses參數接受一個數組參數(從技術上講是SmtpProxyAddress對象的集合,但這並不重要,您可以為其提供一個數組,並且轉換將自動進行處理),但是看起來您正在給它一個包含多個字符串的字符串地址。 您需要提供一個數組參數(其中每個元素是一個地址),或嘗試執行以下操作:

command2.AddParameter("EmailAddresses","'SMTP:lferrigno@sscincorporated.com','SMTP:lou.ferrigno@altegrahealth.com'");

即使那仍然是您的C#代碼中的字符串,但如果將其原樣傳遞給PowerShell,PowerShell會將其解釋為數組。

您還應該能夠省略SMTP:前綴,因為這是默認設置,如果您不指定前綴,則會自動設置。

暫無
暫無

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

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