I've been working on maybe thousands of lines of code for the Send-MailMessage cmdlet, and have only gotten 1 to work. I've researched maybe more that 50 sites, and very rarely does their code work.
First of all, here's the code that works for me:
$EmailFrom = “myself@gmail.com”
$EmailTo = “olivia@myplace.com”
$Subject = “PowerShell test of sending an email using Gmail”
$Body = “PowerShell test of sending an email using Gmail”
##$Att = “D:\DOWNLOAD\xcopy.text”
$SMTPServer = “smtp.gmail.com”
##$SMTPServer = "$PSEmailServer"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“myself@gmail.com”, “pwd”);
##$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body, $Att)
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
Notice that I have the Attachments line commented out because it doesn't work if it's in.
Question #1: How do I send an attachment?
Question #2: I use Thunderbird for my email client so I can see (from account settings) that the smtp server is: smtp.verizon.net; and the port is: 465. I am using Windows 10 x64 (all up-to-date). So,
if I use this code:
$EmailFrom = “myself@verizon.net”
$EmailTo = “olivia@myplace.com”
$Subject = “PowerShell test 3004 of sending an email”
$Body = “PowerShell test 3004 of sending an email”
##$Attachments = "D:\DOWNLOAD\xcopy.txt"
$SMTPServer = “smtp.verizon.net”
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 465)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“myself@verizon.net”, “pwd”)
##$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body, $Attachments)
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
and it doesn't work. On the $SMTPClientCredentials = New... line I also tried "myself" instead of "myself@verizon.net" to no avail. With the "myself@verison.net" in it, the error I get is:
Exception calling "Send" with "4" argument(s). "The operation has timed out." At ... line 14 char 1
which is the last line. Recently, I have also gotten other errors (using the smtp.verizon.net server) that signified "unauthorized ..." and "access denied" so I added any email addresses I use to the Verizon white-list in
MyVerizon.
Also, I can't even seem to use Help. I ran this: Update-Help from the ISE which did a lot of updating, but if I do these:
get-help $SMTPClient -or- get-help $emailMessage
-or- get-help $PSEmailServer
it just bombs out. I just got this to work: get-help send-MailMessage
Y-a-a-a-y!!!
Question #3: Is there a way to see exactly what smtp client is being used in my PC or by PowerShell? I tried to show the properties of this: $SMTPClient so I can see what the system has stored, but couldn't
do it?
If you are so kind as to answer my questions, please don't leave out any characters or lines of code as I know very little about these variables and PowerShell (i.e., don't assume I know something that is completely obvious to others), and I'm having a real
hard time with my trying to send an email message from the command line using PowerShell. I've been using 2 other slightly out-of-the-mainstream programs for my job but neither of them offers a way to send an attachment.
I can always use my script that works with gmail, but it has been really bugging me why the Verizon script won't work. Whole days have gone by while I plugged away with this problem, and then I almost panic.
Thanks Very Greatly in advance. crank1948