Download MetaTrader 5

Watch how to download trading robots for free

Interesting script?
So post a link to it -
let others appraise it

You liked the script? Try it in the MetaTrader 5 terminal

2014.08.01 15:02
Libraries

SendAdvancedEmail - library for MetaTrader 4

| English Español Português 日本語 Deutsch

Published by:
Gorkhaan
Views:
6895
Rating:
votes: 13

Real author:

Dorian Ocsovszki - http://ocsovszki-dorian.blogspot.co.uk/

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

Indicator settings

Notification Email

Notification Email


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);
        }

    }
}

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      "http://ocsovszki-dorian.blogspot.co.uk/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.
BBMACD BBMACD

BBMACD is the Bollinger Bands® and MACD indicator in the same place at separate window.

Genie Stochastic RSI Genie Stochastic RSI

Stochastic RSI Expert Advisor.

BBMACD_V2 BBMACD_V2

BBMACD_v2 is the Bollinger Bands® and MACD indicator in the same place at separate window version 2.

Analyze History Analyze History

EA to find gaps in history data.