Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #41282

Re: send email and socket programming

From alexo <alelvb@inwind.it>
Newsgroups comp.lang.c++
Subject Re: send email and socket programming
Date 2016-02-27 18:59 +0100
Organization Aioe.org NNTP Server
Message-ID <naso5m$594$1@gioia.aioe.org> (permalink)
References (4 earlier) <napfkh$15em$1@gioia.aioe.org> <eM6dnbIKR64k2k3LnZ2dnUU78RmdnZ2d@giganews.com> <naqk5o$rt3$1@dont-email.me> <nas5d2$15ah$1@gioia.aioe.org> <fd392257-14cb-4b73-b15e-2772b77ec4cf@googlegroups.com>

Show all headers | View raw


> Now can you please break the program with debugger and look:
>      Where does it "hang"?
>      How does it "hang"?
>      Why does it "hang"?
>      What's it doing at the time of "hanging"?
>

This is the source code of the toy program. It is executed correctly
but for the login you obviously have to type in your username password 
and smtp server address.

when I insert this code in my program,
This code being called by pressing a button on the GUI transmission goes 
forever.
the line that fails is this:


res = curl_easy_perform(curl);


It starts the transmission of the data and should return.
This is not what happens. The funclion locks the wireless transmission
and after closing the software with xkill, data transmission still 
continues, but no mail starts.


/***************************************************************************
  *                                  _   _ ____  _
  *  Project                     ___| | | |  _ \| |
  *                             / __| | | | |_) | |
  *                            | (__| |_| |  _ <| |___
  *                             \___|\___/|_| \_\_____|
  *
  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
  *
  * This software is licensed as described in the file COPYING, which
  * you should have received as part of this distribution. The terms
  * are also available at https://curl.haxx.se/docs/copyright.html.
  *
  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  * copies of the Software, and permit persons to whom the Software is
  * furnished to do so, under the terms of the COPYING file.
  *
  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY 
OF ANY
  * KIND, either express or implied.
  *
 
***************************************************************************/

/* <DESC>
  * SMTP example showing how to send e-mails
  * </DESC>
  */

#include <stdio.h>
#include <string.h>

#include <time.h>
#include <cstdlib>

#include <curl/curl.h>

/* This is a simple example showing how to send mail using libcurl's SMTP
  * capabilities. For an example of using the multi interface please see
  * smtp-multi.c.
  *
  * Note that this example requires libcurl 7.20.0 or above.
  */

#define FROM    "<alelvb@inwind.it>"
#define TO      "<alessandro.volturno@libero.it>"
//#define CC    "<alelvb@inwind.it>"

char *current_date();

char *date = current_date();

static const char *payload_text[] = {
   "To: " TO "\r\n",
   "From: " /*FROM*/ "Stilottica\r\n",
   //"Cc: " CC "Another example User\r\n",
   "Subject: ordine lenti\r\n",  // subject of tha message
   "\r\n", /* empty line to divide headers from body, see RFC5322 */
   "Ordine del ",  //The body of the message starts here.
   // more strings go here
   date,
   "\r\nA long text email can be very tedious to read."
   "\r\n-- end of the message --",
   NULL
};

struct upload_status {
   int lines_read;
};

static size_t payload_source(void *ptr, size_t size, size_t nmemb, void 
*userp)
{
   struct upload_status *upload_ctx = (struct upload_status *)userp;
   const char *data;

   if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
     return 0;
   }

   data = payload_text[upload_ctx->lines_read];

   if(data) {
     size_t len = strlen(data);
     memcpy(ptr, data, len);
     upload_ctx->lines_read++;

     return len;
   }

   return 0;
}

