Thread: Would like some help / code to send an email

  1. #1
    Registered User Ivory348's Avatar
    Join Date
    Oct 2019
    Posts
    75

    Would like some help / code to send an email

    I found this code on the Net, but cannot get it going. I know you don't like this sort of query, but I cannot do it myself.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
    
            char cmd[100];  // to hold the command.
            char to[] = "[email protected]"; // email id of the recepient.
            char body[] = "SO rocks";    // email body.
            char tempFile[100];     // name of tempfile.
    
            strcpy(tempFile,tempnam("/tmp","sendmail")); // generate temp file name.
    
            FILE *fp = fopen(tempFile,"w"); // open it for writing.
            fprintf(fp,"%s\n",body);        // write body to it.
            fclose(fp);             // close it.
    
            sprintf(cmd,"sendmail %s < %s",to,tempFile); // prepare command.
            system(cmd);     // execute it.
    
            return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So, if you open up a command console and type in

    sendmail [email protected] < text.txt

    What do you see?

    Are you on a Linux/Unix/BSD type system?
    Do you have sendmail installed?

    Using system() is basically the program version of typing things in yourself, but with less information when things go wrong.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User Ivory348's Avatar
    Join Date
    Oct 2019
    Posts
    75
    Quote Originally Posted by Salem View Post
    So, if you open up a command console and type in

    sendmail [email protected] < text.txt

    What do you see?

    Are you on a Linux/Unix/BSD type system?
    Do you have sendmail installed?
    <snipped>
    I am on Windows10.
    After entering what you suggest I get: "Windows cannot find sendmail"

    So I suppose I am out of luck, with the code above.
    Might you know of a viable alternative?
    Last edited by Ivory348; 02-03-2020 at 01:47 PM.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User Ivory348's Avatar
    Join Date
    Oct 2019
    Posts
    75
    Quote Originally Posted by john.c View Post
    Yes that would be the easy way out. However I prefer to use my own email client. I scrapped my programs that I wrote in C# (also all other Microsoft stuff) and am rewriting my programs in C or a Basic dialect. So I hope someone will help me with this query.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You might be able to compile libcurl for Windows or otherwise obtain a library file since curl is available for Windows, then use its SMTP functionality to send an email from your program.

    Quote Originally Posted by Ivory348
    Yes that would be the easy way out.
    Using Send-MailMessage is arguably akin to using sendmail though, so if you were willing to invoke sendmail in post #1 using system(), it is reasonable to suppose that you would be willing to invoke Send-MailMessage too.
    Last edited by laserlight; 02-04-2020 at 03:58 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User Ivory348's Avatar
    Join Date
    Oct 2019
    Posts
    75
    Quote Originally Posted by laserlight View Post
    You might be able to compile libcurl for <snip>

    Using Send-MailMessage is arguably akin to using sendmail though, so if you were willing to invoke sendmail in post #1 using system(), it is reasonable to suppose that you would be willing to invoke Send-MailMessage too.
    I misunderstood. I thought I was being told to use my regular email program. But, having checked whether I have send-mailmessage on board - the answer is no. I don't have it on my W10.

  8. #8
    Registered User
    Join Date
    Dec 2017
    Posts
    1,626
    Quote Originally Posted by Ivory348 View Post
    I misunderstood.
    Due to a total lack of experience, no doubt. But your "opinions" are still "valuable".

    In the unlikely event that you don't have Send-MailMessage (the windows equivalent of sendmail for *nix), I can think of three other ways to send mail on windows. But you are too horrible a person to bother helping.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  9. #9
    Registered User Ivory348's Avatar
    Join Date
    Oct 2019
    Posts
    75
    In response to John C.:

    "Due to a total lack of experience, no doubt. But your "opinions" are still "valuable". Your remark is accurate.

    "But you are too horrible a person to bother helping.
    " Coming from you, that is a relief.

    Would it be too much to ask you, to stop responding to queries that are not directed specifically at you? Our backgrounds don't seem to be compatible. We don't speak the same language.
    Last edited by Ivory348; 02-04-2020 at 02:34 PM.

  10. #10
    Registered User Ivory348's Avatar
    Join Date
    Oct 2019
    Posts
    75
    To Laserlight: Thanks for your response. As John C. so aptly and accurately remarks (q.v.) , I have little experience with things like "power shell". In C# there are freely downloadable libraries that make it easy to send emails form applications. I was hoping it to be the same in C.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I don't have it on my W10.
    You need to run powershell, not cmd.exe

    Would like some help / code to send an email-pshell-png
    Maybe you can monkey around with the command line to feed something into powershell via cmd.exe.

    system() only runs with cmd.exe (perhaps), but there is an environment variable called ComSpec which ordinarily points to cmd.exe.

    I don't know if changing the ComSpec environment variable in a program to point to powershell, then calling system() gets you directly to powershell.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User Ivory348's Avatar
    Join Date
    Oct 2019
    Posts
    75
    Quote Originally Posted by Salem View Post
    > I don't have it on my W10.
    You need to run powershell, not cmd.exe
    [/ATTACH]
    Maybe you can monkey around with the command line to feed something into powershell via cmd.exe.
    system() only runs with cmd.exe (perhaps), but there is an environment variable called ComSpec which ordinarily points to cmd.exe.
    I don't know if changing the ComSpec environment variable in a program to point to powershell, then calling system() gets you directly to powershell.
    Salem, thank you very much. In the mean time I did find information like yours above, on the Net. However I want to create an application from which to send emails. I don't immediately see how I could achieve that in C using Power Shell. I will have a look at what can be done in a Basic dialect. Thank you again.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ivory348
    In C# there are freely downloadable libraries that make it easy to send emails form applications. I was hoping it to be the same in C.
    That's why I suggested libcurl, e.g., smtp-mail.c
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User Ivory348's Avatar
    Join Date
    Oct 2019
    Posts
    75
    Re "That's why I suggested libcurl, e.g., smtp-mail.c"

    Thank you for telling me again.
    Apologies for not having paid attention.
    I will try using thing the code.
    Last edited by Ivory348; 02-05-2020 at 05:24 AM.

  15. #15
    Registered User Ivory348's Avatar
    Join Date
    Oct 2019
    Posts
    75
    Quote Originally Posted by laserlight View Post
    That's why I suggested libcurl, e.g., smtp-mail.c
    I nosed around the net for curl. My impression is that being able to apply Curl stuff requires a big investment of time and frustrations. It doesn't seem to be a matter of a download and plug it in.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to send email
    By michealCelena in forum C Programming
    Replies: 3
    Last Post: 09-10-2018, 12:25 AM
  2. Send email
    By surgeon in forum C++ Programming
    Replies: 1
    Last Post: 04-22-2017, 05:26 PM
  3. can't we send email using C ?
    By melviii100 in forum C Programming
    Replies: 4
    Last Post: 02-26-2014, 01:35 PM
  4. Send email
    By Abda92 in forum Windows Programming
    Replies: 4
    Last Post: 12-29-2007, 09:09 AM
  5. How to Send Email
    By sampatel in forum C Programming
    Replies: 1
    Last Post: 11-23-2002, 02:07 PM

Tags for this Thread