if you will do it in 'C', try
--------------------------------------------
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#define NOFILE 64
/*********
change the next 2 lines
*********/
#define SUBJECT "Mail-Subject"
#define MAILTO "username@domain.com"
int main(int argc,char **argv)
{
extern char *basename(char *);
char *myname = basename(*argv);
int ppp[2], mail;
if(0 >pipe(ppp)) exit(fprintf(stderr,"%s: Can't create pipe\n",myname));
if(0 >(mail = fork())) exit(fprintf(stderr,"%s: Can't fork\n",myname));
if(!mail){ /* child process, start mail */
(void)dup2(ppp[0],0);
for(mail = 3; mail< NOFILE; (void)close(mail++));
execl("/bin/mailx","mailx","-s",SUBJECT,MAILTO,(char *)0);
exit(fprintf(stderr,"%s: Can't execl\n",myname));
}
/* father-process, write text for mail */
(void)dup2(ppp[1],1);
fprintf(stdout,"...........mail text............\n"

;
fprintf(stdout,"................................\n"

;
fprintf(stdout,"................................\n"

;
fprintf(stdout,"................................\n"

;
fprintf(stdout,"...........end mail.............\n"

;
fflush(stdout);
(void)close(1);
(void)close(ppp[0]);
(void)close(ppp[1]);
(void)wait(&mail);
exit(0);
}