[C++] Smtp Console To Daemon

Dell / OPTIPLEX
April 14, 2009 at 15:27:14
Specs: Linux i686, 1.8ghz/ 512 mb
I came across a semi - understandable Source for
a Smtp Socket, only one peoblem, its a console, and I d rather have it be a daemon. I have the
source, and Ive got it 90% daemonized, but I have a problem, when it was a console I had to declare
a txt file to use as a message body, which is fine, the problem is: I cant find what to change
to declare where the the text file is as a daemon, I will post my modified source so that
you can see what I mean. I simply need to find where to imput the string "c:\\dcouments blah
blah\\mine\\ blah\\ message.txt"

#define WIN32_LEAN_AND_MEAN

#include <stdio.h>

#include <stdlib.h>

#include <fstream.h>

#include <iostream.h>

#include <windows.h>

#include <winsock2.h>

#pragma comment(lib, "ws2_32.lib")
const VERSION_MAJOR = 1;

const VERSION_MINOR = 1;

#define CRLF "\r\n"  
#define SMTPPORT ----
#define SMTPSERVER --- // put in ""
#define SMTPTO --- // put in ""
#define SMTPFROM --- // put in ""
#define SMTPMSGLINE --- // put in ""

// Basic Error Checking

void Check(int iStatus, char *szFunction) { if((iStatus != SOCKET_ERROR) &&  (iStatus)) return; cerr << "Error during call to " << szFunction << ": " << iStatus << " - " << GetLastError() << endl;

}


//Main Program can be filled in as required

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

{

  int         iProtocolPort        = SMTPPORT; 

  char        szSmtpServerName[64] = SMTPSERVER;

  char        szToAddr[64]         = SMTPTO;

  char        szFromAddr[64]       = SMTPFROM;

  char        szBuffer[4096]       = "1024";

  char        szLine[255]          = SMTPMSGLINE;

  char        szMsgLine[255]       = SMTPMSGLINE;

  SOCKET      hServer;

  WSADATA     WSData;

  LPHOSTENT   lpHostEntry;

  LPSERVENT   lpServEntry;

  SOCKADDR_IN SockAddr;



  //Initialise

lstrcpy(szSmtpServerName, argv[1]); lstrcpy(szToAddr, argv[2]); lstrcpy(szFromAddr, argv[3]); ifstream MsgFile(argv[4]); if(WSAStartup(MAKEWORD(VERSION_MAJOR, VERSION_MINOR), &WSData)) { dead = 1; }



  // Connect

  lpHostEntry = gethostbyname(szSmtpServerName); if(!lpHostEntry) { dead = 1 } hServer = socket(PF_INET, SOCK_STREAM, 0); if(hServer == INVALID_SOCKET{ dead = 1 } lpServEntry = getservbyname("mail", 0); if(!lpServEntry) iProtocolPort = htons(IPPORT_SMTP); else iProtocolPort = lpServEntry->s_port; SockAddr.sin_family = AF_INET; SockAddr.sin_port   = iProtocolPort; SockAddr.sin_addr   = *((LPIN_ADDR)*lpHostEntry->h_addr_list); if(connect(hServer, (PSOCKADDR) &SockAddr, sizeof(SockAddr))) { dead = 1; } Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() Reply"); sprintf(szMsgLine, "HELO %s%s", szSmtpServerName, CRLF); Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() HELO"); Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() HELO");



  // Send MAIL FROM: <sender@mydomain.com>

  sprintf(szMsgLine, "MAIL FROM:<%s>%s", szFromAddr, CRLF);

  Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() MAIL FROM");

  Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() MAIL FROM");



  // Send RCPT TO: <receiver@domain.com>

  sprintf(szMsgLine, "RCPT TO:<%s>%s", szToAddr, CRLF);

  Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() RCPT TO");

  Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() RCPT TO");



  // Send DATA

  sprintf(szMsgLine, "DATA%s", CRLF); Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() DATA"); Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() DATA"); MsgFile.getline(szLine, sizeof(szLine));  do  { sprintf(szMsgLine, "%s%s", szLine, CRLF); Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() message-line"); MsgFile.getline(szLine, sizeof(szLine)); } while(MsgFile.good());



  // Close server socket and exit.

sprintf(szMsgLine, "%s.%s", CRLF, CRLF); Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() end-message"); Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() end-message"); sprintf(szMsgLine, "QUIT%s", CRLF); Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() QUIT"); Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() QUIT"); closesocket(hServer); WSACleanup(); return 0;

}

I only Batch if possible, 2000 more lines of code, oh well.


See More: [C++] Smtp Console To Daemon

Report •


#1
April 15, 2009 at 06:12:00
Windows doesn't use daemons, it uses services. But why do you need a daemon/service to send an email?

Report •

#2
April 15, 2009 at 11:23:42
I want to do this to be able to automate me getting logs + sysinfo from my parents + grandparents since they call me alot with computer questions, its a bit easier to say " ok double click on that program i sent you" as opposed to " open up folder x, append file y to file z in folder a .... etc ... "

I only Batch if possible, 2000 more lines of code, oh well.


Report •

#3
April 16, 2009 at 22:44:42
That still sounds like an application, not a service.

And assuming your program doesn't take any command line switches, they still could double click its icon. You could even make a shortcut, if you wanted.


Report •

Related Solutions

#4
April 17, 2009 at 08:39:41
It does take command line switches, the Smtp part does.... that would be what im trying t o change -_-'

I only Batch if possible, 2000 more lines of code, oh well.


Report •


Ask Question