4

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 = “Myemail@outlook.com”
$EmailTo = “Someonesemail@gmail.com"
$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

Hackoo
  • 18,337
  • 3
  • 40
  • 70
MrTortol
  • 37
  • 1
  • 1
  • 5
  • Does this answer your question? [Send email with PowerShell](https://stackoverflow.com/questions/54000217/send-email-with-powershell) – Daniel Björk Aug 10 '20 at 06:31
  • Duplicate: https://stackoverflow.com/questions/54000217/send-email-with-powershell – Daniel Björk Aug 10 '20 at 06:31
  • 1
    SmtpClient is [obsolete](https://learn.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient?view=netcore-3.1). Use [Send-MailMessage](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/send-mailmessage?view=powershell-7) instead. – vonPryz Aug 10 '20 at 06:32
  • 3
    Start with [replacing all smart quotes](https://stackoverflow.com/q/6968270/1701026) it might lead to unexpected behavior depending on the PowerShell version you work with. – iRon Aug 10 '20 at 07:00
  • Thanks guys for the help! (Also do you guys have any idea how to send it with an attachment?) – MrTortol Aug 10 '20 at 07:34

1 Answers1

2

You should try something like that first :

$Username  = "Myemail@outlook.com"
$EmailPassword = "YourRealPassword of your email"
$Attachment= "C:\Test.txt" # Example you can change its path here instead of "c:\Test.txt"
$EmailTo = "Someonesemail@gmail.com" 
$EmailFrom   = "Myemail@outlook.com" 
$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                       = 'sender@yourdomain.com'
    To                         = 'recipient@yourdomain.com', 'recipient@NotYourDomain.com'
    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 "Mygmail_Account@gmail.com" "MyGmail_Password" "D:\test\myFile.txt"
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • User is already using PowerShell, so there's no need for a `.bat` script at all. – Bill_Stewart Aug 10 '20 at 16:52
  • 1
    @Bill_Stewart Yes ! Just this is an option but what i want to say is the most impotant thing about using `smtp.gmail.com` to activate the less secure application ! – Hackoo Aug 10 '20 at 16:57
  • omg tysm. I actually needed a .bat file, but both works fine! tysm man. – MrTortol Aug 11 '20 at 01:42
  • finnaly i can make something to spam my friend with lewds – MrTortol Aug 11 '20 at 01:51
  • @MrTortol Please take Stack Overflow (SO) [tour](https://stackoverflow.com/tour) to learn how to say thanks on SO by accepting an answer on being helpful by clicking on gray check mark symbol left to an answer. See also [How does accepting an answer work?](https://meta.stackexchange.com/questions/5234/) A question with no accepted answer is rated as a not answered question – Hackoo Aug 27 '20 at 17:40