2
/*24age*/
well i got this from net itself though it is not working....pls try it
yourself..and tell me y the error message....
pls...include the email address....which i have left .....
vijesh
program
************************************************************
#include <stdio.h>
#include <io.h>
int rc;
char buf[256];
//#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
#endif
#ifdef LINUX
# include <netdb.h> /* gethostbyname */
# include <netinet/in.h> /* htons */
# include <sys/socket.h>
#endif
#pragma comment(lib, "wsock32.lib")
static void sendmail_write(const int sock,const char *str,const char
*arg, bool reply)
{
char buf[4096];
if (arg != NULL)
snprintf(buf, sizeof(buf), str, arg);
else
snprintf(buf, sizeof(buf), str);
send(sock, buf, strlen(buf), 0);
if(reply)
{
if((rc = recv(sock, buf, sizeof(buf) - 1, 0)) == -1)
{
perror("recv");
printf("%s", "ERROR");
}
else
{
buf[rc] = 0;
printf("%s", buf);
}
}
}
static int sendmail(const char *from,const char *to,const char
*subject,const char *body, const char *hostname,const int port)
{
struct hostent *host;
struct sockaddr_in saddr_in;
int sock = 0;
#ifdef WIN32
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
return -1;
}
#endif
sock = socket(AF_INET, SOCK_STREAM, 0);
host = gethostbyname(hostname);
saddr_in.sin_family = AF_INET;
saddr_in.sin_port = htons((u_short)port);
saddr_in.sin_addr.s_addr = 0;
memcpy((char*)&(saddr_in.sin_addr), host->h_addr, host->h_length);
if(connect(sock, (struct sockaddr*)&saddr_in, sizeof(saddr_in)) == -1)
{
return -2;
}
char buf[4096];
//read out server welcome message
if((rc = recv(sock, buf, sizeof(buf) - 1, 0)) == -1)
{
perror("recv");
printf("%s", "ERROR");
}
else
{
buf[rc] = 0;
printf("%s", buf);
}
sendmail_write(sock, "helo %s\r\n", from, true); // greeting
sendmail_write(sock, "mail from: %s\r\n", from, true); // from
sendmail_write(sock, "rcpt to: %s\r\n", to, true); // to
sendmail_write(sock, "data\r\n", NULL, true); // begin data
//printf("%s", "\nWe are after quit command");
// next comes mail headers
sendmail_write(sock, "From: %s\r\n", from, false);
sendmail_write(sock, "To: %s\r\n", to, false);
sendmail_write(sock, "Subject: %s\r\n", subject, false);
sendmail_write(sock, "Date: 6/6/6\r\n", NULL, false);
sendmail_write(sock, "\r\n", NULL, false);
sendmail_write(sock, "%s\r\n", body, false); // data
sendmail_write(sock, ".\r\n", NULL, true); // end data
sendmail_write(sock, "QUIT", NULL, false); // terminate
//printf("%s", "\nWe are after quit command"); //never even gets to
this line
#ifdef WIN32
closesocket(sock);
#else
close(sock);
#endif
return 0;
}
int main(int argc, char *argv[]) {
int ret = sendmail(
"(e-mail address removed)", /* from */
"(e-mail address removed)", /* to */
"Subject", /* subject */
"body", /* body */
"vij", /* hostname */
25 /* port */
);
if (ret != 0)
fprintf(stderr, "Failed to send mail (code: %i).\n", ret);
else
fprintf(stdout, "Mail successfully sent.\n");
return ret;
}
yourself..and tell me y the error message....
pls...include the email address....which i have left .....
vijesh
program
************************************************************
#include <stdio.h>
#include <io.h>
int rc;
char buf[256];
//#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
#endif
#ifdef LINUX
# include <netdb.h> /* gethostbyname */
# include <netinet/in.h> /* htons */
# include <sys/socket.h>
#endif
#pragma comment(lib, "wsock32.lib")
static void sendmail_write(const int sock,const char *str,const char
*arg, bool reply)
{
char buf[4096];
if (arg != NULL)
snprintf(buf, sizeof(buf), str, arg);
else
snprintf(buf, sizeof(buf), str);
send(sock, buf, strlen(buf), 0);
if(reply)
{
if((rc = recv(sock, buf, sizeof(buf) - 1, 0)) == -1)
{
perror("recv");
printf("%s", "ERROR");
}
else
{
buf[rc] = 0;
printf("%s", buf);
}
}
}
static int sendmail(const char *from,const char *to,const char
*subject,const char *body, const char *hostname,const int port)
{
struct hostent *host;
struct sockaddr_in saddr_in;
int sock = 0;
#ifdef WIN32
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
return -1;
}
#endif
sock = socket(AF_INET, SOCK_STREAM, 0);
host = gethostbyname(hostname);
saddr_in.sin_family = AF_INET;
saddr_in.sin_port = htons((u_short)port);
saddr_in.sin_addr.s_addr = 0;
memcpy((char*)&(saddr_in.sin_addr), host->h_addr, host->h_length);
if(connect(sock, (struct sockaddr*)&saddr_in, sizeof(saddr_in)) == -1)
{
return -2;
}
char buf[4096];
//read out server welcome message
if((rc = recv(sock, buf, sizeof(buf) - 1, 0)) == -1)
{
perror("recv");
printf("%s", "ERROR");
}
else
{
buf[rc] = 0;
printf("%s", buf);
}
sendmail_write(sock, "helo %s\r\n", from, true); // greeting
sendmail_write(sock, "mail from: %s\r\n", from, true); // from
sendmail_write(sock, "rcpt to: %s\r\n", to, true); // to
sendmail_write(sock, "data\r\n", NULL, true); // begin data
//printf("%s", "\nWe are after quit command");
// next comes mail headers
sendmail_write(sock, "From: %s\r\n", from, false);
sendmail_write(sock, "To: %s\r\n", to, false);
sendmail_write(sock, "Subject: %s\r\n", subject, false);
sendmail_write(sock, "Date: 6/6/6\r\n", NULL, false);
sendmail_write(sock, "\r\n", NULL, false);
sendmail_write(sock, "%s\r\n", body, false); // data
sendmail_write(sock, ".\r\n", NULL, true); // end data
sendmail_write(sock, "QUIT", NULL, false); // terminate
//printf("%s", "\nWe are after quit command"); //never even gets to
this line
#ifdef WIN32
closesocket(sock);
#else
close(sock);
#endif
return 0;
}
int main(int argc, char *argv[]) {
int ret = sendmail(
"(e-mail address removed)", /* from */
"(e-mail address removed)", /* to */
"Subject", /* subject */
"body", /* body */
"vij", /* hostname */
25 /* port */
);
if (ret != 0)
fprintf(stderr, "Failed to send mail (code: %i).\n", ret);
else
fprintf(stdout, "Mail successfully sent.\n");
return ret;
}