SMTP Mail Library - Library for MetaTrader 5 | MT5 EA Download - MetaTrader 5 Resources



This library is designed to send mail messages.
The feature set extends the traditional possibilities for sending emails . Now you can send messages in text and HTML formats. Messages can be sent to multiple recipients. You can attach one or more files to your message. SSL is supported.
The DLL source code was written on Delphi XE4 using the Indy library.
In addition to the project files, you should also install the Open SSL library.
Library function list:
error message:
Script example:
//+------------------------------------------------------------------+ //| Email sending.mq5 | //| Avoitenko| //| https://www.mql5.com/en/users/avoitenko | //+------------------------------------------------------------------+ # Property Copyright "Avoitenko" #Property association "https://www.mql5.com/en/users/avoitenko" # Property version "1.00" #Property script display input #include <Trade\Trade.mqh> #include <Array\ArrayString.mqh> #include//+------------------------------------------------------------------+ //| ENUM_MESSAGE | //+------------------------------------------------------------------+ Enumeration ENUM_MESSAGE { Message text, // text MESSAGE_HTML, // html MESSAGE_INLINE_TEXT, // inline text MESSAGE_INLINE_HTML // inline html }; //+---------------------------------------------------------------------------------+ //| Input parameters | //+------------------------------------------------------------------+ input string mailoptions = "===mailoptions===" ; // mailoptions Input string InpMailHost= "smtp.gmail.com" ; // Host Input integer InpMailPort= Chapter 465 ; // Port input string InpMail user = "user@gmail.com" ; // user input string inpmailpassword = "password" ; // password input string InpMailFrom= ”” ; // from (text) Input string InpMail subject = "SMTP mail library" ; // subject (text) Input string InpMailTo= "user@ukr.net" ; // Mail to Input unit InpMailConnectionTimeout= 5000 ; // Connection timeout, milliseconds input string message 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-20066082.html” ; // Inline files //+------------------------------------------------------------------+ //|Script program startup function | //+------------------------------------------------------------------+ blanknctions">on startup() { long mailsend = 0 ; // initialization // - - contact integer err = MailConnect(smtp,InpMailHost,InpMailPort,InpMailUser,InpMailPassword,InpMailConnectionTimeout); if (Error! = 0 ) { print ( "Mail Connection Error:" ,MailErrorDescription(err)); return ; } Transform (Inp Message Type) { //--- Case MESSAGE_TEXT: { //--- Plain Text String Text = String Format ( "Account: %d\r\nBalance: %.2f %s" , AccountInfo Integer ( Account Login ), AccountInfo Double ( Account Balance ), AccountInfo String ( ACCOUNT_CURRENCY )); //--- Send an email to yourself err=MailSendText(smtp,InpMailUser,InpMailFrom,InpMailSubject,text,InpMessageAttachmentFiles); if (error! = 0 ) print ( "Mail sending text error:" , MailErrorDescription(err)); other printing formats ( "Program '%s' has sent mail to '%s'" , MQL information string ( MQL_PROGRAM_NAME ),InpMailUser); //--- Send mail to MailTo err=MailSendText(smtp,InpMailTo,InpMailFrom,InpMailSubject,text,InpMessageAttachmentFiles); if (Error! = 0 ) print ( "Mail sending text error:" ,MailErrorDescription(err)); other printing formats ( "Program '%s' has sent a mail to '%s'" , MQL message string ( MQL_PROGRAM_NAME ),InpMailTo); } rest ; //--- Case MESSAGE_HTML: { //--- Build html string html=BuildReport(); //--- Send html err=MailSendHtml(smtp,InpMailTo,InpMailFrom,InpMailSubject,html, ”” ); if (error! = 0 ) print ( "Mail sending text error:" , MailErrorDescription(err)); other printing formats ( "Program '%s' has sent mail to '%s'" , MQL information string ( MQL_PROGRAM_NAME ),InpMailTo); } rest ; //--- Case MESSAGE_INLINE_TEXT: { err=MailSendInlineTextFile(smtp,InpMailTo,InpMailFrom,InpMailSubject,InpMessageInlineFiles); if (Error! = 0 ) print ("Mail sending text error:",MailErrorDescription(err)); other print formats ( "Program '%s' has sent a mail to '%s'", MQL message string (MQL_PROGRAM_NAME),InpMailTo); } rest; //---case MESSAGE_INLINE_HTML: { err=MailSendInlineHtmlFile(smtp,InpMailTo,InpMailFrom,InpMailSubject,InpMessageInlineFiles); if (Error!= 0) Print ("Error in sending email text : " , MailErrorDescription ( err ) ) ; Other print formats ( "Program '%s' has sent mail to '%s'" , MQL information string ( MQL_PROGRAM_NAME ),InpMailTo); }rest ; } //--- close connection mail close (smtp); } //+------------------------------------------------------------------+ //| BuildReport | //+------------------------------------------------------------------+ String Build Report () { CArrayString html; //--- html.Add( " report " ); html.Add( " ”); html.Add to("”); html.Add to(“”); html.Add to(" "); html.Add to(“”); html.Add to(String format("%s" , Account information string ( account server ))); html. Add ( " <table cell spacing = 1 cell padding = 3 border = 0 > " ); //--- html. Add ( "" ); html. Add ( String format ( "Account number: %d " , Account information integer ( Account login ))); html. Add ( String format ( "Name: %s " , Account information string ( account name ))); html.add( "" ); html.Add( String format ( " %s " , String time ( time current ()))); html.Add( " " ); //--- html.Add( "" ); html.Add( "Open Transactions: " ); html.Add( " " ); //--- html.Add( "" ); html.add( "ticket " ); html.add( "opening hours " ); html.add( "type " ); html.add( "size " ); html.add("symbol"); html.add( "price ") ; html.add( "S/L " ); html.add( "T/P "); html.add("price "); html.add( "Commission") ; html.Add( "Exchange " ); html.Add( "Profit " ); html.Add( "" ); //--- double profit = 0.0 ; integer total = total number of positions (); if (total == 0 ) { html.Add( "); } else { CPositionInfo m_position; for ( integer i = 0 ; i < total; i++) { m_position.SelectByIndex(i); // - - color if ((i & 1 ) == 0 ) html.add( " No transaction "" ); other html.Add( "" ); // - - data html.Add( String format ( "%d " ,m_position.Identifier())); html.Add( String format ( "%s " , String time (m_position.Time()))); if (m_position.PositionType()== POSITION_TYPE_BUY ) html.Add( "Buy " ); other html.add( "for sale " ); html.add( string format ( "%.2f " ,m_position.Volume())); html.add( string format ( "%s " , m_position.Symbol ())); html.add( string format ( "%.5f " ,m_position.PriceOpen())); html.add( string format ( "%.5f " ,m_position.StopLoss())); html.add( string format ( "%.5f " ,m_position.TakeProfit())); html.add( string format ( "%.5f " ,m_position.PriceCurrent())); html.add( string format ( "%.2f " ,m_position.Commission())); html.add( string format ( " "%.2f " ,m_position.Swap())); html.Add( String format ( "%.2f " ,m_position.Profit())); html.Add( " " ); //--- Profit+=m_position.Profit(); } } //--- html.Add( "" ); html.Add( String format ( "Balance: %.2f " , account information double ( account balance ))); html.add ( string format ( "Net value: %.2f " , account information double ( account assets ))); html.add ( "floating profit and loss: " ); html.add ( string format ( "%.2f " , profit)); html.add ( " " ); //--- html.add ( " " ); //--- save to single string string result = "" ; total = html.Total(); for ( integer i = 0 ; i < total; i++) result += html.At(i); // - - complete return (result); } //+--------------------------------------------------------------------------------+
Attachment download
📎 smtpmaillibrary.mqh (5.89 KB)
📎 mailsend.mq5 (9.18 KB)
Source: MQL5 #11466
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •