Curtis Long
asked on
Send email with .bat file
I'm attempting to create a .bat file that will send a simple email with no attachments. So far ive gotten this script:
If at all possible, i would like to avoid using 3rd party applications to run this function.
Any help is majorly appreciated, thanks.
$EmailFrom = "Sender"
$EmailTo = "Recipient"
$Subject = "New Login Detected"
$Body = "Body"
$SMTPServer = "mail.gmail.com"
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("sender", "password");
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
this is running from Windows Server 2019 standard, and i will say I'm not very experienced in this area. when i run this , i receive this errorIf at all possible, i would like to avoid using 3rd party applications to run this function.
Any help is majorly appreciated, thanks.
put real email addresses and try again...
if thats not the case, make sure, you have internet and the ports involved here are not blocked by firewalls...
if network / ports are ok,
then go to gmail settings and allow 3rd party email send permissions...
gmail does not allow sending emails by default, you have to set it up first...
then go to gmail settings and allow 3rd party email send permissions...
gmail does not allow sending emails by default, you have to set it up first...
ASKER
Thanks for the reply Kurt
That script example is one that i have taken all of the sensitive info out of and replaced with generic info, my actual batch file does contain my email, login, and recipient info.
None of the above listed ports are blocked
i will check out the gmail settings, i was not aware of that setting.
That script example is one that i have taken all of the sensitive info out of and replaced with generic info, my actual batch file does contain my email, login, and recipient info.
None of the above listed ports are blocked
i will check out the gmail settings, i was not aware of that setting.
the access for less secure apps must be enabled in the account that you are adding to the software.
check this, and make sure you use from address same as login
you cannot send email on behalf of others...
check this, and make sure you use from address same as login
you cannot send email on behalf of others...
Less secure apps & your Google Account
https://support.google.com/accounts/answer/6010255?hl=enASKER
im not using 3rd party apps, im only using powershell.
ive allowed the less secure apps on my gmail, still having the same error.
any other ideas?
ive allowed the less secure apps on my gmail, still having the same error.
any other ideas?
looks like google wants you to create an app password and login with that instead of main account/pwd
have a look at here...
I had similar issues 2 years ago, and resolved it with my account settings, but do not remember the details...
or it may be changed at all...
have a look at here...
I had similar issues 2 years ago, and resolved it with my account settings, but do not remember the details...
or it may be changed at all...
Sign in with App Passwords
https://support.google.com/accounts/answer/185833
you should turn off 2-Step Verification
or create an app login/password to login...
or create an app login/password to login...
Protect your account with 2-Step Verification
https://support.google.com/accounts/answer/185839m not using 3rd party apps, im only using powershell
you are a 3rd part app for google :)
the web browser, gmail app or iphone considered secure, the rest is 3rd party, no matter where and what it is...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@Jose
the issue is not the code...
GMail does not allow using their SMTP/account with any untrusted/3rd party code/app...
To make it work, you have to set your account as `less secure`
or create an app level login/password so you can use this credentials
the issue is not the code...
GMail does not allow using their SMTP/account with any untrusted/3rd party code/app...
To make it work, you have to set your account as `less secure`
or create an app level login/password so you can use this credentials
Hello HainKurn the error I see is code. :) I'm aware of what you're saying, but the point is to make the asker aware of it :)
https://support.google.com/accounts/answer/6010255?hl=en
https://support.google.com/accounts/answer/6010255?hl=en
ASKER
Jose,
For the gmail script, how do i embed the gmail password into the .bat file?
Thanks
PS. Sorry for the late response.
For the gmail script, how do i embed the gmail password into the .bat file?
Thanks
PS. Sorry for the late response.
OK
There's a difference:
BAT => are MS-DOS old files
PS1 => Powershell
The code here is "Powershell script," so the correct extension is ".ps1" and not ".bat."
The Gmail password is hardcoded on line 5.
This can't be embedded into a "bat."
This is a PS script.
you can either use it to run Scheduled task using this:
https://social.technet.microsoft.com/wiki/contents/articles/38580.windows-task-scheduler-configure-to-run-a-powershell-script.aspx
There's a difference:
BAT => are MS-DOS old files
PS1 => Powershell
The code here is "Powershell script," so the correct extension is ".ps1" and not ".bat."
The Gmail password is hardcoded on line 5.
This can't be embedded into a "bat."
This is a PS script.
you can either use it to run Scheduled task using this:
https://social.technet.microsoft.com/wiki/contents/articles/38580.windows-task-scheduler-configure-to-run-a-powershell-script.aspx
@jose
no matter what you do, if you do not setup your gmail account properly,
as they explain, I gave the links
you cannot send email using gmail servers!
no matter what you do, if you do not setup your gmail account properly,
as they explain, I gave the links
you cannot send email using gmail servers!
Dear @HainKurt
I'm not sure why are you referring to me :)
Talk to the author. I do know that is required, and I put a link too.
https://support.google.com/accounts/answer/6010255?hl=en
Once Again, I do know what is required to configure, but does the asker?.
I'm not sure why are you referring to me :)
Talk to the author. I do know that is required, and I put a link too.
https://support.google.com/accounts/answer/6010255?hl=en
Once Again, I do know what is required to configure, but does the asker?.
:) this is what I get...
author tries to send email using gmail account
and not successful doing so...
because, gmail does not allow any other app to send emails using any tool
unless people set their account less secure
or create an app level login/password and use that one...
so, that's the issue...
problem is not "how to send email from some app"
if author has any other login/pwd on another server, he can use that one with current code...
author tries to send email using gmail account
and not successful doing so...
because, gmail does not allow any other app to send emails using any tool
unless people set their account less secure
or create an app level login/password and use that one...
so, that's the issue...
problem is not "how to send email from some app"
if author has any other login/pwd on another server, he can use that one with current code...
haha I assume that he has done and taken your consideration, that's why I haven't insisted on it :)
I mean, Your answer is valid if that's the issue :) but the PS code is complaining about Code :) that's why I answered the correct code.
There's no overload for that .send that accepts 4 parameters and that's the error in red that I'm reading.
I mean, Your answer is valid if that's the issue :) but the PS code is complaining about Code :) that's why I answered the correct code.
There's no overload for that .send that accepts 4 parameters and that's the error in red that I'm reading.
your ps code is missing $creds, I dont see where you set/initialize...
Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port $SMTPPort -Credential $creds -UseSsl
No sir is not missing :)
it's here:
The variables are in lines: 4 and 5 and the above is line 20 :)
And on the O365 is on line 9 :) keep trying to be fishy hahahaa! :P
it's here:
$Smtp.Credentials = New-Object System.Net.NetworkCredential($Username,$Password)
The variables are in lines: 4 and 5 and the above is line 20 :)
And on the O365 is on line 9 :) keep trying to be fishy hahahaa! :P