How to attach a file to an email with PowerShell

I have written a PowerShell script that will create an email, however I can't seem to attach a file. The file does exist and PowerShell can open it, Could anyone tell me what I'm doing wrong?

$ol = New-Object -comObject Outlook.Application $message = $ol.CreateItem(0) $message.Recipients.Add("Deployment") $message.Subject = "Website deployment" $message.Body = "See attached file for the updates made to the website`r`n`r`nWarm Regards`r`nLuke" # Attach a file this doesn't work $file = "K:\Deploy-log.csv" $attachment = new-object System.Net.Mail.Attachment $file $message.Attachments.Add($attachment) 
99.8k 88 88 gold badges 300 300 silver badges 439 439 bronze badges asked Oct 22, 2010 at 13:35 TheLukeMcCarthy TheLukeMcCarthy 2,283 2 2 gold badges 26 26 silver badges 35 35 bronze badges

8 Answers 8

If you are on PowerShell 2.0, just use the built-in cmdlet Send-MailMessage:

C:\PS>Send-MailMessage -from "User01 [email protected]>" ` -to "User02 [email protected]>", ` "User03 [email protected]>" ` -subject "Sending the Attachment" ` -body "Forgot to send the attachment. Sending now." ` -Attachment "data.csv" -smtpServer smtp.fabrikam.com 

If you copy/paste this watch out for the extra space added after the backtick. PowerShell doesn't like it.

answered Oct 22, 2010 at 14:25 Keith Hill Keith Hill 200k 44 44 gold badges 361 361 silver badges 375 375 bronze badges

thanks for that, but I get "Unable to connect to the remote server" error when trying to use that, and the server is up.

Commented Oct 25, 2010 at 9:34

That could be authentication, firewall, etc. Check out this thread for additional help - social.msdn.microsoft.com/Forums/en-US/netfxnetcom/thread/… (towards the bottom).

Commented Oct 26, 2010 at 3:19 Commented Oct 29, 2010 at 9:36 @TheLukeMcCarthy Check the possibility of a virus scanner blocking port actions. Commented Aug 5, 2015 at 20:12

I got the above to work by removing the line

$attachment = new-object System.Net.Mail.Attachment $file 
$message.Attachments.Add($attachment) 
$message.Attachments.Add($file) 

While the solution provided by @Keith Hill would be better, even with a lot of goggling I couldn't get it to work.

answered Nov 1, 2010 at 9:24 TheLukeMcCarthy TheLukeMcCarthy 2,283 2 2 gold badges 26 26 silver badges 35 35 bronze badges

This worked for me using powershell-

$fromaddress /cdn-cgi/l/email-protection" data-cfemail="ff9b9091908b8d9a8f9386bf8f9bd19c9092">[email protected]" $toaddress /cdn-cgi/l/email-protection" data-cfemail="e397869097a39387cd808c8e">[email protected]" $Subject = "Test message" $body = "Please find attached - test" $attachment = "C:\temp\test.csv" $smtpserver = "mail.pd.com" 

Use the variables in the script:

$message = new-object System.Net.Mail.MailMessage $message.From = $fromaddress $message.To.Add($toaddress) $message.IsBodyHtml = $True $message.Subject = $Subject $attach = new-object Net.Mail.Attachment($attachment) $message.Attachments.Add($attach) $message.body = $body $smtp = new-object Net.Mail.SmtpClient($smtpserver) $smtp.Send($message) 
49.5k 31 31 gold badges 113 113 silver badges 139 139 bronze badges answered Mar 1, 2018 at 1:22 151 5 5 silver badges 14 14 bronze badges

I have experienced such problem, (windows 10 / PS 5.1) My SMTP is not authentified or secure . I have to finish by this line "MyAttacheObject.Dispose()" . / and finally that's work :!

$smtp = new-object Net.Mail.SmtpClient($smtpserver) $attach.Dispose() 

this is my code with two attachments :

# Email configuration NO AUTH NO SECURE $emailHost = "smtp.bot.com" $emailUser = "" $emailPass = "" $emailFrom /cdn-cgi/l/email-protection" data-cfemail="95f8ecf0f8f4fcf9d5f7fae1bbf6faf8">[email protected]" $emailsTo=@("[email protected]","[email protected]") $emailSubject = $title $emailbody=$body $attachment1 = @($PATh+$outFile) $attachment2 = @($PATh+$inFile) #End of parameters $msg = New-Object System.Net.Mail.MailMessage $msg.from = ($emailFrom) foreach ($d in $emailsTo) < $msg.to.add($d) >$msg.Subject = $emailSubject $msg.Body = $emailbody $msg.isBodyhtml = $true $att = new-object System.Net.Mail.Attachment($attachment1) $msg.Attachments.add($att) $att = new-object System.Net.Mail.Attachment($attachment2) $msg.Attachments.add($att) $smtp = New-Object System.Net.Mail.SmtpClient $emailHost $smtp.Credentials = New-Object System.Net.NetworkCredential($emailUser, $emailPass); $smtp.send($msg) $att.Dispose()