int main(void)
{
     CURL *curl;
     CURLcode res = CURLE_OK;
     struct curl_slist *recipients = NULL;
     struct upload_status upload_ctx;

     upload_ctx.lines_read = 0;

     curl = curl_easy_init();

/* replace xxx with your credentials */

     if(curl) {
     /* This is the URL for your mailserver */
     curl_easy_setopt(curl, CURLOPT_USERNAME, "xxxxxxxxx@xxxxxx.xx");   //
     curl_easy_setopt(curl, CURLOPT_PASSWORD, "xxxxxxxx");
     curl_easy_setopt(curl, CURLOPT_URL, "xxxxxxxx");


     /* Note that this option isn't strictly required, omitting it will 
result
      * in libcurl sending the MAIL FROM command with empty sender data. All
      * autoresponses should have an empty reverse-path, and should be 
directed
      * to the address in the reverse-path which triggered them. Otherwise,
      * they could cause an endless loop. See RFC 5321 Section 4.5.5 for 
more
      * details.
      */
     curl_easy_setopt(curl, CURLOPT_MAIL_FROM, FROM);

     /* Add two recipients, in this particular case they correspond to the
      * To: and Cc: addressees in the header, but they could be any kind of
      * recipient. */
     recipients = curl_slist_append(recipients, TO);
     //recipients = curl_slist_append(recipients, CC);
     curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);

     /* We're using a callback function to specify the payload (the 
headers and
      * body of the message). You could just use the CURLOPT_READDATA 
option to
      * specify a FILE pointer to read from. */
     curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
     curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
     curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);

     /* Send the message */
     res = curl_easy_perform(curl);

     /* Check for errors */

     if(res != CURLE_OK)
     {
         fprintf(stderr, "curl_easy_perform() failed: %s\n", 
curl_easy_strerror(res));
     }
     else
     {
         printf("message sent!");
     }

     /* Free the list of recipients */
     curl_slist_free_all(recipients);

     /* curl won't send the QUIT command until you call cleanup, so you 
should
      * be able to re-use this connection for additional messages (setting
      * CURLOPT_MAIL_FROM and CURLOPT_MAIL_RCPT as required, and calling
      * curl_easy_perform() again. It may not be a good idea to keep the
      * connection open for a very long time though (more than a few minutes
      * may result in the server timing out the connection), and you do 
want to
      * clean up in the end.
      */
     curl_easy_cleanup(curl);
     }

     return (int)res;
}

char *current_date()
{
     time_t rawtime;
     struct tm *timeinfo = NULL;

     char *buf = new char[35];

     if(buf == NULL)
     {
         printf("string allocation error\n");
         exit(1);
     }

     time(&rawtime);

     if ((timeinfo = localtime(&rawtime)) == NULL)
     {
         printf("data generation error");
         exit(1);
     }

     sprintf(buf,"%.2d/%.2d/%4d  ore  %.2d:%.2d",
             timeinfo->tm_mday,
             1 + timeinfo->tm_mon,
             1900 + timeinfo->tm_year,
             timeinfo->tm_hour,
             timeinfo->tm_min
            );

     return buf;
}

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

