$EmailFrom = "[email protected]"
$EmailTo = "yy.com"
$Subject = "Testing, Testing 123"
$Body = "this is a notification from XYZ Notifications.."
$Attachment = "C:\Users\XX\Desktop\Importanttxts\old.txt"
$SMTPServer = "smtp.gmail.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.CredentialsObjectSystem.Net.NetworkCredential("pm8566","123456");
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body, $Attachment)
cmd /c pause | out-null
这是我的powershell脚本,它工作正常,但没有附件,无法发送附件。
试试:
$smtpFrom = "[email protected]"
$smtpTo = "yy.com"
$SMTPServer = "smtp.gmail.com"
$SMTPClient = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$SMTPClient.Subject = "Testing, Testing 123"
$SMTPClient.Attachments = "C:\Users\XX\Desktop\Importanttxts\old.txt"
$SMTPClient.Body = "this is a notification from XYZ Notifications.."
$SMTPClient.EnableSsl = $true
$smtp = New-Object Net.Mail.SmtpClient($SMTPServer)
$smtp.CredentialsObjectSystem.Net.NetworkCredential("pm8566","123456");
$smtp.Send($SMTPClient)
cmd /c pause | out-null
PowerShell v3具有用于邮件用例的内置cmdlet。发送-MAILMESSAGE。
您选择使用.Net与cmdlet的任何原因?
您可以将cmdlet与Gmail配合使用。 PowerShell: Sending Email With Send-MailMessage (Gmail example)
$From = "[email protected]"
$To = "[email protected]"
$Cc = "[email protected]"
$Attachment = "C:\temp\Some random file.txt"
$Subject = "Email Subject"
$Body = "Insert body text here"
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject `
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort -UseSsl `
-Credential (Get-Credential) -Attachments $Attachment
#PSTip Sending emails using your Gmail account
$param = @{
SmtpServer = 'smtp.gmail.com'
Port = 587
UseSsl = $true
Credential = '[email protected]'
From = '[email protected]'
To = '[email protected]'
Subject = 'Sending emails through Gmail with Send-MailMessage'
Body = "Check out the PowerShellMagazine.com website!"
Attachments = 'D:\articles.csv'
}
Send-MailMessage @param