Friday, July 6, 2007

How to send mail in local desktop


How to send mail in local desktop

Sometimes we may need/must send mail from our scripts/program. In some case, we can use the packages or functions supplied by software vendor, Perl for example. But without the specific env, nothing can be done. The following solution is to use the 3rd tool “blat” to implement the mail-send function with any coding env, Perl/C++/WSH and whatever.

With the existing SMTP server
1. Install the blat.exe
As mentioned above, we need to first download the blat first. You can find the installation file as the blow link.
http://downloads.sourceforge.net/blat/blat262.full.zip?modtime=1175256820&big_mirror=0

You can find the syntax here
http://www.blat.net/syntax/syntax.html

2. After finish the tool setup, we need to configuration the tool env. Using the follow command line to specify the SMTP server and the sender’s mail address

Blat.exe –install SMTP-server sender-address
SMTP-server: the SMTP server name. 
sender-address: it will be showed in the sender field. Any well-formatted address can be designate here. Pls be noted that the address can not be used to receive mail since it is just a dummy one.

After setup, you can test it using below command
Blat.exe body-file-name –to your-mail-address –subject mail-subject
body-file-name: one file which contains the mail body

Cheers if you receive the mail ~~.

3. Execute the command in your script/program.
#here is the Perl code sample
#!C:\perl\perl.exe
# separate the mail address with ‘,’
$maillist= "testing@company.com";
$mailsubject= "Testing Message";
$mailbody="mailbody.log";
SendMail ($mailsubject,$ mailbody)

sub SendCQMail
{
 #$_[0] is the mail subject
 #s_[1] is the mail body. It is the contend of the importlog or exportlog
$cmdresult=`C:\\Blat250\\full\\blat.exe "$_[1]" -to $maillist -subject "$_[0]"`;
 print $cmdresult;
        }


'Here is the VBS sample
mailSub="Testing _Message"
mailaddress=" testing@company.com"
mailbody="mailbody.log"
SendMail mailbody,mailaddress,mailSub

Public Sub SendMail(body,mailid,subject)
  command="C:\Blat250\full\blat.exe " & body & " -to " & mailid & " -subject " & subject
  ExeCommand command 
End Sub

' The function is to execute the shell command and return the result
Public Function ExeCommand(commandline)
  On Error Resume next
   Set WshSheell=CreateObject("WScript.Shell")
   Set WshScriptExec=WshSheell.Exec(commandline)
   Do While True
      If WshScriptExec.StdOut.AtEndOfLine Then
       Exit Do
      Else
       ExeCommand=WshScriptExec.StdOut.ReadAll
      End If
   Loop
   Set WshScriptExec=Nothing
   Set WshSheell=Nothing
End Function

Without the existing SMTP server
As described above, we need to assign the SMTP server before using the blat.exe. The following example is to show how to setup one SMTP server in the local desktop.

1. Install the IIS component in local desktop.
     You can install the service under Control Panel->Add or Remove Programs->Add New Programs-> Add/Remove Windows Components->Internet Information Service

2.  Configure SMTP server
     Go to Control Panel->Administrative Tools->Internet Information Services, open the properties of Default SMTP Virtual Server.

     Click the Access Tab

  1. Authentication setting: Check the Anonymous access option
  2. Connection: Check the All except the list below
  3. Relay: Check the All except the list below

3. Follow the above instructions of With the existing SMTP server and just point to the SMTP server to local in the blat.exe –install

That’s it. Cheers~~

No comments: