Real author:
Dorian Ocsovszki –
Check my blog out if you liked this post. Thanks and enjoy!
My goal was to have a nice and easy email notification. MQL[45] did not suit me, so I decided to code this in C#. Basically I pass every variable from MQL[45] to this DLL. That is all. You can find the DLL in the ZIP Archive.
This DLL has only one Function shown below:
#import "SendAdvancedEmail.dll" Â Â void SendAdvancedEmail(string MailFrom, string MailFromName, string MailTo, string MailCC, string MailSubject, string MailBodyContent, string MailBodyTemplate, string MailPriority, string MailAttachmentPath, string MailAttachmentName, string SMTPServer, int SMTPPort, bool SMTPEnableSSL, int SMTPTimeout, string SMTPUsername, string SMTPPassword); #import
SendAdvancedEmail.dll
using System;
using System.Text;
using RGiesecke.DllExport;
using System.Runtime.InteropServices;
using System.Net.Mail;
using System.IO;
namespace SendAdvancedEmail
{
   class CSendAdvancedEmail
   {
       [DllExport(“SendAdvancedEmail”, CallingConvention = CallingConvention.StdCall)]
       public static void SendAdvancedEmail(
           [MarshalAs(UnmanagedType.LPWStr)] string MailFrom,
           [MarshalAs(UnmanagedType.LPWStr)] string MailFromName,
           [MarshalAs(UnmanagedType.LPWStr)] string MailTo,
           [MarshalAs(UnmanagedType.LPWStr)] string MailCCArray,
           [MarshalAs(UnmanagedType.LPWStr)] string MailSubject,
           [MarshalAs(UnmanagedType.LPWStr)] string MailBodyContent,
           [MarshalAs(UnmanagedType.LPWStr)] string MailBodyTemplate,
           [MarshalAs(UnmanagedType.LPWStr)] string MailPriority,
           [MarshalAs(UnmanagedType.LPWStr)] string MailAttachmentPath,
           [MarshalAs(UnmanagedType.LPWStr)] string MailAttachmentName,
           [MarshalAs(UnmanagedType.LPWStr)] string SMTPServer,
           int SMTPPort,
           bool SMTPEnableSSL,
           int SMTPTimeout,
           [MarshalAs(UnmanagedType.LPWStr)] string SMTPUsername,
           [MarshalAs(UnmanagedType.LPWStr)] string SMTPPassword
           )
       {
           SmtpClient client = new SmtpClient();
           client.Port = SMTPPort;
           client.Host = SMTPServer;
           client.EnableSsl = SMTPEnableSSL;
           client.Timeout = SMTPTimeout;
           client.DeliveryMethod = SmtpDeliveryMethod.Network;
           client.UseDefaultCredentials = false;
           client.Credentials = new System.Net.NetworkCredential(SMTPUsername,SMTPPassword);
           MailMessage mm = new MailMessage(MailFrom, MailTo);
           mm.From = new MailAddress(MailFrom, MailFromName);
           mm.Subject = MailSubject;
           StreamReader reader = new StreamReader(MailBodyTemplate);
           string readFile = reader.ReadToEnd();
           string StrContent = “”;
           StrContent = readFile;
           StrContent = StrContent.Replace(“<%MailBodyContent%>”, MailBodyContent);
           mm.Body = StrContent.ToString();
           mm.IsBodyHtml = true;
           switch (MailPriority)
           {
               case “Low”:
                   mm.Priority = System.Net.Mail.MailPriority.Low;
                   break;
               case “Normal”:
                   mm.Priority = System.Net.Mail.MailPriority.Normal;
                   break;
               case “High”:
                   mm.Priority = System.Net.Mail.MailPriority.High;
                   break;
               default:
                   mm.Priority = System.Net.Mail.MailPriority.Normal;
                   break;
           }
           mm.BodyEncoding = UTF8Encoding.UTF8;
           mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
           if (MailCCArray != “”) {
               string[] ccarray = MailCCArray.Split(‘;’);
               foreach (string ccarrayitem in ccarray) {
                   MailAddress cc = new MailAddress(ccarrayitem);
                   mm.CC.Add(cc);
               }
           }
           if (MailAttachmentPath != null)
           {
               Attachment attachment = new Attachment(MailAttachmentPath);
               attachment.Name = MailAttachmentName;
               mm.Attachments.Add(attachment);
           }
           client.Send(mm);
       }
   }
}
using System.Text;
using RGiesecke.DllExport;
using System.Runtime.InteropServices;
using System.Net.Mail;
using System.IO;
namespace SendAdvancedEmail
{
   class CSendAdvancedEmail
   {
       [DllExport(“SendAdvancedEmail”, CallingConvention = CallingConvention.StdCall)]
       public static void SendAdvancedEmail(
           [MarshalAs(UnmanagedType.LPWStr)] string MailFrom,
           [MarshalAs(UnmanagedType.LPWStr)] string MailFromName,
           [MarshalAs(UnmanagedType.LPWStr)] string MailTo,
           [MarshalAs(UnmanagedType.LPWStr)] string MailCCArray,
           [MarshalAs(UnmanagedType.LPWStr)] string MailSubject,
           [MarshalAs(UnmanagedType.LPWStr)] string MailBodyContent,
           [MarshalAs(UnmanagedType.LPWStr)] string MailBodyTemplate,
           [MarshalAs(UnmanagedType.LPWStr)] string MailPriority,
           [MarshalAs(UnmanagedType.LPWStr)] string MailAttachmentPath,
           [MarshalAs(UnmanagedType.LPWStr)] string MailAttachmentName,
           [MarshalAs(UnmanagedType.LPWStr)] string SMTPServer,
           int SMTPPort,
           bool SMTPEnableSSL,
           int SMTPTimeout,
           [MarshalAs(UnmanagedType.LPWStr)] string SMTPUsername,
           [MarshalAs(UnmanagedType.LPWStr)] string SMTPPassword
           )
       {
           SmtpClient client = new SmtpClient();
           client.Port = SMTPPort;
           client.Host = SMTPServer;
           client.EnableSsl = SMTPEnableSSL;
           client.Timeout = SMTPTimeout;
           client.DeliveryMethod = SmtpDeliveryMethod.Network;
           client.UseDefaultCredentials = false;
           client.Credentials = new System.Net.NetworkCredential(SMTPUsername,SMTPPassword);
           MailMessage mm = new MailMessage(MailFrom, MailTo);
           mm.From = new MailAddress(MailFrom, MailFromName);
           mm.Subject = MailSubject;
           StreamReader reader = new StreamReader(MailBodyTemplate);
           string readFile = reader.ReadToEnd();
           string StrContent = “”;
           StrContent = readFile;
           StrContent = StrContent.Replace(“<%MailBodyContent%>”, MailBodyContent);
           mm.Body = StrContent.ToString();
           mm.IsBodyHtml = true;
           switch (MailPriority)
           {
               case “Low”:
                   mm.Priority = System.Net.Mail.MailPriority.Low;
                   break;
               case “Normal”:
                   mm.Priority = System.Net.Mail.MailPriority.Normal;
                   break;
               case “High”:
                   mm.Priority = System.Net.Mail.MailPriority.High;
                   break;
               default:
                   mm.Priority = System.Net.Mail.MailPriority.Normal;
                   break;
           }
           mm.BodyEncoding = UTF8Encoding.UTF8;
           mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
           if (MailCCArray != “”) {
               string[] ccarray = MailCCArray.Split(‘;’);
               foreach (string ccarrayitem in ccarray) {
                   MailAddress cc = new MailAddress(ccarrayitem);
                   mm.CC.Add(cc);
               }
           }
           if (MailAttachmentPath != null)
           {
               Attachment attachment = new Attachment(MailAttachmentPath);
               attachment.Name = MailAttachmentName;
               mm.Attachments.Add(attachment);
           }
           client.Send(mm);
       }
   }
}
SendAdvancedEmail.mq4 – Example Indicator to test DLL for MetaTrader 4
When it runs it shoots a screenshot, calls the DLL with this screenshot attached. It will send an email using SendAdvancedEmail.dll. You get the picture ( literally 🙂 ).
//+——————————————————————+
//|Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â SendAdvancedEmail.mq4 |
//|Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Copyright 2014, Dorian Ocsovszki |
//|Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â |
//+——————————————————————+
#property copyright “Copyright 2014, Dorian Ocsovszki”
#property link     “2014/05/sendextendedemail-dll-function-for.html”
#property version  “1.00”
#property strict
#property indicator_chart_window
#import “SendAdvancedEmail.dll”
  void SendAdvancedEmail(string MailFrom, string MailFromName, string MailTo, string MailCC, string MailSubject, string MailBodyContent, string MailBodyTemplate, string MailPriority, string MailAttachmentPath, string MailAttachmentName, string SMTPServer, int SMTPPort, bool SMTPEnableSSL, int SMTPTimeout, string SMTPUsername, string SMTPPassword);
