PowerShell 5.0 Send-MailMessage with Attachment - powershell

The following command successfully sends an e-mail using the parameters specified in the command:-
Send-MailMessage -From "<mailbox#domain.com>" -To "<mailbox#domain.com>" -Subject "Sending the Attachment" -Body "Forgot to send the attachment. Sending now." -SmtpServer "fqdn.smtprelay.com"
As soon as I add any kind of attachment, regardless of size, file extension (.txt, .zip etc.) the command apparently completes without generating any kind of error into the console output but the e-mail never hits the SMTP relay when I track the mail, it's almost as if the command fails but according to PowerShell it hasn't, it executes the command without any kind of output.
This is the command I'm using:
Send-MailMessage -From "<mailbox#domain.com>" -To "<mailbox#domain.com>" -Subject "Sending the Attachment" -Body "Forgot to send the attachment. Sending now." -SmtpServer "fqdn.smtprelay.com" -Attachments "C:\Temp\targetfile.zip"
Does any body have any advice on how I can go about troubleshooting further? It seems to be such a simple issue but I feel that I have now exhausted all options. I have verified that the target file exists and can be accessed from the PowerShell console.
Also, I can successfully establish a telnet connection to the SMTP relay on port 25 without authentication and generate a basic e-mail, the problem seems to be when I add an attachment.

New hosted e-mail security system was rejecting mail based on security policies, because the attachment was an unzipped HTML file attached to the e-mail it was being rejected as a potential security threat.
Added new code to firstly compress file before attaching to e-mail and sending - now works perfectly.

Related

Is it possible to send attachment via email using Powershell without SMTP? [duplicate]

Is it possible to send attachment using Send-PnPMail in Powershell ?
Send-PnPMail -To $requestor -Subject "Teams App Provisioning Delta Load - $(Get-Date)" -Body "Requested Teams App for $($user) Provisioned Successfully"

Powersheel script to fetch logs from remote desktop

We are working on one application and for that application there is one module called replicator and this replicator shows the status whether failed or successful in every h hrs it runs. So, I want to create a powershell script which will check the logs from my remote desktop server and email me the status of replicator .
Can somebody help me in this.
Are the logs in a text file? Does it have a particular format? Something like:
$logline = Get-Content \\path\to\file.txt -Last 1
Send-MailMessage -smtpserver smtp0 -from me#me.com -to me#me.com -Subject "Latest Result is $logline"

PowerShell send-mailmessage

How do I terminate cmdlet prompts for To[] address?
It keeps asking for more!
this is what I see:
send-mailmessage
cmdlet Send-MailMessage at command pipeline position 1
Supply values for the following parameters:
From: ABCD#boo.edu
Subject: test
To[0]: DCBA#foo.com
To[1]: jhgjjg#trtre.com
To[2]:???????
You have to at least include the SMTP server argument in the command because you won't get a prompt for it:
Send-MailMessage -SmtpServer "yoursmtpserver"
Once you've done that, enter your 'to' addresses as normal, and when you're done, press enter once more to escape from the final prompt and send the email.
Better yet, just enter the entire command with parameters included:
Send-MailMessage -From "ABCD#boo.edu" -To #("DCBA#foo.com", "jhgjjg#trtre.com") -Subject "test" -Body "Testing PS Email" -SmtpServer "yoursmtpserver"

Error in sending an email with BLAT

I have this code:
blat -to test#test.com -server -f test#test.com -subject "subject" -body "body" -attach DATA.log
It uses blat to send a email but it gives the error:
Not enough arguments supplied
Does any one know what I'm doing wrong? Thanks.
Try like this :
The server is setted here for GMX. You have to set it for your provider.
blat.exe -server SMTP.GMX.COM -f your_E-mail_address -to Destination_E-mail_adress -s "cc text" -body "body text" -u "Login of your E-mail" -pw "Password of your E-mail"
http://www.blat.net/syntax/syntax.html
And here a list of the most commons SMTP and POP Servers :
http://www.arclab.com/en/amlc/list-of-smtp-and-pop3-servers-mailserver-list.html
Edit :
Apparently you'll need stunnel to provide the Secure Sockets Layer (SSL) as required by GMail.
You can also try with an another provider like GMX who worked for me.
Or you can try Mailsend :
https://github.com/muquit/mailsend/
It seems you are trying to send via mail() check the error log. You have to add an authenticate method with the port number and security type.
you can find all free SMTP server list here: https://www.weblydigital.com/free-smtp-server-list-public-smtp-host/

Running Powershell script from batch - Send-MailMessage not functioning

I am facing unusual issue with Powershell script. I have written script to run DB queries which runs one STORED Procedure. Below is the complete script:
$server="DBServer,Inst"
$Database = "DATABASE"
$ConnectionTimeout = 30
$QueryTimeout = 120
$Query="STORED_PROCEDURE"
Import-Module “sqlps” -DisableNameChecking
$ds=Invoke-Sqlcmd -Query $Query -Database $Database -ServerInstance $server -ConnectionTimeout $ConnectionTimeout -QueryTimeout $QueryTimeout
$date=(Get-Date).ToString()
$ret=$ds.Return
--Query returns 0 or 1, 0 means SP Passed otherwise failed.
$From="abc#xyz.com"
$SendCC= "aabbcc.b#xyz.com"
If ($ret -eq '0')
{
$Body=" STORED_PROCEDURE: <FONT COLOR=#00FF00> <b>PASS </b> </FONT> in <b> $server</b>:<i>$Database</i>; TimeStamp: $date"
$msg="Test: STORED_PROCEDURE - SUCCESS "
Write-Host "Sending Mail Notification - Ret 0"|
Send-MailMessage -From $From -To $SendCC -Subject $msg ($Body |Out-String) -BodyAsHtml -SmtpServer mail.xyz.com
Write-Host "Notification Sent!!"
}
This script works absolutely fine from my computer which is in xyz domain. I have created batch file to run this script as:
#pushd "D:\Temp\SM_SP"
powershell -ExecutionPolicy Bypass -File .\SP_SM.ps1 -noconsole
#popd
Even this batch file works fine in my computer, sends mail.. Works as expected! But the real issue is running the same in one of the test server (win Ser 2008 R2). Where the batch file when executed manually returns no errors – however I am not getting any mail notification.
Note-
• I have mentioned proper smtp server details in script, as I said the same script and batch are working fine in my computer which is in the same domain as this server.
• I have tested Powershell inbuilt command “Send-MailMessage” in the server as:
Send-MailMessage -From $from -To $to -Subject Test -SmtpServer mail.xyz.com
Where $from and $to both are my address and am receiving the mail absolutely fine. But in the server when I execute the above script in Powershell console or through wrapped Batch file – am not receiving mail.
Any help on this is greatly appreciated. Thanks in advance!
Regards
-Raaj
I've run into this issue several times as a result of Receive Connector settings in Exchange. We typically will just add the IP of a server to allow it to relay if we are going to start using it for sending email, otherwise we want connections refused.
If you're using exchange go to Server Configuration->Hub Transport and check what you have listed under "Receive mail from remote servers that have these IP addresses" on the Network tab. If you're using some other mail server I'd refer to the documentation on connection permissions for it. Also, if you're using a different user account on the server than you are using on your desktop you may want to try a Run as different user and see if it's something with the permissions of the account you're using on the server.