Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending email thro' C program

Status
Not open for further replies.

nappaji

Programmer
Mar 21, 2001
76
US
How do I send an email through a C/C++ program??

Ex: If I want to send the string "Hello World" to abc@hotmail.com, what utility do I use??

Please help.
 
Which operating system and compiler do you have?
 
Well the quick answer is to prepare a text file containing the text you want to send, then create a command like this

Code:
char cmd[200];
char to[] = "user@host";
char subject[] = "testing 1 2 3";
sprintf( cmd, &quot;mail -s %s %s < file.txt&quot;, subject, to );
system( cmd );

You can omit the temporary file by doing something like
Code:
FILE *fp;
char cmd[200];
char to[] = &quot;user@host&quot;;
char subject[] = &quot;testing 1 2 3&quot;;
sprintf( cmd, &quot;mail -s %s %s&quot;, subject, to );
fp = popen( cmd, &quot;w&quot; );
fprintf( fp, &quot;Hello world\n&quot; );  // the message to send
pclose( fp );

You should note that system() can be somewhat insecure. If this is a concern of yours, then you need to mimic all the process and redirection stuff yourself through the use of fork() execl() pipe() dup() system calls.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top