SMTP Mail Library
Info
The SMTP Mail Library is a Library for MetaTrader 5 that this library is designed to send mail messages. The function set expands the possibilities of traditional sendmail.
Usage
This tool is typically used for enhancing chart analysis and decision making.
Platform
This Library works exclusively on MetaTrader 5 (both build 600+ and newer versions).
Setup
Place the downloaded file in MQL5/Libraries folder via File ? Open Data Folder in MetaTrader 5.
How to Install and Use SMTP Mail Library
1. Storage: Place library files in the MQL/Libraries directory to ensure they are accessible to your projects.
2. Implementation: Include the library in your code using the #import directive, ensuring you match the exact function names and parameters.
3. Compilation: Ensure the library is present in the directory before you compile your main EA or script, as the compiler links them during this phase.
4. Management: Keep libraries organized in sub-folders if you manage many custom functions to maintain a clean project structure.
Frequently Asked Questions
Q: What is a library file used for? A: Libraries store reusable code modules, allowing you to centralize common logic used by multiple EAs or indicators.
Q: Is a library executable? A: No, libraries are non-executable files containing functions; they must be imported into an EA, indicator, or script to function.
Q: Can I update a library while the platform is running? A: You should compile your EA or script after updating a library to ensure the latest code changes are integrated.
What this tool does
This library is designed to send mail messages.
Typical Use Case
This Library excels in automated trading and technical analysis on MetaTrader 5.
Compatible Platform & Setup
This Library works on MetaTrader 5. Place the file in the MQL5/Libraries folder and restart the terminal.
Description & Settings
Related: timer - another powerful library for MetaTrader 5 traders.
This library is designed to send mail messages. The function set expands the possibilities of traditional sendmail. Now you can send messages in both text and HTML formats. Messages can be sent to multiple recipients, and you can attach one or more files to your email. SSL is supported.Also recommended: symbol trade made simple with functions - similar library with strong performance on MetaTrader 5.
The DLL source code is written in Delphi XE4 using the INDY library. Besides the project files, you should install the OpenSSL library.
List of library functions:
- Function
- Description
- mailconnect
- Connects with the mail server
- mailsendtext
- Sends text
- mailsendhtml
- Sends HTML messages
- mailsendinlinetextfile
- Sends messages with text file content inserted
- mailsendinlinehtmlfile
- Sends messages with HTML file content inserted
- mailerrordescription
- Returns error description
- mailclose
- Closes connection with the mail server
Error messages:
- Constant
- Value
- Description
- mail_no_connection
- -2
- No connection
- mail_no_initialization
- -1
- No object for SMTP server
- mail_no_error
- 0
- No error
- mail_timeout
- 1
- Connection closed due to timeout
- mail_wrong_login
- 2
- Wrong login/password
- mail_wrong_host_name
- 3
- Wrong host name
- mail_host_not_found
- 60
- Host not found
Script example:
//+------------------------------------------------------------------+
//| mailsend.mq5
//+------------------------------------------------------------------+
#property copyright ""
#property link ""
#property version "1.00"
#property script_show_inputs
#include
#include
#include
//+------------------------------------------------------------------+
//| enum_message
//+------------------------------------------------------------------+
enum enum_message
{
message_text, // text
message_html, // html
message_inline_text, // inline text
message_inline_html // inline html
};
//+------------------------------------------------------------------+
//| input parameters
//+------------------------------------------------------------------+
input string mail_options="=== mail options ==="; // mail options
input string inpmailhost="smtp.gmail.com"; // host
input int inpmailport=465; // port
input string inpmailuser="[email protected]"; // user
input string inpmailpassword="password"; // password
input string inpmailfrom=""; // from (text)
input string inpmailsubject="SMTP Mail Library"; // subject (text)
input string inpmailto="[email protected]"; // mail to
input uint inpmailconnectiontimeout=5000; // connection timeout, msec
input string msg_options="=== message options ==="; // message options
input enum_message inpmessagetype=message_html; // type
input string inpmessageattachmentfiles="d:\temp\dollar.bmp;d:\temp\euro.bmp"; // attachment files
input string inpmessageinlinefiles="d:\temp\reporttester-20266082.html"; // inline files
//+------------------------------------------------------------------+
//| script program start function
//+------------------------------------------------------------------+
void onstart()
{
long smtp=0; //initialize
//--- connection
int err=mailconnect(smtp,inpmailhost,inpmailport,inpmailuser,inpmailpassword,inpmailconnectiontimeout);
if(err!=0)
{
print("mailconnect error: ",mailerrordescription(err));
return;
}
switch(inpmessagetype)
{
//---
case message_text:
{
//--- plain text
string text=stringformat("account: %d\r\nbalance: %.2f %s",accountinfointeger(account_login),accountinfodouble(account_balance),accountinfostring(account_currency));
//--- send mail to yourself
err=mailsendtext(smtp,inpmailuser,inpmailfrom,inpmailsubject,text,inpmessageattachmentfiles);
if(err!=0)
print("mailsendtext error: ",mailerrordescription(err));
else
printformat("program '%s' has sent mail to '%s'",mqlinfostring(mql_program_name),inpmailuser);
//--- send mail to mailto
err=mailsendtext(smtp,inpmailto,inpmailfrom,inpmailsubject,text,inpmessageattachmentfiles);
if(err!=0)
print("mailsendtext error: ",mailerrordescription(err));
else
printformat("program '%s' has sent mail to '%s'",mqlinfostring(mql_program_name),inpmailto);
}
break;
//---
case message_html:
{
//--- build html
string html=buildreport();
//--- send html
err=mailsendhtml(smtp,inpmailto,inpmailfrom,inpmailsubject,html,"" );
if(err!=0)
print("mailsendtext error: ",mailerrordescription(err));
else
printformat("program '%s' has sent mail to '%s'",mqlinfostring(mql_program_name),inpmailto);
}
break;
//---
case message_inline_text:
{
err=mailsendinlinetextfile(smtp,inpmailto,inpmailfrom,inpmailsubject,inpmessageinlinefiles);
if(err!=0)
print("mailsendtext error: ",mailerrordescription(err));
else
printformat("program '%s' has sent mail to '%s'",mqlinfostring(mql_program_name),inpmailto);
}
break;
//---
case message_inline_html:
{
err=mailsendinlinehtmlfile(smtp,inpmailto,inpmailfrom,inpmailsubject,inpmessageinlinefiles);
if(err!=0)
print("mailsendtext error: ",mailerrordescription(err));
else
printformat("program '%s' has sent mail to '%s'",mqlinfostring(mql_program_name),inpmailto);
}
break;
}
//--- close connection
mailclose(smtp);
}
//+------------------------------------------------------------------+
//| buildreport
//+------------------------------------------------------------------+
string buildreport()
{
carraystring html;
//---
html.add("
html.add("");
html.add("");
html.add("");
html.add("");
html.add("");
html.add("
html.add(stringformat("
html.add("
| a/c no: %d | ",accountinfointeger(account_login)));name: %s | ",accountinfostring(account_name))); | ");%s | ",timetostring(timecurrent())));||||||||||
| open trades: | ");|||||||||||||
| ticket | ");open time | ");type | ");size | ");symbol | ");price | ");s/l | ");t/p | ");price | ");commission | ");swap | ");profit | ");||
| no transactions | |||||||||||||
| %d | ",m_position.identifier())); %s | ",timetostring(m_position.time())));buy | ");sell | ");%.2f | ",m_position.volume()));%s | ",m_position.symbol()));%.5f | ",m_position.priceopen()));%.5f | ",m_position.stoploss()));%.5f | ",m_position.takeprofit()));%.5f | ",m_position.pricecurrent()));%.2f | ",m_position.commission()));%.2f | ",m_position.swap()));%.2f | ",m_position.profit()));|
| balance: %.2f | ",accountinfodouble(account_balance)));equity: %.2f | ",accountinfodouble(account_equity)));floating p/l: | ");%.2f | ",profit));||||||||||
//--- save to single string
string result="";
total=html.total();
for(int i=0;i
//--- done
return(result);
}
//+------------------------------------------------------------------+
results:
You may also like: Measure Trends Function - excellent alternative for library users on MetaTrader 5.
Source Code
#property copyright "robotfx"
#property link "https://robotfx.org"
#property version "1.00"
#property script_show_inputs
#include <trade\trade.mqh>
#include <arrays\arraystring.mqh>
#include <smtpmaillibrary.mqh>
enum enum_message
{
message_text, // text
.......
⚠ Limitations & Risk Warning
- This tool is provided for educational and testing purposes only.
- Past performance does not guarantee future results.
- Trading involves substantial risk of loss. Use on a demo account first.
- Results may vary depending on market conditions, broker, and settings.
- We recommend thorough backtesting and forward testing before using with real funds.