PowerShell使用SMTP发送邮件

时间:2023-03-09 21:47:41
PowerShell使用SMTP发送邮件
$smtpServer = "smtp.exmail.qq.com"
$smtpUser = "xxxxx@qq.com"
$smtpPassword = "xxxxxxxxxxxxxx"
#create the mail message
$mail = New-Object System.Net.Mail.MailMessage
#set the addresses
$MailAddress="xxxxx@qq.com"
$MailtoAddress="xxxxx11111111@qq.com"
$mail.From = New-Object System.Net.Mail.MailAddress($MailAddress)
$mail.To.Add($MailtoAddress)
#set the content
$mail.Subject = "Hello PowerShell";
$mail.Priority = "High"
$mail.Body = "Hello Powershell"
$filename="D:\1.txt" #add file
$attachment = new-Object System.Net.Mail.Attachment($filename)
$mail.Attachments.Add($attachment)
#send the message
$smtp = New-Object System.Net.Mail.SmtpClient -argumentList $smtpServer
$smtp.Credentials = New-Object System.Net.NetworkCredential -argumentList $smtpUser,$smtpPassword
$smtp.Send($mail)