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.
To add multiple images you can just use the
$Message.Attachments.Add($att)
multiple times, but then with all the different images for$att
. It would be easiest to do it in a loop.Adding multiple attachments is very easy with
Send-MailMessage
.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.