Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

param
(    
[parameter(Mandatory = $false)]
[string]$l
)

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

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

3 Answers 3

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 = "Beuy@serverfault.com"
  $mail.To.Add("Beuy@serverfault.com")
  $mail.Subject = "Here is your attached file"
  $mail.Body = "See attached file"
  # create attachment
  $att = New-Object System.Net.Mail.Attachment "C:\Test.txt"
  $mail.Attachments.Add($att)
  # send email
  $smtp = New-Object System.Net.Mail.SmtpClient("mail.serverfault.com")
  $smtp.Send($mail)
}
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 = "Beuy@serverfault.com"
        To = "Beuy@serverfault.com"
        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

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

 
discard

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.