Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send an email with an attachment in PowerShell code?

I am trying to write a PowerShell code that sends an email to specified email adress. When i run the code it just displays

+ $SMTPClient.Send <<<< ($EmailFrom, $EmailTo, $Attachment, $Subject, $Bod)
+ CategoryInfo          : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest

Oh and btw this is my code

$EmailFrom = “[email protected]”
$EmailTo = “[email protected]"
$Attachment = "(Filepath)"
$Subject = “Test”
$Bod = “Test”
$SMTPServer = “smtp.outlook.com”
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“Email”,“Pass”); $SMTPClient.Send($EmailFrom, $EmailTo, $Attachment, $Subject, $Bod)

Please help. Im suffereing

like image 769
MrTortol Avatar asked Sep 21 '25 11:09

MrTortol


1 Answers

You should try something like that first :

$Username  = "[email protected]"
$EmailPassword = "YourRealPassword of your email"
$Attachment= "C:\Test.txt" # Example you can change its path here instead of "c:\Test.txt"
$EmailTo = "[email protected]" 
$EmailFrom   = "[email protected]" 
$Subject = "PowershellTest"
$Body= "PowershellTest"
$SMTPServer  = "smtp.outlook.com"  
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom, $EmailTo, $Subject, $Body) 
$Attachment  = New-Object System.Net.Mail.Attachment($Attachment)
$SMTPMessage.Attachments.Add($Attachment)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($Username, $EmailPassword) 
$SMTPClient.Send($SMTPMessage)

EDIT : Using PowerShell’s Send-MailMessage cmdlet

Refer to How to Send Email with Office 365 Direct Send and PowerShell

using PowerShell’s Send-MailMessage cmdlet :

You’ll first need to define a PowerShell PScredential object then provide all of the parameters that Send-MailMessage needs.

# Get the credential
$credential = Get-Credential
## Define the Send-MailMessage parameters
$mailParams = @{
    SmtpServer                 = 'smtp.office365.com'
    Port                       = '587' 
    UseSSL                     = $true
    Credential                 = $credential
    From                       = '[email protected]'
    To                         = '[email protected]', '[email protected]'
    Subject                    = "SMTP Client Submission - $(Get-Date -Format g)"
    Body                       = 'This is a test email using SMTP Client Submission'
    Attachment                 = 'C:\Test.txt' # Here you can change your attachment
    DeliveryNotificationOption = 'OnFailure', 'OnSuccess'
}

## Send the message
Send-MailMessage @mailParams

When you run the code above, you should receive an email received by the internal recipient (yourdomain.com) and the external domain (notyourdomain.com) with Attachment.


IMPORTANT REMARK :

If you encounter this problem while sending a mail with the smtp of google, you should take a look at this Can not send mail using smtp.gmail.com, port 587 from vbs script

You can use batch and powershell to send an email : First copy and paste this code as

PS-Gmail-Sender.bat

@ECHO OFF
REM https://stackoverflow.com/questions/28605803/can-not-send-mail-using-smtp-gmail-com-port-587-from-vbs-script/28606754#28606754
Title Sending E-Mail with Gmail Less Secure Applications using Powershell and Batch
SET GmailAccount="%~1"
SET GmailPassword="%~2"
SET Attachment="%~3"
REM We write our Powershell script 
CALL :WritePS
REM We execute our Powershell script .PS1 by passing arguments from the command line or a batch file
Powershell -ExecutionPolicy bypass -noprofile -file "%PSScript%" "%GmailAccount%" "%GmailPassword%" "%Attachment%"
IF EXIST "%PSScript%" DEL /Q /F "%PSScript%"
pause
EXIT
REM -----------------------------------------------------------------------------------------------------
:WritePS
SET PSScript=%temp%\temp_SendeMail.ps1
> "%PSScript%" (
    ECHO $Username  = $args[0]
    ECHO $EmailPassword = $args[1]
    ECHO $Attachment= $args[2]
    ECHO $EmailTo = $Username
    ECHO $EmailFrom  = $Username
    ECHO $Subject = "This email was sent from Powershell script into a batch file with Less Secure Application Enabled"   
    ECHO $Body= "Test Email Sending with a script"  
    ECHO $SMTPServer  = "smtp.gmail.com"  
    ECHO $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom, $EmailTo, $Subject, $Body^) 
    ECHO $Attachment  = New-Object System.Net.Mail.Attachment($Attachment^)
    ECHO $SMTPMessage.Attachments.Add($Attachment^)
    ECHO $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587^)
    ECHO $SMTPClient.EnableSsl = $true
    ECHO $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($Username, $EmailPassword^) 
    ECHO $SMTPClient.Send($SMTPMessage^)
)
Exit /B
REM -----------------------------------------------------------------------------------------------------

Second You can call this batch file from the command line or you can also save another batch file as Sendit.bat and execute by double click in order to call the first script with arguments PS-Gmail-Sender.bat

PS-Gmail-Sender.bat "[email protected]" "MyGmail_Password" "D:\test\myFile.txt"
like image 146
Hackoo Avatar answered Sep 23 '25 04:09

Hackoo