#import
input string Separator1 = NULL; //=== SCREENSHOT SETTINGS ===
input string PathToScreenshots = “C:\\DATA\\%MT4DIR%\\MQL4\\Files\\”; // Path to Screenshots ( %MT4DIR%\MQL4\Files )
input int SCWidth = 1920; // Screenshot Width
input int SCHeight = 1080; // Screenshot Height
input string Separator2 = NULL; //=== MAIL SETTINGS ===
input string MailFrom = “”;Â Â // FROM:
input string MailFromName = “MT4 Advanced eMail Notifier”; // FROM NAME:
input string MailTo = “”;Â // TO:
input string MailCC = “”;Â // CC: (Mail1;Mail2;MailN …)
input string Separator3 = NULL; //=== SMTP SETTINGS ===
input string SMTPServer = “”;
input int SMTPPort = 587;
input string SMTPUsername = “”;
input string SMTPPassword = “”;
int SMTPTimeout = 10000;
bool SMTPEnableSSL = true;
string MailSubject = “MT4 Notifier”;
string MailBody = “MT4 Notifier MailBody”;
string MailAttachmentPath = NULL;
string MailAttachmentName = NULL;
//+——————————————————————+
//| Custom indicator initialization function                        |
//+——————————————————————+
int OnInit()
 {
//— indicator buffers mapping
  string SCFileNameVer1 = StringConcatenate(Symbol(), “_Period”, Period(), “_”, TimeYear(TimeLocal()), “-“, TimeMonth(TimeLocal()), “-“, TimeDay(TimeLocal()), “_”, PadString(DoubleToStr(TimeHour(TimeLocal()),0),”0″,2), “-“, PadString(DoubleToStr(TimeMinute(TimeLocal()),0),”0”,2), “-“, PadString(DoubleToStr(TimeSeconds(TimeLocal()),0),”0”,2), “.gif” );
  if (ShootScreenShot(SCFileNameVer1)) {
     SendAdvancedEmailSimple(“[FOREX] New Order Opened”, “2014.05.18 13:38:15.738   OrderHistoryTest GBPNZD,M15: #40320001 2014.05.08 18:21:20 buy limit 2.00 GBPNZD 1.92168 0.00000 0.00000 2014.05.15 19:40:55 1.94523 0.00 0.00 0.00 cancelled 0”, “C:\\DATA\\XMMT4-GBPNZD\\MQL4\\Libraries\\MailBody.html”, “Normal”, PathToScreenshots + SCFileNameVer1, SCFileNameVer1);
  } else {
     Print(“ERROR: Screenshot failed. Email event cancelled.”);
  }
 Â
//—
  return(INIT_SUCCEEDED);
 }
//+——————————————————————+
//| Custom indicator iteration function                             |
//+——————————————————————+
int OnCalculate(const int rates_total,
               const int prev_calculated,
               const datetime &time[],
               const double &open[],
               const double &high[],
               const double &low[],
               const double &close[],
               const long &tick_volume[],
               const long &volume[],
               const int &spread[])
 {
//—
 Â
//— return value of prev_calculated for next call
  return(rates_total);
 }
//+——————————————————————+
void SendAdvancedEmailSimple(string fMailSubject, string fMailBodyContent, string fMailBodyTemplate, string fMailPriority = “Normal”, string fMailAttachmentPath = NULL, string fMailAttachmentName = NULL)
{
  SendAdvancedEmail(MailFrom, MailFromName, MailTo, MailCC, fMailSubject, fMailBodyContent, fMailBodyTemplate, fMailPriority, fMailAttachmentPath, fMailAttachmentName, SMTPServer, SMTPPort, SMTPEnableSSL, SMTPTimeout, SMTPUsername, SMTPPassword);
}
bool ShootScreenShot(string SCFileName)
{
  bool status = WindowScreenShot(SCFileName,SCWidth,SCHeight,-1,-1,-1);
  return (status);
}
string PadString(string toBePadded, string paddingChar, int paddingLength)
{
  while(StringLen(toBePadded) < paddingLength)
  {
     toBePadded = StringConcatenate(paddingChar,toBePadded);
  }
  return (toBePadded);
}
//|Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â SendAdvancedEmail.mq4 |
//|Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Copyright 2014, Dorian Ocsovszki |
//|Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â |
//+——————————————————————+
#property copyright “Copyright 2014, Dorian Ocsovszki”
#property link     “2014/05/sendextendedemail-dll-function-for.html”
#property version  “1.00”
#property strict
#property indicator_chart_window
#import “SendAdvancedEmail.dll”
  void SendAdvancedEmail(string MailFrom, string MailFromName, string MailTo, string MailCC, string MailSubject, string MailBodyContent, string MailBodyTemplate, string MailPriority, string MailAttachmentPath, string MailAttachmentName, string SMTPServer, int SMTPPort, bool SMTPEnableSSL, int SMTPTimeout, string SMTPUsername, string SMTPPassword);
#import
input string Separator1 = NULL; //=== SCREENSHOT SETTINGS ===
input string PathToScreenshots = “C:\\DATA\\%MT4DIR%\\MQL4\\Files\\”; // Path to Screenshots ( %MT4DIR%\MQL4\Files )
input int SCWidth = 1920; // Screenshot Width
input int SCHeight = 1080; // Screenshot Height
input string Separator2 = NULL; //=== MAIL SETTINGS ===
input string MailFrom = “”;Â Â // FROM:
input string MailFromName = “MT4 Advanced eMail Notifier”; // FROM NAME:
input string MailTo = “”;Â // TO:
input string MailCC = “”;Â // CC: (Mail1;Mail2;MailN …)
input string Separator3 = NULL; //=== SMTP SETTINGS ===
input string SMTPServer = “”;
input int SMTPPort = 587;
input string SMTPUsername = “”;
input string SMTPPassword = “”;
int SMTPTimeout = 10000;
bool SMTPEnableSSL = true;
string MailSubject = “MT4 Notifier”;
string MailBody = “MT4 Notifier MailBody”;
string MailAttachmentPath = NULL;
string MailAttachmentName = NULL;
//+——————————————————————+
//| Custom indicator initialization function                        |
//+——————————————————————+
int OnInit()
 {
//— indicator buffers mapping
  string SCFileNameVer1 = StringConcatenate(Symbol(), “_Period”, Period(), “_”, TimeYear(TimeLocal()), “-“, TimeMonth(TimeLocal()), “-“, TimeDay(TimeLocal()), “_”, PadString(DoubleToStr(TimeHour(TimeLocal()),0),”0″,2), “-“, PadString(DoubleToStr(TimeMinute(TimeLocal()),0),”0”,2), “-“, PadString(DoubleToStr(TimeSeconds(TimeLocal()),0),”0”,2), “.gif” );
  if (ShootScreenShot(SCFileNameVer1)) {
     SendAdvancedEmailSimple(“[FOREX] New Order Opened”, “2014.05.18 13:38:15.738   OrderHistoryTest GBPNZD,M15: #40320001 2014.05.08 18:21:20 buy limit 2.00 GBPNZD 1.92168 0.00000 0.00000 2014.05.15 19:40:55 1.94523 0.00 0.00 0.00 cancelled 0”, “C:\\DATA\\XMMT4-GBPNZD\\MQL4\\Libraries\\MailBody.html”, “Normal”, PathToScreenshots + SCFileNameVer1, SCFileNameVer1);
  } else {
     Print(“ERROR: Screenshot failed. Email event cancelled.”);
  }
 Â
//—
  return(INIT_SUCCEEDED);
 }
//+——————————————————————+
//| Custom indicator iteration function                             |
//+——————————————————————+
int OnCalculate(const int rates_total,
               const int prev_calculated,
               const datetime &time[],
               const double &open[],
               const double &high[],
               const double &low[],
               const double &close[],
               const long &tick_volume[],
               const long &volume[],
               const int &spread[])
 {
//—
 Â
//— return value of prev_calculated for next call
  return(rates_total);
 }
//+——————————————————————+
void SendAdvancedEmailSimple(string fMailSubject, string fMailBodyContent, string fMailBodyTemplate, string fMailPriority = “Normal”, string fMailAttachmentPath = NULL, string fMailAttachmentName = NULL)
{
  SendAdvancedEmail(MailFrom, MailFromName, MailTo, MailCC, fMailSubject, fMailBodyContent, fMailBodyTemplate, fMailPriority, fMailAttachmentPath, fMailAttachmentName, SMTPServer, SMTPPort, SMTPEnableSSL, SMTPTimeout, SMTPUsername, SMTPPassword);
}
bool ShootScreenShot(string SCFileName)
{
  bool status = WindowScreenShot(SCFileName,SCWidth,SCHeight,-1,-1,-1);
  return (status);
}
string PadString(string toBePadded, string paddingChar, int paddingLength)
{
  while(StringLen(toBePadded) < paddingLength)
  {
     toBePadded = StringConcatenate(paddingChar,toBePadded);
  }
  return (toBePadded);
}
Known issues
- Mail priority does not want to work. I am not sure what is wrong with it it is a minor bug.