Hi Ronaldo
I use PowerShell to send emails
First I create some files in my Harbour program:
1 - script file (EMAILS_SKRYPT.PS1)
2 - body file (EMAILS_BODY.HTML)
3 - run file (EMAILS_RUN.BAT)
At the end I execute RUN( 'EMAILS_RUN.BAT' )
At the beginning you can edit and run it from Windows, then you start programming to create and run files from your program.
Sample files:
------ EMAILS_RUN.BAT ------
@echo off
powershell.exe -executionpolicy remotesigned -File emails_skrypt.ps1
------ EMAILS_BODY.HTML ------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<center>
<table>
<tr><td colspan="2" class="tdtyt">Invoice info</td></tr>
<tr><td colspan="2">Hi, there is your invoice:</td></tr>
<tr><td>Number</td><td>Invoice1<br>Invoice2</td></tr>
<tr><td colspan="2" class="tdend">This is a test message</td></tr>
</table>
</center>
</body>
</html>
------ EMAILS_SKRYPT.PS1 ------
# function to store info in a log file
Function Write-Log {
[CmdletBinding()]
Param (
[string]$Label,
[string]$Message
)
$Stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
$Line = "$Stamp $Label $Message"
Add-Content $logmail -Value $Line
}
# new message
$logmail = 'emails_snd.log'
$nError = 0
$server = 'mail.foo.bar'
$port = 587
$user = 'smtp_user'
$pass = 'smtp_pass'
$from = 'fr...@foo.bat'
$repl = 're...@foo.bar'
$subiect = 'There is a subject'
$priority = 'Normal'
try
{
$body = get-content -Raw 'emails_body.html'
}
catch
{
$nError = 1
Write-Log '[ERROR_MSG]' $_.Exception.Message
}
# TLS 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$smtp = New-Object Net.Mail.SmtpClient($server, $port)
$smtp.Credentials = New-Object System.Net.NetworkCredential($user, $pass)
$smtp.EnableSsl = $True
$msg = New-Object Net.Mail.MailMessage
try
{
$msg.From = $from
}
catch
{
$nError = 1
Write-Log '[ERROR_MSG]' $_.Exception.Message
}
try
{
$msg.Subject = $subiect
}
catch
{
$nError = 1
Write-Log '[ERROR_MSG]' $_.Exception.Message
}
try
{
$msg.Body = $body
}
catch
{
$nError = 1
Write-Log '[ERROR_MSG]' $_.Exception.Message
}
$msg.Priority = $priority
$msg.IsBodyHtml = $True
try
{
$msg.ReplyTo = $repl
}
catch
{
$nError = 1
Write-Log '[INFO]' 'RE: "r...@foo.bar"'
Write-Log '[ERROR_ADR]' $_.Exception.Message
}
# receivers
try
{
$msg.To.Add('cli...@gmail.com')
}
catch
{
$nError = 1
Write-Log '[INFO]' 'TO: "cli...@gmail.com"'
Write-Log '[ERROR_ADR]' $_.Exception.Message
}
try
{
$msg.Bcc.Add('b...@foo.bar')
}
catch
{
$nError = 1
Write-Log '[INFO]' 'BCC: "xx...@foo.bar"'
Write-Log '[ERROR_ADR]' $_.Exception.Message
}
# attachments
try
{
$msg.Attachments.Add('Invoice1.pdf')
}
catch
{
$nError = 1
Write-Log '[INFO]' 'ADD: "Invoice1.pdf"'
Write-Log '[ERROR_ZAL]' $_.Exception.Message
}
try
{
$msg.Attachments.Add('Invoice2.pdf')
}
catch
{
$nError = 1
Write-Log '[INFO]' 'ADD: "Invoice2.pdf"'
Write-Log '[ERROR_ZAL]' $_.Exception.Message
}
# send
if($nError -eq 0)
{
try
{
$smtp.Send($msg)
}
catch
{
$nError = 1
Write-Log '[ERROR_SND]' $_.Exception.Message
}
}
if($nError -eq 0)
{
Write-Log '[OK]' 'Sent'
}
I hope it will help.
Regards
Grzegorz Wojnarowski