• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 345
  • Last Modified:

Mail using C

Hi

I want to use mail function in c with gcc.
0
carchitect
Asked:
carchitect
  • 5
  • 5
1 Solution
 
bpmurrayCommented:
It really depends on how you want to do this. You can either connect directly to a specific mail system, or use some generic standardised solution. As an example of the latter, you could use MAPI (which mose E-mail apps support) and do something like:


    #include <mapi.h>

    MapiMessage myMsg;
    .....

    if (MAPISendMail(0, 0, &myMsg, MAPI_DIALOG, 0) == SUCCESS_SUCCESS)
    {
         printf("OK, I sent that one!\n");
    } else {
         fprintf(stderr, "Uh-oh! That didn't go to well\n");
    }

Note that if you're doing this on the windows platform, I think that this API isn't exported from the DLL, so you'll have to pick it up explicitly, something like:
   HANDLE hMAPILib = LoadLibrary("MAPI32.DLL");
   lpfnMAPISendMail = (LPMAPISENDMAIL)GetProcAddress(hMAPILib, "MAPISendMail");
    (*lpfnMAPISendMail)(0, 0, &myMsg, MAPI_DIALOG, 0);
0
 
carchitectAuthor Commented:
Hi

I am using gcc on Linux and Programming in 'C'. Will MAPI work with it.
0
 
bpmurrayCommented:
Well, MAPI is available on all platforms, but the implementation is slightly different, so the answer is yes, but you'll need an MTA. I'm not familiar with that market, but I know that they definietly exist, and the programming depends on the particular implementation. An alternative on Linux (and probably easier) is to use sendmail, the ubiquitous mail system. Of course, you could write your own SMTP solution - it's actually remarkably simple - you open the socket on the server, send a few predefined strings (FROM, etc) and the associated values, send your message and close the socket.

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
billtouchCommented:
The bottom line about MAPI is that it is from Microsoft. While it may be possible to port parts of it to Linux, I have to ask why? Does Microsoft develop such great code that Linux can't live without it?

If your application needs to run on both platforms, then that is an avenue to approach. But piping stuff to sendmail is quite easy and if you can read (and interpret) RFC's, then you can put together an email client for yourself as suggested by bpmurry. An additional point, if you use the socket approach, that will be portable as well. Microsoft has a pretty good implementation of sockets.

Indicate what you would like to do and I can send you to the right places.

Bill
0
 
carchitectAuthor Commented:
Hi

I am using RHEL, gcc. My 'C' application needs to send an email to the required email address in between. So i am looking for the same.

system("mail -s HI carchitect@hotmail.com < abc");

abc file contans the text to be emailed. Its working but i dont want to use system command & i am unable to send HTML emails from it.
0
 
bpmurrayCommented:
Rather than doing it that way, you could do something like this - I haven't tested it, so it'll probably need some tweaking ....

void sendHtmlFile(char *fileName, char *sender, char *subject, char *recipient, char *charset)
{
    FILE *htmlFP, *mailPipe;
    char buffer[BIGSIZE];
   
    mailPipe = popen("/usr/lib/sendmail", "w");
    htmlFP = fopen(fileName, "r");

    fprintf(mailPipe, "Mime-Version: 1.0\nContent-type: text/html; charset=\"%s\"\n", charset);
    fprintf(mailPipe, "From: %s\nTo: %s\nSubject: %s\n", sender, recipient, subject);
    while (fgets(buffer, sizeof(buffer), htmlFP))
    {
        fprintf(mailPipe, "%s\n", buffeer);
    }
    fclose(htmlFP);
    pclose(mailPipe);
}
   
0
 
carchitectAuthor Commented:
Thx Alot

Its working. It was asking for "Recipient names must be specified" but i opened the pipe along with the Recipient Name like

mailPipe = popen("/usr/lib/sendmail carchitect@hotmail.com", "w");


0
 
carchitectAuthor Commented:
How can i attach a file.
0
 
bpmurrayCommented:
You could try uuencoding it and then sending it embedded in the mail body, although I don't know how that would work with HTML. Howerev, it's probably more correct to use mutt or metamail. For example:

char cmdBuffer[1024];
sprintf(cmdBuffer, "mutt -s %s -a %s %s", subject, filename, recipient);
mailpipe = popen(cmdBuffer, "w");
0
 
carchitectAuthor Commented:
I have tried reaching a place like


        fprintf(mailPipe, "Mime-Version: 1.0\n");
        fprintf(mailPipe, "Content-type: multipart/mixed; boundary=\"----=papa\"\n");
        fprintf(mailPipe, "----=papa\n");
        fprintf(mailPipe, "Content-type: text/html;\ncharset=\"%s\"\n", charset);
        fprintf(mailPipe, "Content-Transfer-Encoding: quoted-printable\n");

        htmlFP = fopen(fileName, "r");
        while (fgets(buffer, sizeof(buffer), htmlFP))
        {
                fprintf(mailPipe, "%s\n", buffer);
        }
        fclose(htmlFP);
        fprintf(mailPipe, "\n");
        fprintf(mailPipe, "----=papa--\n");

        fprintf(mailPipe, "----=dada\n");
        fprintf(mailPipe, "Content-type: application/pdf;\nname=\"fnat.pdf\"\n");
        fprintf(mailPipe, "Content-Transfer-Encoding: base64\n");
        fprintf(mailPipe, "Content-Disposition: attachment;\n");
        fprintf(mailPipe, "filename=\"fnat.pdf\"\n");
        fprintf(mailPipe,"\n");
        htmlFP = fopen("/home/scripts/fnat.pdf", "r");
        while (fgets(buffer, sizeof(buffer), htmlFP))
        {
                fprintf(mailPipe, "%s\n", buffer);
        }
        fclose(htmlFP);

        fprintf(mailPipe, "----=dada--\n");



boundary is creating an issue
0
 
bpmurrayCommented:
I have no idea what'll happen there :-)

One thing to watch out for is the use of fgets on a PDF file. Since PDF is a binary format, fgets is not dependable - you'll have to use fread on the file & fwrite on the data to the pipe.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now