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

标签 powershell email outlook email-attachments

我正在尝试编写一个 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/

相关文章:

excel - 从工作表中的列表创建不重复的约会

amazon-web-services - 将 IIS 日志移动到 AWS s3 存储桶

powershell - 通过WebClient在FTP凭据中使用特殊字符(斜杠)

php - 使用 Zend (PHP) 在 Gmail 中包含客户的默认电子邮件签名

python GAE : incoming mail handler error

css - 如何让 MS Outlook 接受 CSS 样式显示 :block?

powershell - PowerShell 脚本中的 bool 参数值

powershell - 如何使用Powershell脚本禁用任务计划程序作业

PHP UTF8 邮件在 iPhone 邮件应用程序上出现乱码

excel - 如何将电子邮件另存为草稿?