Click here to Skip to main content
12,635,139 members (24,774 online)
Click here to Skip to main content
Add your own
alternative version

Stats

157.3K views
2.6K downloads
24 bookmarked
Posted

Sending mail in Managed C++ using SMTP

, 25 Apr 2002
Rate this:
Please Sign up or sign in to vote.
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:

<pre lang=mc++>// 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:

<pre lang=mc++>#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

<pre lang=mc++>// 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!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Albert Pascual
Web Developer
United States United States
Al is just another Software Engineer working in C++, ASp.NET and C#. Enjoys snowboarding in Big Bear, and wait patiently for his daughters to be old enough to write code and snowboard.

Al is a Microsoft ASP.NET MVP

Blog

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionIs there any way to set sender-IP manually in smtp? Pin
Member 18940854-Apr-11 3:17
memberMember 18940854-Apr-11 3:17 
GeneralTwo comments Pin
Louis Ma6-May-10 9:43
memberLouis Ma6-May-10 9:43 
Generalhelp in writting code Pin
Tusajigwe Isaga12-Sep-07 3:50
memberTusajigwe Isaga12-Sep-07 3:50 
Generalfatal error C1190: managed targeted code requires '#using &lt;mscorlib.dll&gt;' and '/clr' option Pin
GNN_Ricardo27-Jun-05 11:32
memberGNN_Ricardo27-Jun-05 11:32 
GeneralRe: fatal error C1190: managed targeted code requires '#using &lt;mscorlib.dll&gt;' and '/clr' option Pin
Muralish19-Apr-07 2:51
memberMuralish19-Apr-07 2:51 
GeneralRe: fatal error C1190: managed targeted code requires '#using &lt;mscorlib.dll&gt;' and '/clr' option Pin
Member 43652091-Nov-09 21:39
memberMember 43652091-Nov-09 21:39 
GeneralThe New String Type Pin
Heath Stewart1-Jan-05 7:43
protectorHeath Stewart1-Jan-05 7:43 
QuestionWhy not use MailMessage? Pin
MChimley13-May-04 1:45
memberMChimley13-May-04 1:45 
AnswerRe: Why not use MailMessage? Pin
Heath Stewart1-Jan-05 7:36
protectorHeath Stewart1-Jan-05 7:36 
GeneralProblems using header Pin
ericminh13-Dec-03 17:23
memberericminh13-Dec-03 17:23 
QuestionWhere can I get the DLLs to use this? Pin
redsodium10-Aug-03 3:19
memberredsodium10-Aug-03 3:19 
GeneralAnother way to get the bytes Pin
James T. Johnson26-Apr-02 10:24
memberJames T. Johnson26-Apr-02 10:24 
GeneralRe: Another way to get the bytes Pin
Albert Pascual26-Apr-02 10:28
memberAlbert Pascual26-Apr-02 10:28 
GeneralRe: Another way to get the bytes Pin
James T. Johnson26-Apr-02 10:32
memberJames T. Johnson26-Apr-02 10:32 
GeneralRe: Another way to get the bytes Pin
Albert Pascual26-Apr-02 10:34
memberAlbert Pascual26-Apr-02 10:34 
GeneralRe: Another way to get the bytes Pin
James T. Johnson26-Apr-02 10:37
memberJames T. Johnson26-Apr-02 10:37 
GeneralRe: Another way to get the bytes Pin
James T. Johnson26-Apr-02 10:36
memberJames T. Johnson26-Apr-02 10:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161208.2 | Last Updated 26 Apr 2002
Article Copyright 2002 by Albert Pascual
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid