gpt4 book ai didi

powershell - 如何在 PowerShell 代码中发送带有附件的电子邮件?

转载 作者:行者123 更新时间:2023-12-03 00:05:40 27 4
gpt4 key购买 nike

我正在尝试编写一个 PowerShell 代码,将电子邮件发送到指定的电子邮件地址。当我运行代码时,它只显示

+ $SMTPClient.Send <<<< ($EmailFrom, $EmailTo, $Attachment, $Subject, $Bod)
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest

哦,顺便说一句,这是我的代码

$EmailFrom = “Myemail@outlook.com”
$EmailTo = “Someonesemail@gmail.com"
$Attachment = "(Filepath)"
$Subject = “Test”
$Bod = “Test”
$SMTPServer = “smtp.outlook.com”
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“Email”,“Pass”); $SMTPClient.Send($EmailFrom, $EmailTo, $Attachment, $Subject, $Bod)

请帮忙。我很痛苦

最佳答案

你应该先尝试类似的东西:

$Username  = "Myemail@outlook.com"
$EmailPassword = "YourRealPassword of your email"
$Attachment= "C:\Test.txt" # Example you can change its path here instead of "c:\Test.txt"
$EmailTo = "Someonesemail@gmail.com"
$EmailFrom = "Myemail@outlook.com"
$Subject = "PowershellTest"
$Body= "PowershellTest"
$SMTPServer = "smtp.outlook.com"
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom, $EmailTo, $Subject, $Body)
$Attachment = New-Object System.Net.Mail.Attachment($Attachment)
$SMTPMessage.Attachments.Add($Attachment)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($Username, $EmailPassword)
$SMTPClient.Send($SMTPMessage)

编辑: 使用 PowerShell 的 Send-MailMessage cmdlet

引用How to Send Email with Office 365 Direct Send and PowerShell

使用 PowerShell 的 Send-MailMessage cmdlet:

您首先需要定义一个 PowerShell PScredential 对象,然后提供 Send-MailMessage 需要的所有参数。

# Get the credential
$credential = Get-Credential
## Define the Send-MailMessage parameters
$mailParams = @{
SmtpServer = 'smtp.office365.com'
Port = '587'
UseSSL = $true
Credential = $credential
From = 'sender@yourdomain.com'
To = 'recipient@yourdomain.com', 'recipient@NotYourDomain.com'
Subject = "SMTP Client Submission - $(Get-Date -Format g)"
Body = 'This is a test email using SMTP Client Submission'
Attachment = 'C:\Test.txt' # Here you can change your attachment
DeliveryNotificationOption = 'OnFailure', 'OnSuccess'
}

## Send the message
Send-MailMessage @mailParams

当您运行上面的代码时,您应该会收到一封由内部收件人 (yourdomain.com) 和外部域 (notyourdomain.com) 收到的带有附件的电子邮件。


重要提示:

如果你用google的smtp发送邮件遇到这个问题,你应该看看这个Can not send mail using smtp.gmail.com, port 587 from vbs script

您可以使用批处理和 powershell 来发送电子邮件:首先将此代码复制并粘贴为

PS-Gmail-Sender.bat

@ECHO OFF
REM https://stackoverflow.com/questions/28605803/can-not-send-mail-using-smtp-gmail-com-port-587-from-vbs-script/28606754#28606754
Title Sending E-Mail with Gmail Less Secure Applications using Powershell and Batch
SET GmailAccount="%~1"
SET GmailPassword="%~2"
SET Attachment="%~3"
REM We write our Powershell script
CALL :WritePS
REM We execute our Powershell script .PS1 by passing arguments from the command line or a batch file
Powershell -ExecutionPolicy bypass -noprofile -file "%PSScript%" "%GmailAccount%" "%GmailPassword%" "%Attachment%"
IF EXIST "%PSScript%" DEL /Q /F "%PSScript%"
pause
EXIT
REM -----------------------------------------------------------------------------------------------------
:WritePS
SET PSScript=%temp%\temp_SendeMail.ps1
> "%PSScript%" (
ECHO $Username = $args[0]
ECHO $EmailPassword = $args[1]
ECHO $Attachment= $args[2]
ECHO $EmailTo = $Username
ECHO $EmailFrom = $Username
ECHO $Subject = "This email was sent from Powershell script into a batch file with Less Secure Application Enabled"
ECHO $Body= "Test Email Sending with a script"
ECHO $SMTPServer = "smtp.gmail.com"
ECHO $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom, $EmailTo, $Subject, $Body^)
ECHO $Attachment = New-Object System.Net.Mail.Attachment($Attachment^)
ECHO $SMTPMessage.Attachments.Add($Attachment^)
ECHO $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587^)
ECHO $SMTPClient.EnableSsl = $true
ECHO $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($Username, $EmailPassword^)
ECHO $SMTPClient.Send($SMTPMessage^)
)
Exit /B
REM -----------------------------------------------------------------------------------------------------

其次,您可以从命令行调用这个批处理文件,也可以将另一个批处理文件另存为 Sendit.bat 并双击执行以调用第一个带有参数的脚本 PS-Gmail-Sender.bat

PS-Gmail-Sender.bat "Mygmail_Account@gmail.com" "MyGmail_Password" "D:\test\myFile.txt"

关于powershell - 如何在 PowerShell 代码中发送带有附件的电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63334948/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com