send email and socket programming alexo <alelvb@inwind.it> - 2016-02-14 18:01 +0100
  Re: send email and socket programming JiiPee <no@notvalid.com> - 2016-02-14 17:07 +0000
    Re: send email and socket programming JiiPee <no@notvalid.com> - 2016-02-14 17:12 +0000
    Re: send email and socket programming alexo <alelvb@inwind.it> - 2016-02-14 19:44 +0100
  Re: send email and socket programming Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-14 19:18 +0200
  Re: send email and socket programming Öö Tiib <ootiib@hot.ee> - 2016-02-14 09:39 -0800
    Re: send email and socket programming alexo <alelvb@inwind.it> - 2016-02-14 20:23 +0100
  Re: send email and socket programming woodbrian77@gmail.com - 2016-02-14 10:47 -0800
  Re: send email and socket programming Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-14 21:19 +0200
    Re: send email and socket programming alexo <alelvb@inwind.it> - 2016-02-14 20:29 +0100
      Re: send email and socket programming Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-14 22:29 +0200
        Re: send email and socket programming alexo <alelvb@inwind.it> - 2016-02-14 22:45 +0100
          Re: send email and socket programming Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-15 00:15 +0200
            Re: send email and socket programming Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-15 00:39 +0200
              Re: send email and socket programming Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2016-02-14 23:01 +0000
                Re: send email and socket programming Geoff <geoff@invalid.invalid> - 2016-02-14 15:22 -0800
                Re: send email and socket programming Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-14 18:41 -0500
                Re: send email and socket programming Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2016-02-14 23:43 +0000
                Re: send email and socket programming Geoff <geoff@invalid.invalid> - 2016-02-14 17:33 -0800
                Re: send email and socket programming Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-15 09:37 +0200
                Re: send email and socket programming Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2016-02-15 10:04 +0000
                Re: send email and socket programming Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2016-02-15 10:14 +0000
                Re: send email and socket programming Geoff <geoff@invalid.invalid> - 2016-02-15 10:11 -0800
          Re: send email and socket programming Geoff <geoff@invalid.invalid> - 2016-02-14 15:16 -0800
  Re: send email and socket programming Ian Collins <ian-news@hotmail.com> - 2016-02-15 08:40 +1300
    Re: send email and socket programming "Chris M. Thomasson" <nospam@nospam.nospam> - 2016-02-14 12:09 -0800
  Re: send email and socket programming Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-14 16:04 -0500
  Re: send email and socket programming jt@toerring.de (Jens Thoms Toerring) - 2016-02-14 21:20 +0000
    Re: send email and socket programming alexo <alelvb@inwind.it> - 2016-02-14 22:40 +0100
      Re: send email and socket programming Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2016-02-14 22:20 +0000
        Re: send email and socket programming Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-14 18:01 -0500
          Re: send email and socket programming Ian Collins <ian-news@hotmail.com> - 2016-02-15 12:09 +1300
            Re: send email and socket programming Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-14 18:37 -0500
              Re: send email and socket programming Ian Collins <ian-news@hotmail.com> - 2016-02-15 13:06 +1300
                Re: send email and socket programming Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-14 21:40 -0500
          Re: send email and socket programming Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2016-02-14 23:35 +0000
            Re: send email and socket programming Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-14 21:43 -0500
      Re: send email and socket programming Jerry Stuckle <jstucklex@attglobal.net> - 2016-02-14 18:05 -0500
  Re: send email and socket programming Christopher Pisz <nospam@notanaddress.com> - 2016-02-16 16:39 -0600
  Re: send email and socket programming alexo <alelvb@inwind.it> - 2016-02-26 04:09 +0100
    Re: send email and socket programming Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-26 07:49 +0200
      Re: send email and socket programming Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-26 08:39 +0200
        Re: send email and socket programming alexo <alelvb@inwind.it> - 2016-02-26 13:15 +0100
          Re: send email and socket programming Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-26 14:40 +0200
            Re: send email and socket programming Christopher Pisz <nospam@notanaddress.com> - 2016-02-26 16:41 -0600
              Re: send email and socket programming alexo <alelvb@inwind.it> - 2016-02-27 13:38 +0100
                Re: send email and socket programming Öö Tiib <ootiib@hot.ee> - 2016-02-27 05:15 -0800
                Re: send email and socket programming alexo <alelvb@inwind.it> - 2016-02-27 18:59 +0100
                Re: send email and socket programming Öö Tiib <ootiib@hot.ee> - 2016-02-27 10:41 -0800
                Re: send email and socket programming Paavo Helde <myfirstname@osa.pri.ee> - 2016-02-27 21:46 +0200
              Re: send email and socket programming alexo <alelvb@inwind.it> - 2016-02-27 13:42 +0100
                Re: send email and socket programming alexo <alelvb@inwind.it> - 2016-02-27 19:14 +0100
                Re: send email and socket programming alexo <alelvb@inwind.it> - 2016-02-27 19:22 +0100
                Re: send email and socket programming guinness.tony@gmail.com - 2016-02-28 14:19 -0800
      Re: send email and socket programming alexo <alelvb@inwind.it> - 2016-02-27 18:44 +0100
    Re: send email and socket programming Jorgen Grahn <grahn+nntp@snipabacken.se> - 2016-02-27 17:59 +0000
  Re: send email and socket programming alexo <alelvb@inwind.it> - 2016-02-29 14:16 +0100

csiph-web