Clicky

20240701

Select file and email to yourself with one click

My current attachment emailscript uses the MailMessage method, but this method is obsolete (see: https://github.com/dotnet/platform-compat/blob/master/docs/DE0005.md). So I followed the recommendation, and used the MailKit Powershell Module to send emails.

Updates:

  1. Use of MailKit to send emails (requires installation of the MailKit Powershell Module).
  2. Added an "Install" procedure. Automate user settings.
  3. Added a "setPassword" procedure. Secure password entry and password storage in USERPROFILE (no plain text password anywhere).

Works with:

  • Windows Explorer Context menu (right click file and send email)
  • Command line (file parameter must have absolute/complete file path)
  • Drag and drop (create a Deaktop shortcut to the psEmailAttachment.ps1 script and drag a file to the shortcut be emailed)

 Installation procedure:

  1. Install the MailKit Module (e.g.: PS C:\> Install-Module -force -Name "Send-MailKitMessage" -Scope CurrentUser)
  2. Store the psEmailAttachment.ps1 script in some folder
  3. Update the email server properties and your email address in the psEmailAttachment.ps1 script
  4. Run:
    • Powershell -file psEmailAttachment.ps1 -install     #--- Create the Explorer context menu entry
    • Powershell -file psEmailAttachment.ps1 -setPassword #--- Setting the password of the email sender account.
Done.

Sending an email from Windows Explorer-> Right click filename:

psEmailAttachment.ps1

using module Send-MailKitMessage
<#

    To install the MailKit:
    PS C:\> Install-Module -force -Name "Send-MailKitMessage" -Scope CurrentUser #--- Install Module only for this user. User privileges only.
   
    Or:
    PS C:\> Install-Module -force -Name "Send-MailKitMessage" -Scope AllUsers           #--- Install Module for all users. Administrative privileges needed.
   
    Emailing examples shamelessly stolen from: https://github.com/austineric/Send-MailKitMessage

#>

param(
    [SWITCH]$install,
    [SWITCH]$setPassword
)    
   
#--- User variables...
$SMTPServer         = "{FQDN or IP address from email server}"
$Port               = 587 #---> STARTTLS or 465 SSL/TLS...
$emailSenderAccount = "emailAttachment@ddress.ext"
$To                 = "yourEmail@ddress.ext"


#=== MAIN =====================================================================================================
#--- Static variables...
$TextBody           = "Attachment sent with $($MyInvocation.MyCommand.Path) script."
$scriptPath         = $MyInvocation.MyCommand.Path

#--- Create password filename in AppData...
$scriptName         = (Get-Item $scriptPath).Basename
$appDataFolder      = "$ENV:USERPROFILE\AppData\Local\$scriptName"
new-item -type directory -path $appDataFolder -force | out-null
$smtpPasswordFile   = "$appDataFolder\$emailSenderAccount-password.dat"

if ($setPassword) {

    write-host "*** Setting encrypted password..."
   
    #--- Read and store encrypted password in $smtpPasswordFile...
    $readPassword     = Read-Host -assecurestring "Enter sender email account's ($($emailSenderAccount)) password now"
    $password         = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($readPassword))
    $password | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File $smtpPasswordFile
    $password         = $null
   
    write-host "*** Ready."
    Exit
   
}


if ($install) {
   
    #--- Note: writing in "HKEY_CLASSES_ROOT" requires Administrative privileges...
   
    #--- Some Powershell Registry black magic, setting Windows Explorer context menu...
    write-host "*** Setting Explorer context menu settings..."
    new-item         -Path Registry::"HKCR\``*\Shell"              -Name "Email to $To" -force | out-null
    set-ItemProperty -Path Registry::"HKCR\``*\Shell\Email to $To" -Name "Icon"    -Value "%systemroot%\system32\dsuiext.dll,16" -force | out-null
    new-item         -Path Registry::"HKCR\``*\Shell\Email to $To" -Name "command" -Value "powershell -ExecutionPolicy Bypass -noprofile -file ""$scriptPath"" ""%1"" " -force | out-null
   
    write-host "*** Right-click a filename in Windows Explorer and select: 'Email to $To'"
    write-host "*** Ready."
    Exit
   
}    


if ($args.count -EQ 0) {
   
    write-host "*** No attachment data provided."
    break
   
}    


#--- From...
$From = [MimeKit.MailboxAddress]$emailSenderAccount
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $emailSenderAccount, (Get-Content $smtpPasswordFile | ConvertTo-SecureString)

#--- To...
$RecipientList = [MimeKit.InternetAddressList]::new()
$RecipientList.Add([MimeKit.InternetAddress]$To)

#--- Attachment...
$Attachment = $args[0]
$AttachmentList = [System.Collections.Generic.List[string]]::new()
$AttachmentList.Add($Attachment)
$Subject = "Attachment: $Attachment"

#--- Send email...
write-host "*** Emailing attchment '$Attachment' to $To..."
$UseSecureConnectionIfAvailable = $true
Send-MailKitMessage -SMTPserver $SMTPServer -Credential $Credential -Port $Port -From $From -RecipientList $RecipientList -Subject $Subject -textBody $TextBody -AttachmentList $AttachmentList
write-host "*** Ready."

start-sleep 2

 

Real Time Web Analytics