使用Powershell发送多个Gmail附件 [英] Sending Multiple Gmail Attachments with Powershell

查看:229
本文介绍了使用Powershell发送多个Gmail附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题几乎可以解释它。现在,我从其他网站收集了这些信息:

  $ SendTo =me@gmail.com
$ SMTPServer =smtp.gmail.com
$ EmailFrom =me@gmail.com
$ EmailSubject =主题
$图片=0.png
$留言= new-object Net.Mail.MailMessage
Add-PSSnapin Microsoft.Exchange.Management.Powershell.Admin -erroraction quietlyContinue
$ 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()

但它没有工作,它给了我错误:

 带有1参数的异常调用发送:SMTP服务器需要
的安全连接,或者客户端不是
认证的服务器响应是:5.7.0必须首先发出STARTTLS命令p56sm3035297qta.18 - gsmtp
在A:\ScreenshotEmail .ps1:19 char:1
+ $ smtp.Send($ Message)
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo:NotSpecified:(:) [],MethodInvocationException
+ FullyQualifiedErrorId:SmtpException

我也想要发送多个图片,0.png是第一个,我希望能够添加m任何,如二十,所有这些都是这样的:


0.png
1.png
2.png


等等。

编辑:
LotPings'和MarliesV的代码,这就是我所拥有的。它工作,但它只发送1.png,而不是两个。

  $ SendTo =receiver @ gmail .com
$ SMTPServer =smtp.gmail.com
$ EmailFrom =me@gmail.com
$ EmailSubject =主题
$图片= @( )
$图片+ =0.png
$图片+ =1.png
foreach($图片$图片){
$ Message.Attachments.Add ($ Image)
}
$ Message = new-object Net.Mail.MailMessage
Add-PSSnapin Microsoft.Exchange.Management.Powershell.Admin -erroraction quietlyContinue
$ att =新对象Net.Mail.Attachment($ Image)
$ att.ContentId =att
$ smtp =新对象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 $ b $ Message.Attachments。添加($ 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 = @( )
$图片+ =0.png
$图片+ =1.png
$图片+ =2.png
$图片+ =3 .png
$图片+ =4.png
$图片+ =5.png
$图片+ =6.png
$图片+ = 7.png
$图片+ =8.png
$图片+ =9.png
$图片+ =10.png

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

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

最后工作代码



  $ message = @ {
From ='ME@gmail.com'
To ='RECIEVER@gmail.com'
Subject ='Subject'
Body ='< img src =cid:att/> gt ;'
BodyAsHt ml = $ true
附件='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个附件限制。

解决方案

使用<$ c添加多个附件非常简单$ c> Send-MailMessage 。

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


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

Send-MailMessage @message @server

顺便说一句,因为我使用Google帐户使用双因素身份验证,所以我必须为此创建一个应用程序密码上班。 此页面解释了原因和方式。


Title pretty much explains it. Right now from other sites I have gathered this:

$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()

but it doesn't work, it gives me the error:

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

I also want to send multiple pictures, "0.png" is the first, and I want to be able to add however many, like twenty, all in order like this:

0.png 1.png 2.png

And so on.

EDIT: After adding LotPings' and MarliesV's code, this is what I have. It works, but it only sends 1.png, not both. I will have it send even more, but I'm only using two here for example.

$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()

EDIT 2: Never mind, I got multiple to work. I feel like it is not as good as it could be, however. I think it would be more efficient to send everything that is contained in a folder instead, then put all of the pictures in a folder, so that there could be a theoretical infinite amount of attachments. Here's the code I have right now, tested working with 10 (actually 11, because 0-10 is 11) pictures, but did not appear in my inbox when I tried 20 pictures.

$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()

EDIT 3: Thanks to Swonkie we have this code which works wonders:

Final Working Code

$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

Be aware that Gmail seems to have an 11 attachment limit doing this.

解决方案

Adding multiple attachments is very easy with 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

By the way, because I use 2-factor authentication with my Google account, I had to create an app password for this to work. This page explains why and how.

这篇关于使用Powershell发送多个Gmail附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