65.9K
CodeProject is changing. Read more.
Home

Sending mail in Managed C++ using SMTP

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.89/5 (6 votes)

Apr 26, 2002

viewsIcon

180633

downloadIcon

2969

Sending mail in Managed C++ using SMTP

Introduction

I wrote this class after reading Agus Kurniawan's article "Sending Mail in C# using SMTP". So please make sure you recognize Agus's work. I just create it a class in Managed C++ to send mail.

The thing the bothers me the most, while writing MC++, is conversions and types. The new String type in Managed C++, in my opinion, does not have the flexibility that I expected.

Code listings

These is the code of the class starting with the header file:

// Header File
#pragma once

__gc class CSendEmail
{
public:
    CSendEmail(String * Server);
    bool SendEmail( String * From, 
        String * To, 
        String * Subject,
        String * Body);

private:
    String * m_sServer;

};

Note the declaration of ConvertToChar as a member of the class.

This is the cpp file:

#using <mscorlib.dll>
#using <System.dll>

using namespace System;
using namespace System::Runtime::InteropServices;
using namespace System::Net;
using System::Net::Sockets::NetworkStream;
using System::Net::Sockets::TcpClient;
using System::IO::StreamReader;

#include "sendemail.h"

CSendEmail::CSendEmail(String * Server)
{
    m_sServer = Server;
}

bool CSendEmail::SendEmail( String * From, 
                           String * To, 
                           String * Subject,
                           String * Body)
{
    NetworkStream  * pNsEmail;
    StreamReader   * RdStrm;
    String           * Data;
    unsigned char  sendbytes __gc[];


    TcpClient *pServer = new TcpClient(m_sServer,25);

    try
    {
        pNsEmail = pServer->GetStream();
        RdStrm = new StreamReader(pServer->GetStream());
    }

    catch (Exception  * e )
    {
        Console::WriteLine(e->ToString());
        Console::WriteLine("Socket Close by the server");

        pServer->Close();
        sendbytes = 0;
        return false;
    }

    Console::WriteLine(RdStrm->ReadLine());

    Data  = "HELO server \r\n";    
    sendbytes = System::Text::Encoding::ASCII->GetBytes(Data);    
    pNsEmail->Write(sendbytes, 0, sendbytes->get_Length());
    sendbytes = 0;
    Data = 0;
    Console::WriteLine(RdStrm->ReadLine());

    Data = String::Concat(S"MAIL FROM: ",
        S"<", From, S">", S"\r\n");     
    sendbytes = System::Text::Encoding::ASCII->GetBytes(Data);    
    pNsEmail->Write(sendbytes, 0, sendbytes->get_Length());
    sendbytes = 0;
    Data = 0;
    Console::WriteLine(RdStrm->ReadLine());

    Data = String::Concat(S"RCPT TO: ", 
        S"<", To, S">", S"\r\n");    
    sendbytes = System::Text::Encoding::ASCII->GetBytes(Data);
    pNsEmail->Write(sendbytes, 0, sendbytes->get_Length());
    sendbytes = 0;
    Data = 0;
    Console::WriteLine(RdStrm->ReadLine());

    Data = "DATA \r\n";
    sendbytes = System::Text::Encoding::ASCII->GetBytes(Data);
    pNsEmail->Write(sendbytes, 0, sendbytes->get_Length());
    sendbytes = 0;
    Data = 0;
    Console::WriteLine(RdStrm->ReadLine());

    Data = String::Concat(S"FROM: ", From, S"\r\n", S"SUBJECT: ", 
        Subject, S"\r\n", Body, S"\r\n.\r\n");
    sendbytes = System::Text::Encoding::ASCII->GetBytes(Data);
    pNsEmail->Write(sendbytes, 0, sendbytes->get_Length());
    sendbytes = 0;
    Data = 0;
    Console::WriteLine(RdStrm->ReadLine());

    Data = "QUIT \r\n";
    sendbytes = System::Text::Encoding::ASCII->GetBytes(Data);
    pNsEmail->Write(sendbytes, 0, sendbytes->get_Length());
    sendbytes = 0;
    Data = 0;
    Console::WriteLine(RdStrm->ReadLine());

    pNsEmail->Close();
    RdStrm->Close();
    pServer->Close();

}

Usage

// your SMTP server
CSendEmail * pServer = new CSendEmail("mail.server.com");

pEmail->SendEmail("you@yourserver.com",
                     "yourfriend@yourfriendserver.com",
                     "Hello Friend",
                     "Message body");

I do know, that there is no UI in this program. When Microsoft creates a UI for forms as C# style, I'll update this article. I don't really want to use MFC. Have fun!