Work With E-Mail Easily Using the Mailman Class



Click here for a larger image.

Environment: VC6 SP4, NT4 SP3, winCE 2.0

Introduction

MailMan is a simple C++ class that makes the task of reading and sending e-mails easy. This class uses the Messaging Application Programming Interface (MAPI) to accomplish its functionalities.

I have seen code listings regarding sending e-mails or reading e-mails in different bits and pieces on this site and on codeproject, but I did not find them as a plug-and-play kind of code. Then I thought of creating a class to encapsulate the entire functionality in one piece.

Requirement

For sending e-mails, you do not need to have Microsoft Outlook (or any other e-mail client) open, but for receiving it you do. (Some have told me you don't, but experience has shown me otherwise...).

If you happen to be one who disagrees, please e-mail me.

Methods

This class has the following members:

Method

Description

bool LogonMail() ; This is the first thing you should do after instantiating the object.
Int ReadMail(); This should tell you if there was any UNREAD E-MAIL found in the INBOX folder.
If any e-mail was found, the content can be accessed from the mailbox structure of this class.
See the code attached
bool SendMail(
CString name,
CString address,
CString Subject,
CString Text);
This allows you to send e-mails and not only to one but to as many as you would like if the addresses are delimited by semicolons.
void LogoffMail(); This is called before you terminate your application.

Example

First of all, you should have a pointer to CMailMan:

CMailMan *mail=;

This can be defined in the header file of your class.

You then need to allocate it to an instance of this class:

mail = new CMailMan();

// Initialise it

if(mail->LogonMail() = false)
{

// Error

}

How to read e-mail:

switch (mail->ReadMail())
{
  case GOTMAIL: 
  // If you have any UNREAD e-mail in your INBOX, it will be fetched here
    x.Format(_T("Name:%s Date:%s Address:%s notetext:%s Subject:%s"),
      mail->mailbox.name,
      mail->mailbox.date,
      mail->mailbox.address,
      mail->mailbox.notetext,
      mail->mailbox.subject);
    break;

  case FAILMAIL:
  // "Reading Failed – Boy, you got a problem";
    break;
  
  case NOMAIL:
  //"No mail... – No worries!";
    break;

}

How to send e-mail:

mail->SendMail("Saeed","GWBush@usa.com;Osma@Kabul.com", 
  "Stop fighting. Make Love, not war.");

How to log off from your e-mail:

//Log off to avoid memory leakage

if(mail)
{
mail->LogoffMail();
delete mail;
}

Piece of cake? You bet.

Demo:

Follow the steps as numbered.

  1. Enter your e-mail address in the edit control. This is the account that will be used for testing.
  2. Click the send button a few times (say 2).
  3. Click the Send/Receive message button of Microsoft Outlook instead of waiting for Outlook to do its checking every few minutes. DO NOT OPEN YOUR E-MAILS in Outlook; the messages will be marked as READ and that is not what we want for this test.
    After you have fetched the e-mail messages sent by this application to your INBOX, you can proceed.
  4. Click the Read button – At every click, one UNREAD e-mail is read and displayed. Use the scroll slider to view.

Although I have developed this code on Windows 2000 only, I don't anticipate any problems to pop up on the NT or XP platforms. However, if you run into any problems, please contact me in a civilized manner with a clear explanation of what the problem is.

Share what you know and ask what you don't!

// MailMan.h: interface for the CMailMan class.
//
/////////////////////////////////////////////////////////////////


//need to include mapi headers files mapix,mapiutil,
//mapitags,mapi (.h)

#if !defined(AFX_MAILMAN_H__99148C57_15FD_4D06_9D9E_5A66AD50E95A__INCLUDED_)
#define AFX_MAILMAN_H__99148C57_15FD_4D06_9D9E_5A66AD50E95A__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

enum status { OK , CANT_LOGON , CANT_LOAD};
enum readstatus { GOTMAIL,NOMAIL ,FAILMAIL};



struct MailBox
{
  CString date;
  CString name;
  CString address;
  CString subject;
  CString notetext;
  int flag;
};


class CMailMan  
{
public:
  CMailMan();
  virtual ~CMailMan();

  int ReadMail();
  int LogonMail() ;
  void LogoffMail(); 
  bool SendMail( CString name, 
                 CString address,
                 CString Subject,
                 CString Text);
  MailBox      mailbox;

protected:
  HINSTANCE GetHandle();
  HINSTANCE hMAPILib;
public:

  LHANDLE           lhSession;
  LPMAPILOGON       lpfnMAPILogon;
  LPMAPIFINDNEXT    lpfnMAPIFindNext;
  LPMAPIREADMAIL    lpfnMAPIReadMail;
  LPMAPILOGOFF      lpfnMAPILogoff;
  LPMAPIDETAILS     lpfnMAPIDetails;
  LPMAPIFREEBUFFER  lpfnMAPIFreeBuffer;
  LPMAPISENDMAIL    lpfnMAPISendMail;

};
#endif 
// !defined(AFX_MAILMAN_H__99148C57_15FD_4D06_9D9E_5A66AD50E95A__INCLUDED_)



Downloads

Comments

  • GHD Glattejern virkelig optager en stor markedsandel i hÃ¥rpleje industri

    Posted by pletcherycn on 06/15/2013 02:28pm

    [url=http://ghdfladjerntilbud.webstarts.com/]GHD fladjern tilbud[/url] Nowdays, der er mange værktøjer for os at gøre vores hår mere style.men de ghd fladjern er vores bedste valg, for det kan holde vores hår mere sundt og fashion.I foreslår, at du vasker dit hår og tørre det helt, før du bruger glattejern. Uanset om du bruger håret eller noget andet hår strygejern, vil de medføre repareres skader på hår, som er våd eller ej grundigt tør.Vi tilbyder mange former for billige GHD Babyliss, mærke glattejern, nye ankomst ghd fladjern, rabat ghd glattejern, osv. nye stilarter, god pris, forskellige farver, hurtig levering, mindre ordremængde accepteret.Efter min mening, vil du blive juble op ved nyheden om, at ghd glattejern er forberedt til you.All de ghd glattejern er meget chi og billige med overlegen quality.They er absolut det bedste produkt på markedet. [url=http://glattejernghdpris.webgarden.es/]Glattejern ghd pris[/url] Du kan manuelt styre temperaturen i ghd glattejern. Hvis du ønsker en høj temperatur kontakt dit hår, så du kan varme lang tid at nå dine ønskede temperatur. Du kan bruge. Bemærk: når du bruger ghd glattejern, skal du være opmærksom på deres egen sikkerhed, for at undgå at blive brændt. Let at bruge ghd glattejern, godt modelleringsværktøjer.Perfekt hår tænketank til at hjælpe dig med at ændre dit udseende, kan du finde online DVD præsentation af måder at opnå varme ghd glat hår stilarter trin for trin instruktioner. Såsom hands-on lære dig, så snart som muligt fortrolig med brugen af billige ghd glattejern. [url=http://ghdfladjerntilbud.webstarts.com/]GHD fladjern tilbud[/url] GHD glatning strygejern tilbyder forvandlet den faktiske kloden er forbundet med låse som absolut ingen yderligere element før. Hvilket er på grund af denne form for helt nye parabol forsyninger såsom keramik, der har indført dem alle i deres eget splinterny generationsskifte sted. Ikke bare udføre disse mennesker udfører varme langt bedre end standard stål retter, men de desuden fungerer med henblik på at distribuere hvilken varme. Men at besøge faktisk forbi hvordan den GHD element tilbyder desuden skabt den flade jern, som ikke blot tjener til at reducere den faktiske skade, men derudover kæmper de spørgsmål, der er forbundet med frizz som en række andre elementer og også de konventionelle håret udløst. Billige ghd glattejern 1st, skal du sørge for generelt at erhverve kommer fra en godkendt forhandler. Må ikke være tilbageholdende for dig at spørge ejeren bekymringer, herunder i tilfælde et element, der kan være hus til salg, vil være selve formålet i hele dit billede? Kan være denne specifikke en ny helt ny objekt samt anvendt objekt? Ofte tjek din sælgers udtalelser at afgøre, om du finde næsten eventuelle dårlige meninger. Hvis du gør, skal du sørge for at gå gennem netop den udfordring endte med at blive sammen med præcis, hvordan ejeren afgjort der vil noget. Spørge, hvis du finder nogen form for udvidet garanti sammen med netop, hvad hans eller hendes give tilbage forsikring planen kan være. En række eBayers kan også tilføje de oprindelige poster sammen med udvidet garanti data forsikrer, at det skal være en realistisk ghd fladjern.

    Reply
  • Nike Wind Max+instagram, will you hold the color to bear up on your feet!

    Posted by madytreathy on 04/22/2013 04:27am

    Remember in 2008, if not earlier, when Nike launched ahead of the separated shoe color projects, the catchword "Whiz Your Colours", "Nike PhotoiD" scheme, [url=http://markwarren.org.uk/goodbuy.cfm]nike free[/url] effect has not been as avid as expected. Have in mind, 2008 Canon IXUS 80 IS Digital card arcade but one 8 million pixels, Nokia, the facile phone market is the only regulation, NikeiD was support to color in the photos as a infrastructure someone is concerned sneakers duty color, although exciting, but does trouble some. Instagram which sort this thing make sport and elemental, Nike PHOTOiD homeopathic upgrade customization services, recently [url=http://markwarren.org.uk/property-waet.cfm]air max 90[/url] released a fresh plan. That such iD can you realize pictures as instagram account shoe color, for a short put up Nike Breeze Max shoes and Nike Style Max 1, Nike Show Max 90 953 options. Interested in children's shoes, you [url=http://markwarren.org.uk/goodbuy.cfm]nike free run[/url] can every conform with each other's valid website photoid.Nike.com, in besides to skim through other people's originative charge, or you can hear to upload your own instagram photo, physique your own Nike Mood Max.

    Reply
  • Mailman Class

    Posted by wisa on 01/29/2008 06:44pm

    A very powerful tool.!!!
    Thanks Saeed.
    
    W.SC!nchez

    • SORRY FOR DELAY IN RESPONSE AFTER A YEAR

      Posted by Saeed on 09/07/2009 01:31am

      BUT YOU ARE WELCOME

      Reply
    Reply
  • "a program is trying to automatically send email" message

    Posted by skinnyreptile on 03/02/2006 12:00pm

    Hi Saeed, First of all, great code. I run into a little problem hope you can help me out. I am using Outlook 2000 SP-3. When I send email out, the Outlook security feature pops up "a program is trying to automatically send email" message which force user to response in order to send out the message. Can you tell me how to bypass this message? Thank you.

    • YOU CANT

      Posted by Saeed on 09/07/2009 01:30am

      YOU CANT - THATS YOUR ANTI VIRUS/SPAM THING

      Reply
    Reply
  • Thank you

    Posted by Legacy on 11/17/2003 12:00am

    Originally posted by: Manoj Kumar

    Hi Saeed,
    Thank you very much for this article. I have successfully modified my code on my own for attachments. Please try to make a class which should be able to handle the mails without outlook.

    • ok

      Posted by tmem on 08/06/2005 10:35pm

      ok

      Reply
    Reply
  • How to attach files

    Posted by Legacy on 11/14/2003 12:00am

    Originally posted by: Manoj Kumar

    I find this code very useful in my project. Thanx a lot.
    But i tried to attach few files by modifying SendMail function. but i could not . can u please help me modify code. so that i can send attachments also.

    Reply
  • Connecting to the IE's defautl mail client

    Posted by Legacy on 09/11/2003 12:00am

    Originally posted by: lefteris

    Hi,

    I 'm developing a custom web browser in a dialog based MFC application hosting the Web Browser control. My question regards the mailto protocol and the behavior of the Web Browser control in this case.
    When a page contains an <a> element with href="mailto:.." the web browser control's action is to open the default mail client (outlook express in most cases) and let the user type her message. I wonder if there is any way to connect in some way to this new instance of the outlook express. Any help would be very appreciated

    Thanks in advance

    Lefteris Trimintzios

    Reply
  • return receipt request doesn't seem to work when sending

    Posted by Legacy on 08/18/2003 12:00am

    Originally posted by: Phil Sobolik

    What a useful class! Thanks a lot.

    I tried to send an email an request a return reciept by setting MapiMessage::flFlags to MAPI_RECEIPT_REQUESTED. It seems that this flag is read only like the sender name and address. True?

    Reply
  • Sending EMail without Outlook Popup

    Posted by Legacy on 08/09/2003 12:00am

    Originally posted by: Robert Valentino

    ****o:

    I am downloading your app as I am typing.

    I tried your demo and every time I do a send Outlook puts up this dialog box saying an EMail is being sent from my computer and forces me to wait 5 seconds before I can say yes to send the message.

    I want to use your MailMan to send EMail from an Unattended PC. Is there someway to avoid this popup and having to press YES to send the EMail.

    Thanks

    Bob Valentino

    Reply
  • Eh?

    Posted by Legacy on 07/21/2003 12:00am

    Originally posted by: Matt

    Where can I download the mailman.h file?

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

  • Live Event Date: August 14, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Data protection has long been considered "overhead" by many organizations in the past, many chalking it up to an insurance policy or an extended warranty you may never use. The realities of today makes data protection a must-have, as we live in a data-driven society -- the digital assets we create, share, and collaborate with others on must be managed and protected for many purposes. Check out this upcoming eSeminar and join Seagate Cloud …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds