I want the Email and Message in the Code to be sent to my Email "example#gmail.com'. I'm new to coding. Please help me to do this.
#echo off
color a
title Message Me
echo Instructions:
echo 1) Type the message and hit ENTER/RETURN
cd "C:\MessageMe"
set /p user=Email ID:
set /p message=Message:
echo Username="%user%" Message="%message%">Sent_Items.txt
echo Press any key to continue
pause >n
echo Loading.....
echo Enter E-Mail Again
set /p user=Email ID:
echo Username="%user%">Confirmed.txt
echo.
echo Press any key to Send
echo.
echo.
pause >
echo.
echo Processing...
echo.
echo.
echo.
echo.
echo.
pause >n
To send Emails from the Windows command-line, you can
...forget using raw telnet.exe as it cannot accept input from a file
...use a telnet.exe replacement like Albert Yale's telnet scripting tool
...use an actual command-line Email client like blat
...use PowerShell, like e.g. this1:
$EmailFrom = “yourgmailadress#gmail.com”
$EmailTo = “destination#somedomain.com”
$Subject = “The subject of your email”
$Body = “What do you want your email to say”
$SMTPServer = “smtp.gmail.com”
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“usr”, “pass”);
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
Related
Hi i'm trying to send email from batch file.bat but I'm getting a list of red errors and rolling really fast and the window close also fast I tried to keep it open by the command
cmd /k
and it's still open but it won't show any error list.
not that: I'm using Gmail account as smtp and i opened smtp settings and enabled the less secure login option.
finally what to run the bat is that command. cmd execute command :
file.bat "mygmail#gmail.com" "mypassword" "D:\test\myFile.txt"
file.bat contains :
#ECHO OFF
SET GmailAccount=%~1
SET GmailPassword=%~2
SET Attachment=%~3
CALL :PowerShell
CD /D "%PowerShellDir%"
Powershell -ExecutionPolicy Bypass -Command "& '%PSScript%' '%GmailAccount%' '%GmailPassword%' '%Attachment%'"
EXIT
:PowerShell
SET PowerShellDir=C:\Windows\System32\WindowsPowerShell\v1.0
SET PSScript=%temp%\~tmpSendeMail.ps1
IF EXIST "%PSScript%" DEL /Q /F "%PSScript%"
ECHO $Username = $args[0]>> "%PSScript%"
ECHO $EmailPassword = $args[1]>> "%PSScript%"
ECHO $Attachment = $args[2]>> "%PSScript%"
ECHO >> "%PSScript%"
ECHO $Username = $Username >> "%PSScript%"
ECHO $EmailTo = "target#mail.com" >> "%PSScript%"
ECHO $EmailFrom = "mygmail#gmail.com" >> "%PSScript%"
ECHO $Subject = "test" >> "%PSScript%"
ECHO $Body = "test" >> "%PSScript%"
ECHO $SMTPServer = "smtp.gmail.com" >> "%PSScript%"
ECHO $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom, $EmailTo, $Subject, $Body) >> "%PSScript%"
ECHO $Attachment = New-Object System.Net.Mail.Attachment($Attachment) >> "%PSScript%"
ECHO $SMTPMessage.Attachments.Add($Attachment) >> "%PSScript%"
ECHO $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) >> "%PSScript%"
ECHO $SMTPClient.EnableSsl = $true >> "%PSScript%"
ECHO $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($Username, $EmailPassword) >> "%PSScript%"
ECHO $SMTPClient.Send($SMTPMessage)
please Help me what is the problem here. Thanks.
First of all : This is a reminder for how to use less secure applications with google accounts
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 -----------------------------------------------------------------------------------------------------
So with the command line or a batch file we can call it as below :
PS-Gmail-Sender.bat "Mygmail_Account#gmail.com" "MyGmail_Password" "D:\test\myFile.txt"
I would like to ask for help if there is a way to send an e-mail notification when a scheduled ms access macro in task scheduler is in "Running" state for more than 20 minutes?
First thing that comes to mind is writing a second scheduled tasks to poll the runtime of the MS Access macro task.
If the task is over 20 minutes send out an email.
Another option might be to start a background job (Start-Job) in the MS Access code that will send the email after 20 minutes. And in your code, if the MS Access job succeeds you can cancel (Stop-Job) the email background task.
(There are many ways to skin a cat so just finding the best solution for the problem really)
Finally got it using powershell.
$ScheduledTaskName = "full path of your scheduled task e.g Folder1\Folder2\taskname"
$Result = (schtasks /query /FO LIST /V /TN $ScheduledTaskName | findstr "Result")
$Result = $Result.substring(12)
$Code = $Result.trim()
If ($Code -gt 0) {
$From = "MacroFails#DoNotReply.com"
$To = "Admin#Domain.com"
$Subject = "Scheduled task 'AppendCreatedSO' failed on SERVER"
$Body = "Error code: $Code"
$SMTPServer = "your SMTP Server"
$SMTPPort = "SMTP erver port"
Send-MailMessage -From $From -to $To -Subject $Subject -Body $Body -BodyAsHtml -SmtpServer $SMTPServer -Port $SMTPPort #-UseSsl -Credential (Get-Credential) #-Attachments $Attachment
}
The goal is to automate the execution of 3 files:
A batch file that runs a selenium webdriver automated test suite.
Another batch file that generates a reports for the tests run in step 1.
A PowerShell script that attaches the reports and sends an email.
How can I automate the execution of these 3 files so that upon running a command or executing a file, all 3 are executed?
This is how my PowerShell looks like:
$Username = "";
$Password = "";
function Send-ToEmail([string]$email, [string]$attachmentpath) {
$message = New-Object Net.Mail.MailMessage;
$message.From = "c#c.com";
$message.To.Add($email);
$message.Subject = "abc";
$message.Body = "abc";
$attachment1 = New-Object Net.Mail.Attachment($attachmentpath);
$message.Attachments.Add($attachment1);
$smtp = New-Object Net.Mail.SmtpClient("build3", "25");
$smtp.EnableSSL = $false;
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
$smtp.Send($message);
Write-Host $smtp.EnableSSL;
Write-Host "Mail Sent";
}
Send-ToEmail -email "a#a.com" -attachmentpath "C:\file1";
Send-ToEmail -email "b#b.com" -attachmentpath "C:\file1";
This is what the first batch file looks like:
FOR /F "TOKENS=2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET dd=%%A
FOR /F "TOKENS=2,3 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
FOR /F "TOKENS=2,3,4 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET yyyy=%%C
SET todaysdate=%yyyy%%mm%%dd%
start /d "nunitPATH" nunit3-console.exe "seleniumtestsuite dll PATH" --where:cat==SignUp --result="xmlreportPATH"
This is what the second batch file looks like:
start /d "exeFilePATH" ReportUnit.exe "folderPATH"
Simply write a batch script that runs
call script1.bat
call script2.bat
D:\path\to\powershell.exe -File script3.ps1
If you need async:
start script1.bat
start script2.bat
start "" "D:\path\to\powershell.exe" "-File" "script3.ps1"
Note the pair of double quotes before powershell.exe path.
Note that this assumes your device has at least powershell v2
However, this question is a duplicate of here
I am trying to create a batch file that will create a file that will send an email through powershell. It does many things, but email is just a snippet of it. Here is what I have so far:
REM Creates batch file with contents of the powershell script
echo "I need to add the below powershell script" > email.bat
echo "into email.bat with all the quotes included">> email.bat
echo "like what I'm doing now with appending text">> email.bat
REM powershell script to send email.
$smtp = new-object Net.Mail.SmtpClient("smtp.gmail.com")
if( $Env:SmtpUseCredentials -eq "true" ) {
$credentials = new-object Net.NetworkCredential("username","password")
$smtp.Credentials = $credentials
}
$objMailMessage = New-Object System.Net.Mail.MailMessage
$objMailMessage.From = "myemail#gmail.com"
$objMailMessage.To.Add("whereImsendingto#gmail.com")
$objMailMessage.Subject = "Logs for today"
$objMailMessage.Body = "(the logs)"
$smtp.send($objMailMessage)
In batch, I have the way to append text to another file down if what I'm adding does not have quotes, but I need to add text that includes quotes to the file.
Why don't you simply echo whithout quotes and escape possible chars <>|& with a caret ^ and double percent signs?
In a (code block) you've to also escape closing parentheses ^)
#Echo off
( Echo:$smtp = new-object Net.Mail.SmtpClient("smtp.gmail.com"^)
Echo:
Echo:if( $Env:SmtpUseCredentials -eq "true" ^) {
Echo: $credentials = new-object Net.NetworkCredential("username","password"^)
Echo: $smtp.Credentials = $credentials
Echo:}
Echo:$objMailMessage = New-Object System.Net.Mail.MailMessage
Echo:$objMailMessage.From = "myemail#gmail.com"
Echo:$objMailMessage.To.Add("whereImsendingto#gmail.com"^)
Echo:$objMailMessage.Subject = "Logs for today"
Echo:$objMailMessage.Body = "(the logs^)"
Echo:
Echo:$smtp.send($objMailMessage^)
) > "SomeFile.ps1"
I am making a program using batch and upon a crash or an error it will make a text file called debug.txt. I need to know if there is a way of emailing this file automatically using batch to the following email address "something#example.com". debug.txt is in the same location as the batch file. Does anyone know a code I could use. It must not have any extra softwere.
I see 3 options for you as of now:
1. The bottom line is there's no built-in way in batch, but there are third-party tools like blat etc. that can be called from a batch file, but as you have mentioned you don't want any extra software.
2. You can enable the installed SMTP Server of Windows. And then run a Powershell script:
$smtpServer = "system.abc.com"
$smtpFrom = "dontreply#abc.com"
$smtpTo = "something#abc.com"
$messageSubject = "Put your subject here"
$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $true
$message.Body = Get-Content debug.txt
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)
3. You can enable the installed SMTP Server of Windows. And then run a VBScript:
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const FileToBeUsed = "debug.txt"
Dim objCDO1
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(FileToBeUsed, ForReading)
Set objCDO1 = CreateObject("CDO.Message")
objCDO1.Textbody = f.ReadAll
f.Close
objCDO1.TO ="something#abc.com"
objCDO1.From = "dontreply#abc.com"
objCDO1.Subject = "Put your subject here"
objCDO1.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration /sendusing") = 2
objCDO1.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration /smtpserver") = "system.abc.com"
objCDO1.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration /smtpserverport") = 25
objCDO1.Configuration.Fields.Update
objCDO1.Send
Set f = Nothing
Set fso = Nothing
As you have mentioned that you are using Windows 7 ultimate so both option 2 and 3 will work on your system very well.
You can use free tool like sendemail.exe http://caspian.dotconf.net/menu/Software/SendEmail/. Download and copy sendemail.exe to system path. This is only for simple internal SMTP messages. This cannot be used to send external messages unless your Exchange server is setup to send anonymous external messages. Almost all Exchange servers are not setup to do this.
You can use this simple routine in the batch script.
CALL:SENDEMAILALERT "From SMTP address" "To SMTP addresses" "Subject" "Message" "File to attach" "smtp.host.com:25"
:SENDEMAILALERT
SET SENDEMAILCMD=-f "%~1"
SET SENDEMAILCMD=%SENDEMAILCMD% -t "%~2"
SET SENDEMAILCMD=%SENDEMAILCMD% -u "%~3"
SET SENDEMAILCMD=%SENDEMAILCMD% -m "%~4"
SET SENDEMAILCMD=%SENDEMAILCMD% -a "%~5"
SET SENDEMAILCMD=%SENDEMAILCMD% -s "%~6"
SENDEMAIL %SENDEMAILCMD% >NUL 2>&1
SET SENDEMAILCMD=
GOTO:EOF