Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass credentials to the Send-MailMessage command for sending emails

Tags:

powershell

I'm having difficulty passing my credentials to the Send-MailMessage command

This is what I am running:

Send-MailMessage -smtpServer smtp.gmail.com -from '[email protected]' `     -to '[email protected]' -subject 'Test' -attachment C:\CDF.pdf 

it errors with below the message which is obviously because I have not passed my gmail credentials

Send-MailMessage : 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. 

I googled a bit and also went through the man page of Send-MailMessage and found that the "-credential" parameter needs to be passed.

My issue is: HOW ?

I tried with Get-Credentials as below:

$mycredentials = Get-Credential 

Then entered my usrname and password for gmail in the box that pops up.

then I run below command:

Send-MailMessage -smtpServer smtp.gmail.com -credentail $mycredentials `   -from '[email protected]' -to '[email protected]' -subject 'Test' -attachment C:\CDF.pdf 

and still it fails with the exact same error.

So I need help from you guys on how do I pass my Credentials to the Send-MailMessage command. I learned about PScredentials but not exactly sure what it is and how to use it in this context.

like image 372
slayedbylucifer Avatar asked Sep 17 '12 14:09

slayedbylucifer


People also ask

How do I pass credentials in PowerShell MailMessage?

I was able to make it work with this command: PS > Send-MailMessage -smtpServer smtp.gmail.com -Credential $credential -Usessl true -from '[email protected]' -to '[email protected]' -subject 'Testing' -attachment C:\CDF. pdf .

How do I pass credentials in PowerShell without prompt?

$cred = Get-Credential without asking for prompts in powershell - Microsoft Tech Community.

How does PowerShell send-MailMessage work?

The Send-MailMessage cmdlet uses the From parameter to specify the message's sender. The To parameter specifies the message's recipients. The Cc parameter sends a copy of the message to the specified recipient. The Bcc parameter sends a blind copy of the message.

What can I use instead of MailMessage?

NET MailKit. The most generic method that would replace SmtpClient and Send-MailMessage would be the recommended replacement, which is MailKit. This is a third-party, open-source library but maintained by a Microsoft employee and officially recommended for use in the documentation.


1 Answers

I found this blog site: Adam Kahtava
I also found this question: send-mail-via-gmail-with-powershell-v2s-send-mailmessage
The problem is, neither of them addressed both your needs (Attachment with a password), so I did some combination of the two and came up with this:

$EmailTo = "[email protected]" $EmailFrom = "[email protected]" $Subject = "Test"  $Body = "Test Body"  $SMTPServer = "smtp.gmail.com"  $filenameAndPath = "C:\CDF.pdf" $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body) $attachment = New-Object System.Net.Mail.Attachment($filenameAndPath) $SMTPMessage.Attachments.Add($attachment) $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)  $SMTPClient.EnableSsl = $true  $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("username", "password");  $SMTPClient.Send($SMTPMessage) 

Since I love to make functions for things, and I need all the practice I can get, I went ahead and wrote this:

Function Send-EMail {     Param (         [Parameter(`             Mandatory=$true)]         [String]$EmailTo,         [Parameter(`             Mandatory=$true)]         [String]$Subject,         [Parameter(`             Mandatory=$true)]         [String]$Body,         [Parameter(`             Mandatory=$true)]         [String]$EmailFrom="[email protected]",  #This gives a default value to the $EmailFrom command         [Parameter(`             mandatory=$false)]         [String]$attachment,         [Parameter(`             mandatory=$true)]         [String]$Password     )          $SMTPServer = "smtp.gmail.com"          $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)         if ($attachment -ne $null) {             $SMTPattachment = New-Object System.Net.Mail.Attachment($attachment)             $SMTPMessage.Attachments.Add($SMTPattachment)         }         $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)          $SMTPClient.EnableSsl = $true          $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($EmailFrom.Split("@")[0], $Password);          $SMTPClient.Send($SMTPMessage)         Remove-Variable -Name SMTPClient         Remove-Variable -Name Password  } #End Function Send-EMail 

To call it, just use this command:

Send-EMail -EmailTo "[email protected]" -Body "Test Body" -Subject "Test Subject" -attachment "C:\cdf.pdf" -password "Passowrd" 

I know it's not secure putting the password in plainly like that. I'll see if I can come up with something more secure and update later, but at least this should get you what you need to get started. Have a great week!

Edit: Added $EmailFrom based on JuanPablo's comment

Edit: SMTP was spelled STMP in the attachments.

like image 122
Nick Avatar answered Oct 09 '22 03:10

Nick