Power shell script to send an email with an attachment RRS feed

  • Question

  • i am new to powershell scripting and i am trying to write a script that will send an email with an attachment to it. Please help 

    here is the code 

    #Clear Screen
    cls Write-Host "Sending Email"

    #SMTP server name
    $smtpServer = "smtp.mail.yahoo.com"

    #Creating a Mail object
    $msg = new-object Net.Mail.MailMessage

    #Email structure
    $msg.From = "awyyyy@yahoo.com"
    $msg.subject = "hellow"
    $msg.body = "The Body of the email."
    $msg.attachment = ""C:\user\user.docx""

    #Creating SMTP server object
    $smtp = new-object Net.Mail.SmtpClient($smtpServer)
    $smtp.EnableSsl = 1

    $smtp.Port = 587
    $cred = New-Object Net.NetworkCredential("sssssss@yahoo.com","xxxxxxxxxx")
    $attach = New-Object Net.Mail.Attachment($msg.attachment, "C:\user\user.docx")
    $smtp.Credentials = $cred

    #Sending email
    Write-Host "Mail Sent"

    Monday, September 18, 2017 2:39 PM

All replies

  • Get-Help Send-MailMessage -Full

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''

    • Proposed as answer by jrv Monday, September 18, 2017 6:22 PM
    Monday, September 18, 2017 2:55 PM