Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have the following function to send e-mails in powershell with attachments:

[parameter(Mandatory = $false)]

function SendMail(){
if($e -ine '')
$mail = New-Object System.Net.Mail.MailMessage
$mail.From = $f;
$mail.Subject = $s;
$att = New-Object System.Net.Mail.Attachment –ArgumentList $l
$mail.Body = $b;
$smtp = New-Object System.Net.Mail.SmtpClient($serv);

When I try to send a .txt file attachment it seems to strip out all of the information from within the text file. Any suggestions as to what's going wrong?

share|improve this question
You're not including your parameters. What type is $l? – James Pogran May 11 '10 at 0:51
Sorry about that James [parameter(Mandatory = $false)] [string]$l, – Beuy May 11 '10 at 1:00
just as a sanity check I copy/pasted your code - the only changes I made were to add the local variables that aren't defined here ($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? – MattB May 11 '10 at 13:54
up vote 2 down vote accepted

I basically used your same code - and a great variety of attachments all came through just fine. I am fairly confident your problem is outside of PowerShell - do you have a SPAM or other filter setup on the SMTP server you are communicating with? is there another SMTP server you can talk to that is clean?

For reference, here is the sample code I was tweaking parameters to test with:

function SendMail() {
  # create mail message
  $mail = New-Object System.Net.Mail.MailMessage
  $mail.From = ""
  $mail.Subject = "Here is your attached file"
  $mail.Body = "See attached file"
  # create attachment
  $att = New-Object System.Net.Mail.Attachment "C:\Test.txt"
  # send email
  $smtp = New-Object System.Net.Mail.SmtpClient("")
share|improve this answer
Hi Goyuix, You're right about the problem being bigger than powershell but it wasn't related to SPAM filtering. For some reason a mounted drive (H:) was being referenced for the location of $att instead of it's full location, to resolve I added the following line of code earlier $logfile = Resolve-Path $logfile which resolve the issue. I've had problems on this machine with that mounted drive before, should have tested it on a clean environment. Thank you for the help. – Beuy May 13 '10 at 0:40

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 = ""
        To = ""
        Subject = "Here is your attached file"
        Body = "See attached file"
        Attachments = $Attachment        
        SmtpServer = ""        
    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,


share|improve this answer
if($e -ine '')

-ine seems to be a typo?

you really should include the values... When you create the attachment, maxybe there is a problem with the path or filename? Try to use the "FullName"(path+filename) of the File.

share|improve this answer
incorrect, -ine is equivalent to -ne - I believe it exists as a code hint that it is case-insensitive. – MattB May 11 '10 at 13:46

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.