Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 01-20-2004, 01:32 PM   #1
LQ Newbie
Registered: Jan 2004
Location: Lahore
Distribution: Red Hat
Posts: 10

Rep: Reputation: 0
SMTP implementation

Hello people
I am stuck here with a little problem. As i am intrested in doing a little socket programming. I was looking for SMTP implementation. i got it downloaded from the net. Now I want to know about it that how it works. I have certain confusions about the code, that I want to clear. I am uploading the soucrce (smtp.c file) and I have commented every line with my confusion. I hope some one expert in socket programming will help me out with this.Yesi know it would take a little time but would be a great contribution towards my learning
Thank you all...for reading

here's the smtp code
#include <stdio.h>
#include <io.h>

#include <stdio.h>//why do we need to include this again.....when we have done it above

//#define LINUX /* define this if you are on linux */
//#define WIN32 /* define this if you are on windows */

#ifdef WIN32
# include "io.h"
# include "winsock2.h" /* WSAGetLastError, WSAStartUp */
# define snprintf _snprintf

#ifdef LINUX
# include <netdb.h> /* gethostbyname */
# include <netinet/in.h> /* htons */
# include <sys/socket.h>

static void sendmail_write(
const int sock,
const char *str,
const char *arg
) {
char buf[4096];

if (arg != NULL)
snprintf(buf, sizeof(buf), str, arg); //I didnt get it
snprintf(buf, sizeof(buf), str); //I didnt get it

send(sock, buf, strlen(buf), 0); //I didnt get it

static int sendmail(
const char *from,
const char *to,
const char *subject,
const char *body,
const char *hostname,
const int port
) {
struct hostent *host; // where is hostent structure defined
struct sockaddr_in saddr_in; //and where is this structure
int sock = 0;

#ifdef WIN32
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
return -1;
#endif //should we disable it if we want it to be run on linux platform

sock = socket(AF_INET, SOCK_STREAM, 0); //wot is AF_INET and SOCK_STREAM??//
host = gethostbyname(hostname);//wher is gethostbyname

saddr_in.sin_family = AF_INET; //didnt get it completely
saddr_in.sin_port = htons((u_short)port);//didnt get it completely//
saddr_in.sin_addr.s_addr = 0;//didnt get it completely

memcpy((char*)&(saddr_in.sin_addr), host->h_addr, host->h_length);////didnt get it completely

if (connect(sock, (struct sockaddr*)&saddr_in, sizeof(saddr_in)) == -1) {//didnt get it
return -2;

sendmail_write(sock, "HELO %s\n", from); // greeting
sendmail_write(sock, "MAIL FROM: %s\n", from); // from
sendmail_write(sock, "RCPT TO: %s\n", to); // to
sendmail_write(sock, "DATA\n", NULL); // begin data

// next comes mail headers
sendmail_write(sock, "From: %s\n", from);//would it ask for input from the user//
sendmail_write(sock, "To: %s\n", to);
sendmail_write(sock, "Subject: %s\n", subject);

sendmail_write(sock, "\n", NULL);

sendmail_write(sock, "%s\n", body); // data

sendmail_write(sock, ".\n", NULL); // end data
sendmail_write(sock, "QUIT\n", NULL); // terminate


return 0;

int main(int argc, char *argv[]) {

int ret = sendmail( //what is this/how would it work
"", /* from */
"", /* to */
"Subject", /* subject */
"body", /* body */
"hostname", /* hostname */
25 /* port */

if (ret != 0)
fprintf(stderr, "Failed to send mail (code: %i).\n", ret);
fprintf(stdout, "Mail successfully sent.\n");

return ret;
//also can someone refine it for linux platform so that i could run it with know i dont have to comment and uncomment it....just the command to compile it and it gets compiled
Old 01-02-2011, 11:02 AM   #2
LQ Newbie
Registered: Jan 2011
Posts: 1

Rep: Reputation: 0
i need smtp implementation code

hello ommy,

i need the code of SMTP implementation.
if u hv it..den plz mail me at
plz its urgent


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
SMTP "open relay" and SMTP AUTH aikempshall Linux - Security 3 10-11-2006 08:19 AM
Postfis SMTP forwarding and Incoming SMTP pheasand Linux - Software 0 04-27-2005 05:32 PM
NAT implementation sonika_singhi Linux - Networking 2 03-29-2005 05:02 AM
SMTP and KMail and SMTP and Netscape Mail jazz... Linux - Newbie 5 10-11-2003 03:49 AM
glibc implementation ? kaspar_ru Programming 4 07-25-2003 06:44 AM

All times are GMT -5. The time now is 04:33 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration