ivan
  • ivan
  • 100% (Exalted)
  • Administration Topic Starter
14 years ago
Managed C++/CLI Example

// The following example codes demonstrate sending email message using email queue + databasae
// To get full sample projects, please download and install EASendMail on your machine.
// To run it correctly, please change SMTP server, user, password, sender, recipient value to yours


#include "stdafx.h" 

using namespace System; 
using namespace EASendMail; 

int main(array<System::String ^> ^args) 
{ 
    SmtpMail ^oMail = gcnew SmtpMail("TryIt"); 
    SmtpClient ^oSmtp = gcnew SmtpClient(); 

    // Set sender email address, please change it to yours
    oMail->From = "test@emailarchitect.net"; 

    // Set email subject
    oMail->Subject = "test email from Managed C++ project"; 

    // Your SMTP server address
    SmtpServer ^oServer = gcnew SmtpServer("smtp.emailarchitect.net"); 

    // User and password for ESMTP authentication, if your server doesn't require
    // User authentication, please remove the following codes.
    oServer->User = "test@emailarchitect.net"; 
    oServer->Password = "testpassword"; 

    // If your smtp server requires SSL connection, please add this line
    // oServer->ConnectType = SmtpConnectType::ConnectSSLAuto;

    // If you want EASendMail service to send the email after 10 minutes,
    // use the following code.
    // oMail->Date = System::DateTime::Now.AddMinutes( 10 );

    // EASendMail will use the following connection to connect to the database,
    // the syntax is same as ADO connection object.
    oMail->Headers->ReplaceHeader("X-Data-Connection", 
        "Driver={Microsoft Access Driver (*.mdb)}; " + 
        "Dbq=c:\\easendmail\\easendmail_demo.mdb;Uid=;Pwd=;"); 

    // EASendMail will select the fields by the following sql statement
    // before sending email,
    // then pick the recipient address from specified field.
    oMail->Headers->ReplaceHeader("X-Sql-Select", "SELECT id, name, address FROM Recipients"); 

    // pick "name" field as the recipient name and "address" field as
    // the recipient address.
    // you can also use {$var_srecord:fieldname} to pick any field
    // in X-Sql-Select statement and put it to subject, bodytext, then EASendMail will
    // replace it automatially.

    oMail->Headers->ReplaceHeader("To", "\"{$var_srecord:name}\" <{$var_srecord:address}>"); 
    oMail->Headers->ReplaceHeader("X-Rcpt-To", "{$var_srecord:address}"); 

    // EASendMail service will execute the following sql statement on
    // every email was sent successfully.
    oMail->Headers->ReplaceHeader("X-Sql-OnSentSuccess", 
        "INSERT INTO sentlog ( server, email ) VALUES( '{$var_server}', '{$var_rcptaddr}' )"); 

    // EASendMail service will execute the following sql statement on
    // every email could not be sent.
    oMail->Headers->ReplaceHeader("X-Sql-OnSentError", 
        "INSERT INTO errorlog( email, server, errorcode, errordescription )" + 
        " VALUES( '{$var_rcptaddr}', '{$var_server}', '{$var_errcode}', '{$var_errdesc}' )"); 

    String ^s = "Hi {$var_srecord:name}, \r\n"; 
    s += "this sample demonstrates how to send email using email queue.\r\n\r\n"; 
    // {$var_srecord:address} will be replaced by EASendMail automatically.
    s += "Your id in database is {$var_srecord:id}.\r\n\r\n"; 
    s += "No matter how many recipients there are, EASendMail "; 
    s += "service will send the email in background.\r\n\r\n"; 

    // {$var_srecord:id} {$var_srecord:address} {$var_srecord:name} in
    // body text will
    // be replaced by EASendMail automatically.
    oMail->TextBody = s; 
    try 
    { 
        Console::WriteLine("start to send email from Managed C++..."); 
        oSmtp->SendMailToQueue(oServer, oMail); 
        Console::WriteLine("email was sent to queue successfully!"); 
    } 
    catch (Exception ^ep) 
    { 
        Console::WriteLine("failed to send email with the following error:"); 
        Console::WriteLine(ep->Message); 
    } 

    return 0; 
} 
Click here to read original topic - full version ... 


If you have any comments or questions about above example codes, please add your comments here.

EXPLORE TUTORIALS

© All Rights Reserved, AIFEI Software Limited & AdminSystem Software Limited.