使用Powershell发送多个Gmail附件

时间:2017-06-17 03:44:00

标签: image powershell email gmail png

标题几乎解释了它。现在我从其他网站收集了这个:

$SendTo = "me@gmail.com"
$SMTPServer = "smtp.gmail.com" 
$EmailFrom = “me@gmail.com”
$EmailSubject = “Subject”
$Image = "0.png"
$Message = new-object Net.Mail.MailMessage
Add-PSSnapin Microsoft.Exchange.Management.Powershell.Admin -erroraction silentlyContinue
$att = new-object Net.Mail.Attachment($Image)
$att.ContentId = "att"
$smtp = new-object Net.Mail.SmtpClient($smtpServer, 587)
$smtp.Credentials = New-Object System.Net.NetworkCredential("me@gmail.com", "password"); 
$body = '<img src="cid:att" />'
$Message.From = $EmailFrom
$Message.To.Add($SendTo)
$Message.Subject = $EmailSubject
$Message.Body = $body
$Message.IsBodyHTML = $true
$Message.Attachments.Add($att)
$smtp.Send($Message)
$att.Dispose()

但它不起作用,它给了我错误:

Exception calling "Send" with "1" argument(s): "The SMTP server requires a 
secure connection or the client was not
authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first. p56sm3035297qta.18 - gsmtp"
At A:\ScreenshotEmail.ps1:19 char:1
+ $smtp.Send($Message)
+ ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SmtpException

我也想发送多张照片,&#34; 0.png&#34;是第一个,我希望能够像这样添加许多,比如二十个:

  

0.png   1.png   2.png

等等。

编辑: 添加LotPings后#39;和MarliesV的代码,这就是我所拥有的。它有效,但它只发送1.png,而不是两者。我会发送更多,但我只是在这里使用两个。例如。

$SendTo = "receiver@gmail.com"
$SMTPServer = "smtp.gmail.com" 
$EmailFrom = “me@gmail.com”
$EmailSubject = “Subject”
$Images = @()
$Images += "0.png"
$Images += "1.png"
foreach ($Image in $Images) {
    $Message.Attachments.Add($Image)
}
$Message = new-object Net.Mail.MailMessage
Add-PSSnapin Microsoft.Exchange.Management.Powershell.Admin -erroraction silentlyContinue
$att = new-object Net.Mail.Attachment($Image)
$att.ContentId = "att"
$smtp = new-object Net.Mail.SmtpClient($smtpServer, 587)
$smtp.Credentials = New-Object System.Net.NetworkCredential("me@gmail.com", "password"); 
$smtp.EnableSsl = $true
$body = '<img src="cid:att" />'
$Message.From = $EmailFrom
$Message.To.Add($SendTo)
$Message.Subject = $EmailSubject
$Message.Body = $body
$Message.IsBodyHTML = $true
$Message.Attachments.Add($att)
$smtp.Send($Message)
$att.Dispose()

编辑2:没关系,我有多个工作。不过,我觉得它不是那么好。我认为发送包含在文件夹中的所有内容更高效,然后将所有图片放在一个文件夹中,这样就可以有理论上无限量的附件。这是我现在拥有的代码,测试了10个(实际上是11个,因为0-10个是11个)图片,但是当我尝试了20张图片时没有出现在我的收件箱中。

$SendTo = "RECIEVER@gmail.com"
$SMTPServer = "smtp.gmail.com" 
$EmailFrom = “ME@gmail.com”
$EmailSubject = “SUBJECT”
$Message = new-object Net.Mail.MailMessage
Add-PSSnapin Microsoft.Exchange.Management.Powershell.Admin -erroraction silentlyContinue
$smtp = new-object Net.Mail.SmtpClient($smtpServer, 587)
$smtp.Credentials = New-Object System.Net.NetworkCredential("ME@gmail.com", "PASSWORD"); 
$smtp.EnableSsl = $true
$body = '<img src="cid:Images" />'
$Message.From = $EmailFrom
$Message.To.Add($SendTo)
$Message.Subject = $EmailSubject
$Message.Body = $body
$Message.IsBodyHTML = $true
$Images = @()
$Images += "0.png"
$Images += "1.png"
$Images += "2.png"
$Images += "3.png"
$Images += "4.png"
$Images += "5.png"
$Images += "6.png"
$Images += "7.png"
$Images += "8.png"
$Images += "9.png"
$Images += "10.png"

foreach ($Image in $Images) {
    $Message.Attachments.Add($Image)
}
$smtp.Send($Message)
$Images.Dispose()

编辑3: 感谢Swonkie,我们有这个代码可以创造奇迹:

最终工作代码

$message = @{
From = 'ME@gmail.com'
To = 'RECIEVER@gmail.com'
Subject = 'Subject'
Body = '<img src="cid:att" />'
BodyAsHtml = $true
Attachments = '0.png', '1.png', '2.png', '3.png','4.png', '5.png', '6.png', '7.png', '8.png', '9.png', '10.png'
}

$server = @{
    SmtpServer = 'smtp.gmail.com'
    Credential = New-Object System.Management.Automation.PSCredential('ME@gmail.com', (ConvertTo-    SecureString 'PASSWORD' -AsPlainText -Force))
    UseSsl = $true
}

Send-MailMessage @message @server

请注意,Gmail似乎有11个附件限制。

2 个答案:

答案 0 :(得分:1)

要添加多张图片,您可以多次使用$Message.Attachments.Add($att),但随后使用$att的所有不同图片。最简单的做法是循环。

$Images = @()
$Images += "0.png"
$Images += "1.png"
$Images += "2.png"
foreach ($Image in $Images) {
    $Message.Attachments.Add($Image)
}

答案 1 :(得分:1)

使用Send-MailMessage非常容易添加多个附件。

$message = @{
    From = 'me@gmail.com'
    To = 'receiver@gmail.com'
    Subject = 'Subject'
    Body = '<img src="cid:att" />'
    BodyAsHtml = $true
    Attachments = Get-ChildItem FolderWithAttachments
}

$server = @{
    SmtpServer = 'smtp.gmail.com'
    Credential = New-Object System.Management.Automation.PSCredential('me@gmail.com', (ConvertTo-SecureString 'password' -AsPlainText -Force))
    UseSsl = $true
}

Send-MailMessage @message @server

顺便说一下,因为我使用我的Google帐户进行双因素身份验证,所以我必须创建一个应用密码才能使用此功能。 This page解释了原因和方法。