You can significantly shorten the code by using Send-MailMessage (the V2 cmdlet for emailing). This might also solve your problem, because of a couple of things:
- The .NET details will be hidden from you, so you don't have to worry about constructing an object incorrectly.
- It's much more likely that the cmdlets will handle paths correctly. A very common PowerShell problem when interacting in .NET is that you have to refer to things by an absolute directory. While the system has a concept of a current working directory, because a PowerShell directory could be on the filesystem, or could by in the registry, the .NET current directory is rarely in sync with the PowerShell current directory.
An example that uses Send-MailMessage is below. In it, a hashtable of parameters is created and, by using the @ instead of $ in front of the variable name, each of the parameters becomes a parameter to Send-MailMessage.
For more examples, see: Sending Automated Emails with Send-MailMessage, ConvertTo-HTML, and the PowerShell Pack's Task Scheduler Module
function SendMail($Attachment) {
# create mail message
$MessageParameters = @{
From = "[email protected]"
To = "[email protected]"
Subject = "Here is your attached file"
Body = "See attached file"
Attachments = $Attachment
SmtpServer = "mail.serverfault.com"
}
Send-MailMessage @MessageParameters
}
I believe this is a much more "PowerShell" way to do things, and I hope it gets rid of your issue.
Hope this helps,
James
$f $t $s $b $serv and $e
) - I tried it about 5 times with different.txt
attachments and it worked each time. I think you have a bigger problem than powershell going on. SPAM filter or mail server issue?