Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Mail using C

Posted on 2006-06-24
11
Medium Priority
?
342 Views
Last Modified: 2008-03-10
Hi

I want to use mail function in c with gcc.
0
Comment
Question by:carchitect
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
11 Comments
 
LVL 15

Expert Comment

by:bpmurray
ID: 16975656
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
 
LVL 6

Author Comment

by:carchitect
ID: 16977783
Hi

I am using gcc on Linux and Programming in 'C'. Will MAPI work with it.
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 16978346
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 6

Expert Comment

by:billtouch
ID: 16979757
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
 
LVL 6

Author Comment

by:carchitect
ID: 16982393
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
 
LVL 15

Accepted Solution

by:
bpmurray earned 200 total points
ID: 16982845
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
 
LVL 6

Author Comment

by:carchitect
ID: 16989979
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
 
LVL 6

Author Comment

by:carchitect
ID: 17016300
How can i attach a file.
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 17016439
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
 
LVL 6

Author Comment

by:carchitect
ID: 17017010
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
 
LVL 15

Expert Comment

by:bpmurray
ID: 17017435
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question