Reputation: 13680
I've created a small cmdlet in for Powershell to be able to send an e-mail using exchange services. When I pass the body of the message in Powershell seems to be stripping all whitespace out of the string.
Here is the cmdlet code:
[Cmdlet(VerbsCommunications.Send, "ExchangeEmail")]
public class SendExchageEmailCommand : Cmdlet
{
[Parameter(Mandatory=true)]
public string ServerUri { get; set; }
[Parameter(Mandatory=true)]
public string Subject { get; set; }
[ValidatePattern("^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$")]
[Parameter(Mandatory = true)]
public string To { get; set; }
[ValidatePattern("^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$")]
[Parameter]
public string From { get; set; }
[Parameter]
public string Body { get; set; }
protected override void ProcessRecord()
{
var service = new ExchangeService(ExchangeVersion.Exchange2007_SP1) {Url = new Uri(ServerUri)};
var mail = new EmailMessage(service) {Subject = Subject};
if (!string.IsNullOrEmpty(From))
{
mail.From = From;
}
if (!string.IsNullOrEmpty(Body))
{
mail.Body = Body;
}
mail.ToRecipients.Add(To);
mail.Send();
}
}
And the PowerShell code looks like:
$subject = "Testing Reports"
$body = ""
$files = Get-ChildItem $ReportsFolder
foreach ($file in $files)
{
$body += "`r`n"
$body += $file.FullName
}
Send-ExchangeEmail -ServerUri $SMTPServerURI -Subject $subject -To $MessageTo -Body $body
Is there any way to ensure that whitespace is passed into a cmdlet (and am I even right that it is PS stripping the whitespace?)
Upvotes: 0
Views: 305
Reputation: 4838
I don't think that PowerShell is removing whitespace. I find it more likely that you're sending the e-mail body as if it was HTML, not plain text. I don't know what class EmailMessage is (since I don't know the namespace or anything), but in the MailMessage class there is a flag for IsBodyHtml, which you can set.
Upvotes: 1