🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.socket
Article #5549

Re: I need a simple example of sending an email

#5549
From: Colin B Maharaj
Date: Wed, 04 Aug 2004 11:14
160 lines
4597 bytes
Charles W Leggette wrote:
> Why is this so daggone hard. Please use code that can run under C++bldr 6,
> sp4. Its easy as pie under MS VC++6.0 or 7.0??? I've tried FastNet,etc...no
> success.
> 
> 

This is straight from an application I am writing... no time to clean it 
up need to go to work.........

AnsiString SendMail(AnsiString emailto, AnsiString subject, AnsiString 
MailFile)
{

   TWinshoeSMTP * SMTP;
   TWinshoeMessage * msg;

   AnsiString Attachment;
   AnsiString SendMsg = "Mail Sent OK";
   bool Attached = false;
   int state=0;

   SMTP = new TWinshoeSMTP(NULL);
   msg = new TWinshoeMessage(NULL);
   try
   {
     SMTP->Host      = Host2;// External Var e.g. mail.whocares.com
     SMTP->UserID    = UserID2;// External Var e.g. jd
     msg->Subject    = subject;
     AnsiString From = (AnsiString)"John Doe <jd@whocares.com">";
     msg->From = From;

     AnsiString EM = "~";
     int q=0;
     while (EM != " ")
     {
       EM = GetCsvParameter(emailto.c_str(), q++);
       if (EM.Length()>1)
       {
         if (q == 1)
         {
           msg->Too->Text = EM;
         }
         else
         {
           msg->CCList->Add(EM);
         }
       }
     }



     AnsiString MailMsg = "See ZIPPED attachment for report";
//    bool IsRtf = false;
     if (MailFile.Pos(".rtf") > 0) msg->ContentType = "application/rtf";
     if (MailFile.Pos(".htm") > 0) msg->ContentType = "text/html";
     if (MailFile.Pos(".doc") > 0) msg->ContentType = "text/plain";

     if (FileExists(MailFile)==false)
     {
       MailMsg = MailFile + " not found, or no data in report!";
     }
     else
     {
       if (GetFileSize(MailFile.c_str()) > (10*1024))
       {
//        Attachment = DoZipFile(MailFile.c_str(), MailFile.c_str());
         msg->Attachments->AddAttachment(Attachment);
         Attached = true;
       }
       else
       {
         TStringList * S = new TStringList;
         S->LoadFromFile(MailFile);
         msg->Text = S;
         S->Clear();
         delete S;
       }
     }

     state = 1;
     SMTP->Send(msg);

     delete msg;
     delete SMTP;
     state = 0;

//    if (Attached == true) DeleteFile(Attachment);
   }

   catch (Exception & e)
   {
     SendMsg = (AnsiString) "Email Error : " + e.Message;
     if (state==1)
     {
       SMTP->Disconnect();
     }
     delete SMTP;
     delete msg;
     if (Attached ==true) DeleteFile(Attachment);
     return SendMsg;
   }

   catch (...)
   {
     delete SMTP;
     delete msg;
     SendMsg = "Email Error : Undefined error";
     return SendMsg;
   }

   return SendMsg;
}




// used to separate comma separated email addresses..
AnsiString GetCsvParameter(const char *s, int index)
{
     AnsiString ret = (AnsiString) " ";
     int numcommas=0;
     int len = strlen(s);
     for (int i=0; i<len; i++)
     {
       if (s[i]=='\,') numcommas++;
     }

     if (numcommas == 0 && index == 0)
     {
        ret = s; return ret;
     }

     if (numcommas < index)
     {
        return ret;
     }

     int n = index;
     while (n && *s)
     {
       if (*s++ == '\,')
       {
         n--;
       }
     }
     if (!*s) return ret;

     char *t = new char [1024]; // NOT an Error. Other pointer deleted.
     int q = 0;
     while (*s && *s=='\,') s++; /// was while could cause errors if  ,,
     while (*s && *s!='\,' &&  q < 1023)
     {
       t[q++] = *s++;
     }
     t[q++] = NULL;
     ret = t;
     delete t;
     return ret;
}
Technical Details
Message-ID: <4110c529@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!logbridge.uoregon.edu!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <41099df0$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,aadb9a10f427e91
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
Date: Wed, 04 Aug 2004 07:14:45 -0400
From: Colin B Maharaj <noreply@myhost.com>
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7a) Gecko/20040219
X-Accept-Language: en-us, en
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.internet.socket
Subject: Re: I need a simple example of sending an email
References: <41099df0$1@newsgroups.borland.com>
In-Reply-To: <41099df0$1@newsgroups.borland.com>
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 200.108.15.100
Message-ID: <4110c529@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1091618089 200.108.15.100 (4 Aug 2004 04:14:49 -0700)
Lines: 160
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!logbridge.uoregon.edu!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail