Powershell脚本不适用于附件

问题描述 投票:0回答:2
$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脚本,它工作正常,但没有附件,无法发送附件。

powershell email powershell-v2.0 powershell-v3.0 email-attachments
2个回答
0
投票

试试:

$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

0
投票

PowerShell v3具有用于邮件用例的内置cmdlet。发送-MAILMESSAGE。

Send-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
© www.soinside.com 2019 - 2024. All rights reserved.