Powershell – Send mail with attachment

That’s a simple one but good to have

$file = “MYFILE.TXT”
$smtpServer = “MYSMTPSERVER.EMAIL.CO.UK”
$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment($file)
$smtp = new-object Net.Mail.SmtpClient($smtpServer)

$msg.From = “FROMME@EMAIL.CO.UK”     #reply-to-address
$msg.Sender = “Csoma Ádám”                 #From field
$msg.To.Add(“TOME@EMAIL.CO.UK“)        #send-to part
$msg.Subject = “MY SUBJECT”
$msg.Body = “MY TEXT FOR THE EMAIL”
$msg.Attachments.Add($att)

 

# To authentaicate at the mail server you need to use the next line

# $smtp.Credentials =[System.Net.CredentialCache]::DefaultCredentials

 

$smtp.Send($msg)

$att.Dispose()

1 thought on “Powershell – Send mail with attachment”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s