【问题标题】:How to send an email with an attachment in PowerShell code?如何在 PowerShell 代码中发送带有附件的电子邮件?
【发布时间】:2020-08-10 06:06:58
【问题描述】:

我正在尝试编写一个将电子邮件发送到指定电子邮件地址的 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)

请帮忙。我很痛苦

【问题讨论】:

标签: powershell email outlook email-attachments


【解决方案1】:

你应该先尝试这样的事情:

$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,因此根本不需要 .bat 脚本。
  • @Bill_Stewart 是的!只是这是一个选项,但我想说的是使用smtp.gmail.com 激活不太安全的应用程序最重要的事情!
  • 天哪。我实际上需要一个 .bat 文件,但两者都可以正常工作! tysm 人。
  • 最后我可以做点什么来向我的朋友发送淫秽内容
  • @MrTortol 请使用 Stack Overflow (SO) tour 了解如何通过单击答案左侧的灰色复选标记符号接受有帮助的答案来对 SO 表示感谢。另请参阅How does accepting an answer work? 没有接受答案的问题被评为未回答问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-09
  • 1970-01-01
相关资源
最近更新 更多