aktuelle.kurse/m122/tools-technics/mailing-mit-powershell1/ps-sendmail-easy.ps1

35 lines
1.4 KiB
PowerShell
Raw Normal View History

2023-02-13 21:55:43 +01:00

# Method to write an email with the logfile as attachement
#--------------------------------------------------------------------------------------------------
function sendmail() {
$toMail = "harald.mueller@bluewin.ch"
$attachment = "C:\Users\Harald\OneDrive\Technik\mails\data.pdf"
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
$Username = "invoice.autointerleasing@gmail.com"
# password of the mail account
$Password = "invoice77autointerleasing" | ConvertTo-SecureString -AsPlainText -Force
$subject = "Powi mail"
$body = "Logfile has been modified. Please review attached document $entry."
$message = New-Object System.Net.Mail.MailMessage
$message.subject = $subject
$message.body = $body
$message.to.add($toMail)
$message.from = $username
$message.attachments.add($attachment)
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.send($message)
}
## (Get-Credential).password | ConvertFrom-SecureString > mailpasswort.txt
## $pw = Get-Content .\mailpasswort.txt | ConvertTo-SecureString
## $cred = New-Object System.Management.Automation.PSCredential "MailUser", $pw
## Send-MailMessage -Credential $cred -to "harald.mueller@bluewin.ch" -from "PowerShell <ps@fabrikam.de>" -Subject "Test" -body "Test für Send-MailMessage"
sendmail