Recently, I saw a new method of using emails online, as the title was found. I downloaded several of these software and found that several of them were either not easy to use or incomplete functions. I searched on the blog park and saw that there are implementations using Java and python. Here we use batch processing implementations of Windows.
The most basic function we want to implement is naturally to execute the cmd command. With this, everything else is easy to say.
Advantages of Windows Batch Processing:
1. A batch file, combined with third-party batch processing, can be run directly on almost all Windows computers.
2. The code is easy to write, the logic is relatively simple, and basically it is cmd command.
Disadvantages of batch processing:
1. We control remotely, and the commands sent by email are also commands. Due to the preprocessing mechanism of Windows command interpretation, the original batch command and the sent command (variable) will be mixed together. Here we will involve space issues, quotes, escape issues, etc. that are not very complicated but are always dizzy.
2. If the above step is not handled properly, syntax errors are likely to occur. If it is a lighter error, the command completion can also return an errorlevel to you. If it is a serious syntax error, it may directly cause the command line to crash. (There is nothing.) This problem is most likely to occur with the for and if commands.
1. Send and receive emails
Windows does not come with a program that can send and receive emails through the command line, so our program needs to come with a third-party command line. Here we use toolsgetmailCome to receive emails. getmail uses the pop3 protocol, which can download emails as txt and download its attachments.
Use the emailsblatconduct. blat uses SMTP to send emails, and also supports uploading attachments.
Can be entered--help
or/?
To get their detailed usage, you canRefer to this articleorRefer to this article. Although the translation is not very professional. Let me explain it briefly below.
How to receive emails in getmail
We use not every parameter in the help file. The following are several used in this example.
-u <userid>
Specify the login email account-pw <password>
Login password. In China, the common email addresses do not use email account passwords to directly serve as pop3/imap passwords. You usually need to go to the settings page to obtain them yourself.-s <server>
pop3 server. You can find it on the settings page of each email.-delete
Delete the downloaded email after downloading. If this parameter is not added, it will not be deleted.-xtract
Download the attachments that the email has and decode the plain text of the email content. Without adding this parameter, the attachment will not be downloaded, nor will the plain text be decoded. Only an MSG file will be downloaded, containing the information about the attachment, and the string obtained by the email content after base64 encoding is saved.-headersonly
Only download email header information, namely sender, receiver, email subject, etc. This will theoretically speed up the acquisition.-n <n>
Get n emails in total. It seems to be counting from the earliest email I received.
getmail can also write configurations to the registry, and use configurations in the registry every time in the future, which can simplify parameters, but I didn't use them this time.
Therefore, after we configure the above parameters, the echo obtained is as follows (there is no email on the server this time):
Failed to open registry key for GetMail profile , using default.
Failed to open registry key for GetMail
Getting *********@'s mailbox contents from server :110
There are 0 messages on the server.
Use of blat emails
There are many parameters. If you want to see the details, you can also visit the page mentioned above. This is only the one you will use.
<filename>
Write the first parameter directly after the command, specify a text file, and the contents of the message will be used as the contents of the message.
If you do not want to send content from the file, enter only the above parameter-
, you can add a parameter to the following-body "<Email Content>"
。-to <address>
The recipient's email address.-charset <cs>
Text encoding. In order to correctly send Chinese, we fixed the parameter to add-charset gbk
Specifies to use GBK encoding.-subject
The subject of the email.-server
Enter the smtp server address and can be found in the email settings interface.-f
From, specify the email address used to send the login.-u
Username to log in to your email address. Most of them are the part before your email address @. If the login is not successful, please search for the help interface of your email address.-pw
Login password. The same password as the getmail above.-attach
Attach attachment to the mail.
2. Email address used by the computer
Our strategy is that the computer uses an email address independently, and you can use other email addresses to send messages to this address to achieve control.
I recommend that the computer use Sina email, which can register multiple independent email addresses with a mobile phone number. Moreover, the connection is relatively stable, and there are few cases of unsuccessful acquisition/send. The 5s email acquisition interval is stress-free and will not be blocked.
There are almost no restrictions on the email address of the sender, but the DingTalk's own DingTalk cannot be used here because the subject of the email will be encrypted together (or it uses utf8 encoding, which can't be remembered). It is more troublesome to read the batch directly. The ones that have been tried and used are Alibaba Email and QQ Email. 163 should be easy to use, but I haven't tried it.
3. Overview of principles
3.1 Execute the command
Since the subject is not encrypted in the text file received by getmail, the content is encoded by base64. So the initial plan was to only read the subject and put all the commands in the subject.
The first function that the program needs to implement is to execute the cmd command. We will add several custom functions later, and we need to specify the functions we selected here through commands. My implementation method here is to use #number to separate it, and the function selection is wrapped with the first #, and the added parameters are placed after the second #. In batch processing, you can use the for command to get these two strings separately.
For example, we name the function of executing the cmd commandcmd
, commands need to be executedstart
Then the subject of our email will be entered as:#cmd#start
After this email is downloaded by getmail, the line that appears in the file is:Subject: #cmd#start
We interpret the input through for:
echo off for /f "tokens=2,* delims=#" %%i in ('type ^| findstr /b Subject:') do ( set mode=%%i set para="%%j" ) echo mode:%mode% echo command:%para% pause
Get the result:
mode:cmd
command:"start "
Then we call cmd to execute this command. It is best to open a new cmd here. Add min to minimize running.
start /MIN /c %para%
We can also call another bat file, which will also open a new cmd window. At the same time, some commands can be written and executed together. Echoes can also be input into the file and sent out using blat, so that echoes can be seen on the email side.
At the same time, it is best to open a new batch operation when executing other functions. In this way, if the execution of commands takes a long time, or the executed commands are running in the background, the process of checking emails will not be blocked, and other commands can still be executed by email.
3.2 File transfer
This is relatively simple. As long as the -xtract parameter is added to getmail, the attachment will be downloaded directly. To upload attachments using blat, we can name them as the upfile function and use if to judge%mode%
, if it is upfile, another batch execution blat is called, and the sent file name is attached to-attach
Just do it.
Using this function, we can also send batch files and write multiple commands to the file to achieve batch execution of commands. passstart
The command calls this batch. It should be noted that some mailboxes (such as Sina Emailbox) will automatically intercept emails such as bat extensions and other executable programs as attachments. The solution is also very simple. You can change the file extension and send it, for example, to .txt. After receiving the attachment, execute the renaming command via email, change it back to the extension, and then run it.
3.3 The commands containing Chinese
With Chinese subject, it cannot be displayed directly in msg file. For example, it will be displayed as:
Subject: =?UTF-8?B?4oCq4oCqZGltb0BhbGl5dW4uY29t4oCs4oCs?=
This way it is more troublesome to decode. The following content can be directly seen in Chinese after decoding using base64.-xtract
After adding parameters, the content will be automatically decoded, which is more convenient. Therefore, we can enter commands in the email body and execute the program after reading it.
However, the content decoded by getmail is html (Click to view details), it is more troublesome to read text directly in this batch. There are solutions to the previous page.
3.4 Hide run
It's also relatively simple. Use the vbs command to completely hide the black box of cmd, and at the same time you can obtain UAC administrator permissions.
Here suppose we want to run:
REM Hide run only echo set ws=("") > echo "%~ /start",0 >> del /f /q REM Hide run and get administrator permissions ECHO SET UAC = CreateObject^(""^) > ECHO "", "Here you can add a parameter", "", "runas", 0 >> del /f /q
3.5 Power on and prevent shutdown
Starting and running can be achieved by setting a task plan. You can use the task scheduler to configure tasks in a windowed manner, or you can use the schtasks command to write a batch to implement one-click addition of tasks. At the same time, we can also send reminder emails when the program starts to monitor the startup time.
rem The batch file that needs to be started here is set file='%~' schtasks /Create /SC ONLOGON /TN \Windows\MailService /TR "%file%" /F /RL HIGHEST /DELAY 0001:00 rem Delay startup is used to prevent the computer from failing to send the boot email. pause
Regarding the issue of preventing processes from being killed, welcomeClick here,orrefer to。
3.6 Configuration File
Since many different batch files need to accept/send emails, we need to write the email address, login username, and password into a configuration file to facilitate email sending and receiving. Of course, you can also use programs to store configurations in the registry function.
In the configuration file, we only need to write different configurations to a separate line and read them separately using batch processing, which also facilitates file editing.
The for command can read each line of a file and perform the same operation on each line. To use for to read the contents of a single line, you need to add a goto to the end of the execution to jump out of the for command. Use such for multiple times to read the contents of each line of the configuration file. The relevant content is visibleWeb links。
3.7 More features
We can also add more practical features throughif
Judgment andgoto
Jump to function.
For example, we want to pop up a prompt box through the command, the code is relatively long and the input is inconvenient.
mshta vbscript:msgbox("content",64,"title")()
At this time, you can save the command to bat. Name the function popup and use if to judge%mode%
Just do it. After jumping, execute the corresponding bat file and send the displayed content to bat as parameters. For example, we stipulate that $ is used as the delimiter character, and then when sending emails, enter:#popup#title$64$content
The main program separates the input according to # and determines that it needs to jump to popup; then it will receive the input:"title$64$content"
At this time, you can divide the input by dividing the input to get each part of the content and use it for pop-up windows:
echo off for /f "tokens=1,2,3 delims=$" %%i in ('echo %~1') do ( set tit=%%i set num=%%j set text=%%k ) mshta vbscript:msgbox("%text%",%num%,"%tit%")() exit
4. Final code
Because of the use of many functions, there are also many third-party and bat files placed in a program folder.
Click to view the code
All the following codes can be clicked to expand like this.
echo off cd /d "%~dp0" echo set ws=("") > echo "%~ /start",0 >> rem Send startup reminder email; read configuration file :euser for /f "eol=# tokens=* delims=" %%i in () do ( set euser=%%i goto ename ) :ename for /f "eol=# skip=4 tokens=* delims=" %%i in () do ( set ename=%%i goto epw ) :epw for /f "eol=# skip=6 tokens=* delims=" %%i in () do ( set epw=%%i goto smtp ) :smtp for /f "eol=# skip=10 tokens=* delims=" %%i in () do ( set smtp=%%i goto eto ) :eto for /f "eol=# skip=12 tokens=* delims=" %%i in () do ( set eto=%%i goto getcfgend ) :getcfgend set subj="[MailCTRL]%DATE% %TIME% %COMPUTERNAME%" echo host has started.> echo for more info:>> echo date and time:%DATE% %TIME%>> echo computer:%COMPUTERNAME%>> echo userdomain:%USERDOMAIN%>> echo username:%USERNAME%>> echo -------------------->> systeminfo >> ipconfig >> set content= ::------------------ blat %content% -to %eto% -charset gbk -subject %subj% -server %smtp% -f %euser% -u %ename% -pw %epw% del /f /q %content% del /f /q exit
cd /d "%~dp0" del /F /Q z*.todo del /F /Q Extract*.out del /F /Q html*.out @echo off timeout /t 3 cls echo ""> ::echo %~dp0> echo ########################## echo setting email service...... :euser for /f "tokens=* delims=" %%i in () do set cfgfile=%%~i echo setted cfgfile:%cfgfile% for /f "eol=# tokens=* delims=" %%i in (%cfgfile%) do ( set euser=%%i goto epw ) :epw for /f "eol=# skip=6 tokens=* delims=" %%i in (%cfgfile%) do ( set epw=%%i goto pop ) :pop for /f "eol=# skip=8 tokens=* delims=" %%i in (%cfgfile%) do ( set pop=%%i goto getcfgend ) :getcfgend echo service started successfully AT %DATE% %TIME% echo ------------------------------------ :see TIMEOUT /T 5 echo checking new messages at %TIME% for /f "skip=3 tokens=3 delims=# " %%i in ('getmail -u %euser% -pw %epw% -s %pop% -headersonly') do set newmsg=%%i echo new message received:%newmsg% if %newmsg% GEQ 1 goto get goto see :get set mode= set para= del /F /Q MSG*.TXT del /F /Q Extract*.out echo downloading the new messages... getmail -u %euser% -pw %epw% -s %pop% -delete -xtract -n 1 :: -delete :tell for /f "tokens=2,* delims=#" %%i in ('type ^| findstr /b Subject:') do ( set mode=%%i set para="%%j" ) set htext=%RANDOM% del /f /q html%htext%.out echo use ------------------------ html2txt html%htext%.out echo ---------------------------------------- echo information read from : echo mode: %mode% echo command: %para% echo html file:html%htext%.out echo RUNNING THE PROGRAM...... ::if %mode%==cmd goto directcmd ::if %mode%==back goto backcmd ::if %mode%==xcmd goto xcmd ::if %mode%==xback goto xbackcmd if %mode%==cmd goto textcmd if %mode%==back goto textback if %mode%==xcmd goto textX if %mode%==xback goto textXback if %mode%==popup goto popup if %mode%==poptext goto poptext if %mode%==upfile goto upfile if %mode%==use goto changecfg ::if %mode%==dir goto changedir rem There are still some functions that have not been developed。There are several functions that are replaced below。 goto see :directcmd start /MIN %para% goto see :backcmd start /MIN %para% goto see :xcmd set xmark=%RANDOM% echo %para%> z%xmark%.todo ECHO SET UAC = CreateObject^(""^) > ECHO "", "z%xmark%", "", "runas", 0 >> echo using vbs to run an admin command. del /f /q goto see :xbackcmd set xmark=%RANDOM% echo %para%> z%xmark%.todo ECHO SET UAC = CreateObject^(""^) > ECHO "", "z%xmark%", "", "runas", 0 >> echo using vbs to run an admin command. del /f /q goto see :textcmd start /MIN %htext% goto see :textback start /MIN %htext% goto see :textX ECHO SET UAC = CreateObject^(""^) > ECHO "", "%htext%", "", "runas", 0 >> echo using vbs to run an admin command. del /f /q goto see :textXback ECHO SET UAC = CreateObject^(""^) > ECHO "", "%htext%", "", "runas", 0 >> echo using vbs to run an admin command. del /f /q goto see :popup start /MIN %para% goto see :poptext start /MIN %htext% goto see :upfile start /MIN %para% goto see :changecfg echo %para%> goto euser :changedir start /MIN %htext% goto see
rem Used for command echo。 echo off cd /d "%~dp0" :euser for /f "tokens=* delims=" %%i in () do set cfgfile=%%~i echo setted cfgfile:%cfgfile% for /f "eol=# tokens=* delims=" %%i in (%cfgfile%) do ( set euser=%%i goto ename ) :ename for /f "eol=# skip=4 tokens=* delims=" %%i in (%cfgfile%) do ( set ename=%%i goto epw ) :epw for /f "eol=# skip=6 tokens=* delims=" %%i in (%cfgfile%) do ( set epw=%%i goto smtp ) :smtp for /f "eol=# skip=10 tokens=* delims=" %%i in (%cfgfile%) do ( set smtp=%%i goto eto ) :eto for /f "eol=# skip=12 tokens=* delims=" %%i in (%cfgfile%) do ( set eto=%%i goto getcfgend ) :getcfgend for /f "tokens=* delims=" %%i in ('EnTextChange -Text:"html%"') do ( set todo=%%i goto out ) :out del /f /q html% set remark=re%RANDOM% %todo%> %remark%.txt echo ----------------------------->> %remark%.txt echo the cmd you run BY ADMIN: %todo%>> %remark%.txt blat %remark%.txt -to %eto% -charset gbk -subject [MailCTRL]command"%TIME%" -server %smtp% -f %euser% -u %ename% -pw %epw% timeout /t 5 del /f /q %remark%.txt exit
echo off ::Need to display Chinese,Please useANSIcoding cd /d "%~dp0" for /f "tokens=1,2,3 delims=$" %%i in ('EnTextChange -Text:"html%"') do ( set tit=%%i set num=%%j set text=%%k ) ::del /f /q html% mshta vbscript:msgbox("%text%",%num%,"%tit%")() pause exit
rem Used to upload files echo off cd /d "%~dp0" :euser for /f "tokens=* delims=" %%i in () do set cfgfile=%%~i echo setted cfgfile:%cfgfile% for /f "eol=# tokens=* delims=" %%i in (%cfgfile%) do ( set euser=%%i goto ename ) :ename for /f "eol=# skip=4 tokens=* delims=" %%i in (%cfgfile%) do ( set ename=%%i goto epw ) :epw for /f "eol=# skip=6 tokens=* delims=" %%i in (%cfgfile%) do ( set epw=%%i goto smtp ) :smtp for /f "eol=# skip=10 tokens=* delims=" %%i in (%cfgfile%) do ( set smtp=%%i goto eto ) :eto for /f "eol=# skip=12 tokens=* delims=" %%i in (%cfgfile%) do ( set eto=%%i goto getcfgend ) :getcfgend blat - -body "The file you sent on %TIME% by %USERNAME% on computer:%COMPUTERNAME%. Used email address:%euser%" -to %eto% -charset gbk -subject [MailCTRL]file:%1 -server %smtp% -f %euser% -u %ename% -pw %epw% -attach %~1 exit
Only some of the files are displayed here. If you want to view all files, please download it.
MailCTRL Download
This is the article about Windows batch processing to realize remote control of mail (third-party batch processing). For more related content on Windows batch processing, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!