Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending email using Batch file

Tags:

batch-file

I have my outlook configured with my office id and am extremely new to batch scripting. What is the simplest way (simplest code) to send an email via batch file to my colleague.

thanks

like image 829
Vinod Chelladurai Avatar asked Dec 01 '25 04:12

Vinod Chelladurai


1 Answers

I can see 3 options for you:

  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.

  2. You can enable the installed SMTP Server of Windows. And then run a Powershell script:

    $smtpServer = "system.abc.com"
    $smtpFrom = "[email protected]"
    $smtpTo = "[email protected]"
    $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 ="[email protected]"
    objCDO1.From = "[email protected]"
    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
    

Choice is yours.

like image 144
Sunny Avatar answered Dec 02 '25 19:12

Sunny