how can attach file to body of outlook email by using cmd batch file command create vbs, body part by html code
Assistant
User
โดยปกติฉันจะลากไฟล์วางที่ body ไฟล์นั้นจะถูกวางไว้ใน body ในทางกลับกันถ้าฉันต้องการวางลักษณะแบบนี้ด้วย command batch file
Assistant
หากคุณต้องการส่งอีเมลผ่าน Outlook โดยมีไฟล์หนึ่งหรือมากกว่าแนบอยู่ในเนื้อหาของอีเมลในลักษณะคล้ายกับการลากไฟล์วาง (drag and drop) โดยใช้ไฟล์ Batch (command batch file) และ VBScript คุณจะต้องตั้งค่าแนบไฟล์ใน VBScript โดยตรง เนื่องจากไม่สามารถส่งไฟล์ในขณะที่กำหนดใน ของอีเมลในลักษณะนี้ได้ ดังนั้นเราจึงสามารถแสดงที่อยู่ของไฟล์ในข้อความของอีเมลแทน
To attach a file to an Outlook email and use HTML for the body content through a CMD batch file that creates a VBScript, you can follow these steps.
Step 1: Create a Batch File
Create a batch file (e.g., send_email.bat) with the following content:
batch
@echo off
setlocal
:: Set email parameters
set "recipient=recipient@example.com"
set "subject=Your Subject Here"
set "bodyFile=body.html"
set "attachment=C:\path\to\your\file.txt"
:: Create HTML body file
echo ^<html^> > %bodyFile%
echo ^<body^> >> %bodyFile%
echo ^<h1^>This is an HTML Email^</h1^> >> %bodyFile%
echo ^<p^>This is a paragraph in the body of the email.^</p^> >> %bodyFile%
echo ^</body^> >> %bodyFile%
echo ^</html^> >> %bodyFile%
:: Create VBS script to send email
set "vbsFile=%temp%\sendEmail.vbs"
(
echo Set objOutlook = CreateObject("Outlook.Application")
echo Set objMail = objOutlook.CreateItem(0)
echo objMail.To = "%recipient%"
echo objMail.Subject = "%subject%"
echo objMail.HTMLBody = CreateObject("Scripting.FileSystemObject").OpenTextFile("%bodyFile%", 1).ReadAll()
echo objMail.Attachments.Add "%attachment%"
echo objMail.Send
echo WScript.Echo "Email sent."
) > "%vbsFile%"
:: Run the VBS script
cscript //nologo "%vbsFile%"
:: Clean up
del "%vbsFile%"
del "%bodyFile%"
endlocal
Explanation
Setting parameters: The script starts by defining the recipient's email, the subject line of the email, the path to the file you wish to attach, and a temporary HTML file for the body of the email.
Creating HTML file: It generates a simple HTML file containing the body of the email.
Creating VBScript: The script generates a VBScript that uses the Outlook application to create a new email item, specify the recipient, subject, HTML body, and attachment.
Executing the VBScript: Finally, it runs the VBS script using cscript which sends the email and then cleans up by deleting temporary files.
Step 2: Run the Batch File
Double-click the send_email.bat file to execute it. If Outlook is installed and configured on your system with the proper permissions set, it should send the email as intended.
Notes
Outlook Settings: Make sure that your Outlook is set up to allow sending emails programmatically.
Permissions: Depending on your security setting, you may encounter permission prompts in Outlook.
File Path: Ensure that the paths for your attachment and any files are correctly referenced.
HTML Content: Modify the HTML content as needed by editing the lines where the HTML is written to body.html.