0

I am trying to send mail from PowerShell without being promoted for user and password (I want it to be automatically) I am using the code bellow but I get the error The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required.
When I just enter Get-Credentials it works fine This is my code


    $pass = ConvertTo-SecureString “mypassword” -AsPlainText -Force
    $From = "uname@gmail.com"
    $To = "uname@gmail.com"
    $Attachment = "C:\hosts.txt"
    $Subject = "files from script"
    $Body = "none"
    $SMTPServer = "smtp.gmail.com"
    $SMTPPort = "587" 
    $Cred = New-Object System.Management.Automation.PSCredential (“uname@gmail.com”, $pass)
    Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -BodyAsHtml -SmtpServer $SMTPServer -Port $SMTPPort -Credential $cred -Attachments $Attachment -UseSsl

Eran Avni
  • 25
  • 1
  • 7
  • 1
    Is the GMail account [configured so](https://stackoverflow.com/q/32260/503046) that less-secure applications can use it too? – vonPryz May 04 '20 at 06:32
  • Last time I did this I had to wait for three days after configuring gmail for less secure applications. Very weird or Gmail's AI. – Aaron May 05 '20 at 03:48

1 Answers1

0

Store your creds in a secure file, the registry or Windows credential manager, and call from there as needed. Plenty of examples all over the web.

• Securing Credentials and Passwords

Securely Store Credentials on Disk

Quickly and securely storing your credentials – PowerShell

Save Encrypted Passwords to Registry for PowerShell

Manipulate credentials in the Windows 8/2012 PasswordVault using Powershell PasswordVault.psm1

Powershell: How to encrypt and store credentials securely for use with automation scripts

Sample from the last link:

<# Set and encrypt credentials to file using default method #>

$credential = Get-Credential
$credential.Password | 
ConvertFrom-SecureString | 
Set-Content c:scriptsencrypted_password1.txt

<# 
    Set some variables
    ...
#>
$emailusername = "myemail"
$encrypted = Get-Content c:scriptsencrypted_password.txt | 
ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PsCredential($emailusername, $encrypted)

if($something = $somethingElse)
{
    <#
        Do some stuff
        ...
    #>

    $EmailFrom = "myemail@gmail.com"
    $EmailTo = "myemail+alerts@gmail.com"
    $Subject = "I did some stuff!" 
    $Body = "This is a notification from Powershell." 
    $SMTPServer = "smtp.gmail.com" 
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) 
    $SMTPClient.EnableSsl = $true 
    $SMTPClient.Credentials = $credential;
    $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
}

This is a very common thing to do to limit the need to enter creds manually/multiple times and very well documented, with modules in the MS powershellgallery.com to use for this type of thing.

Find-Module -Name '*credential*' | 
Format-Table -AutoSize
<#
# Results

Version        Name                          Repository Description 
-------        ----                          ---------- -----------    
2.0            CredentialManager             PSGallery  Provides access to credentials ...
1.0.4          WindowsCredential             PSGallery  Management module for Windows Credential Store.
...
1.1.0          CredentialSpec                PSGallery  Tools to create and find Credential Spe ...
4.5            BetterCredentials             PSGallery  A (compatible) major upgrade for Get-Credential,  ...
1.0.11         pscredentialmanager           PSGallery  This module allows management and autom ...
...                     
1.0.0          CredentialLocker              PSGallery  CredentialLocker is a module that provide ...    
1.1            CredentialsManager            PSGallery  The module Credentials Manager provides y ...
1.2.2.20190715 SimplyCredential              PSGallery  Simply Module for windows credentials.        
1.0.2          CredentialManagement          PSGallery  Manage Credentials stored in the Windows Credential Manager
...
1.1.0          PSCredentialTools             PSGallery  PSCredentialTools provides various methods for ...
1.0.0.0        SelectCredential              PSGallery  A module for selecting the credential stor ...     
2.1            SecuredCredential             PSGallery  SecuredCredential Routines for modules suppor ....
1.0.477        PSCredentialStore             PSGallery  A simple credential manager to store and  ...
...                                             
3.0            CredentialUtility             PSGallery  This is a credential manager tool which co ...
1.3            MiCredentialModule            PSGallery  Saves/Retrieves credentials to/from a file (w ...
1.3            vaultcredential               PSGallery  Manages credentials in the credential vault       
0.0.1          SecureCredentials             PSGallery  This module allow to secure store encrypted  ... 
#>
postanote
  • 15,138
  • 2
  • 14
  • 25