Click here to Skip to main content
11,629,614 members (81,627 online)
Click here to Skip to main content

CSmtpProxyMT 1.0

, 15 Nov 2001 CPOL 112.9K 1.5K 37
Rate this:
Please Sign up or sign in to vote.
An SMTP proxy class with ability to insert signatures to outgoing mails. Does not require MFC.

Overview

CSmtpProxyMT is a C++ class that provides developers with a simple way to implement a multi-threaded SMTP proxy server in their applications. You can use it in MFC projects as well as in non-MFC C++ projects. I'd like to thank my good friend Colin Davies for his idea of inserting signatures into mails at the SMTP level instead of at the mail client level.

Features

  • Simple C++ interface
  • The code does not use the MFC Socket classes. Thus it avoids several pitfalls associated with the MFC Socket classes
  • You can use it in MFC/non-MFC applications as well as in console applications
  • Multithreaded [uses the CRT function _beginthreadex instead of the API functions]
  • Ability to insert a signature to all outgoing mails [html mails, text mails, mails with attachments]
  • You can create CSmtpProxyMT object only on the heap. If you attempt to create the CSmtpProxyMT object on the stack, you'll get unpredictable results.

Usage

  1. Add SmtpProxyMT.cpp and SmtpProxyMT.h to your project

  2. Put #include "SmtpProxyMT.h" on top of your source file, from where you wish to instantiate the class

  3. Add WS2_32.lib to the list of Library/object modules

  4. Add a member variable to your class or if you are not using a class, simply declare a member variable as follows :- 

    CSmtpProxyMT *m_smtpproxy;

  5. Now create the object either in your constructor or somewhere similarly suitable :-

    m_smtpproxy = new CSmtpProxyMT;

  6. You can also set the signature to be attached to all outgoing mails

     m_smtpproxy->SetSignature("Hello World from Nish");

  7. Starting the proxy is easy :-

    m_smtpproxy->StartProxy("192.168.1.44",25,125);

  8. You can stop the proxy just as easily :-

    m_smtpproxy->StopProxy();

Function Reference

I'll list below the public member functions that you can use

CSmtpProxyMT::CSmtpProxyMT

CSmtpProxyMT();

Remarks :- Constructs a CSmtpProxyMT object. Please remember that you should only create the CSmtpProxyMT object on the heap.

CSmtpProxyMT::StartProxy

int StartProxy(char *server, int port, int localport);

Return Value :- This will return ERR_RUNNING if the proxy is already started. Else it will return OK_SUCCESS

Parameters :-

server - You specify the SMTP server here. You can use a domain name such as smtp.nish.com or you can use an IP address such as 202.54.6.60

port - This is where you specify the SMTP port, usually 25

localport - This is the SMTP proxy port. You can specify 25 here too, unless you are running the proxy on the same IP as the SMTP server. In that case use something else.

Remarks :- This will start the SMTP proxy service. Currently if there is a host resolve problem you will not get notified. This is a TODO for me.

CSmtpProxyMT::StopProxy

int StopProxy();

Return Value :- This returns ERR_STOPPED if the proxy is not running. Otherwise it returns OK_SUCCESS

Remarks :- This stops the SMTP proxy service.

CSmtpProxyMT::SetSignature

BOOL SetSignature(char *sig);

Return Value :- This will return true if the signature was successfully set. It will return false if the signature is too large.

Parameters :-

sig - This is used to specify the signature. It should be a null-terminated string.

Remarks :- Currently there is a 2 KB limit which can be easily modified by editing the source. This version supports only text signatures. It's a TODO for me to add HTML signature options in the next version

Acknowledgements

  • Colin Davies - For the signature idea

Some remarks

I am sure purist C++ programmers will frown on seeing my source code. I have not followed strictly  proper coding standards nor have I tried to stick to perfect OOP concepts. There might be people who say, that this is not the way to write code. Well, that's the way I write code. Perhaps I need to change, but it's not going to be easy

I have tested the class using Outlook Express 6.0. I have tested it with several combinations such as text only mails, html mails, mails with single/multiple attachments. So far I haven't had any problems. But I presume that there might be problems with some mail clients that use their own methods in sending mails.

You'll also notice that I haven't commented the code much. But the code is self-explanatory. I don't usually comment my code and even when I do, the comments I put are obscure and often serve only to confuse my colleagues. Therefore I try to avoid comments. But there are comments where required. And if anyone requires any clarification, I'll be glad to oblige him or her.

Thanks

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Nish Nishant
United States United States
Nish Nishant is a Software Architect/Consultant based out of Columbus, Ohio. He has over 15 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish is a recipient of the annual Microsoft Visual C++ MVP Award since 2002 (13 consecutive awards as of 2014).

Nish is an industry acknowledged expert in the Microsoft technology stack. He authored
C++/CLI in Action for Manning Publications in 2005, and had previously co-authored
Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his
WordPress blog. Nish is vastly experienced in team management, mentoring teams, and directing all stages of software development.

Contact Nish : You can reach Nish on his google email id voidnish.

Website and Blog

You may also be interested in...

Comments and Discussions

 
QuestionQuestion Pin
bijulsoni30-Apr-07 21:12
memberbijulsoni30-Apr-07 21:12 
QuestionMaybe you know how to catch outgoing email? Pin
dirand8514-Sep-03 7:19
memberdirand8514-Sep-03 7:19 
QuestionIs this board still active? Pin
tommy1234515-Apr-03 10:49
membertommy1234515-Apr-03 10:49 
AnswerRe: Is this board still active? Pin
Nishant S15-Apr-03 15:40
editorNishant S15-Apr-03 15:40 
GeneralRe: Is this board still active? Pin
tommy1234515-Apr-03 23:02
membertommy1234515-Apr-03 23:02 
GeneralRe: Is this board still active? Pin
Nishant S19-Apr-03 19:53
editorNishant S19-Apr-03 19:53 
GeneralRe: Is this board still active? Pin
tommy1234520-Apr-03 2:18
membertommy1234520-Apr-03 2:18 
GeneralBypass Firewall Restriction Pin
Wally_wu7-Feb-03 22:11
memberWally_wu7-Feb-03 22:11 
If my Proxy Server is only HTTP(80 PORT) and HTTPS(8080 PORT) is available, but I need to send mail to external mail server on my program. Can you give me the solution?
If use the proxy's command CONNECT, how to use it?
can you give me a sample about this?

Wally
GeneralInfo about WS2_32.lib Pin
Pierre Couderc17-Feb-02 23:37
memberPierre Couderc17-Feb-02 23:37 
GeneralRe: Info about WS2_32.lib Pin
Nish [BusterBoy]18-Feb-02 0:27
memberNish [BusterBoy]18-Feb-02 0:27 
QuestionHow did you set up your test? Pin
Doug Brower5-Feb-02 2:14
memberDoug Brower5-Feb-02 2:14 
AnswerRe: How did you set up your test? Pin
Nish [BusterBoy]5-Feb-02 2:27
memberNish [BusterBoy]5-Feb-02 2:27 
GeneralA few questions Pin
Tim Lesher4-Dec-01 8:06
memberTim Lesher4-Dec-01 8:06 
GeneralRe: A few questions Pin
Nish [BusterBoy]4-Dec-01 18:20
memberNish [BusterBoy]4-Dec-01 18:20 
GeneralRe: A few questions Pin
Tim Lesher5-Dec-01 3:27
memberTim Lesher5-Dec-01 3:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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
Web01 | 2.8.150723.1 | Last Updated 16 Nov 2001
Article Copyright 2001 by Nish Nishant
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid