0

Fellow coders Ive contributed this code to show how to send an email using winsock but Im having problems working out what to do to get this to be SSL oriented and know that it requires something to do with Secure Sockets and SetSockOpt and GetSockOpt and WSAIOctl but Im stumped about setting the code Ive got to work with SECURE SOCKETS. Can anyone out there in DaniWeb Help make this to work with Secure Sockets just getting the Secure Socket Connection working would be fine as all the code for the SMTP is already done below:

Ideally getting this code to work with Secure Sockets will mean it connecting to PORT 465 on SSL SMTP Servers elsewhere which will require a logon and password and Ive no idea how this all fits in with the Secure Sockets Layer. I know im on the right track though.

#include <iostream.h>
#include <winsock2.h>
#include <windows.h>
//#include <string>
#include <stdio.h>
#include <string.h>

//using namespace std;




#define MAX_LENGTH 1024
#pragma argsused

char *fgets(char *line, int maxline, FILE *fp);
int fputs(char *line, FILE *fp);
int getline(char *line, int max);

void main()
{
int s_len, r_len;
int skt_Smtp;
int success;
struct sockaddr_in st_Sockaddr;
char recv_Buf[MAX_LENGTH];
char send_Buf[MAX_LENGTH];

char abuf[MAX_LENGTH] = "";;
char bbuf[MAX_LENGTH] = "";;
//using namespace std;
//string my_string;


char text[1024] = "";



//Initialize Sockets
WSADATA wsa;
WSAStartup(MAKEWORD(2, 0), &wsa);


skt_Smtp = socket(AF_INET,SOCK_STREAM,0);
if (skt_Smtp < 0)
{
cout<< "Error Creating Socket"<<endl;
return;
}
else
{
st_Sockaddr.sin_family = AF_INET;
st_Sockaddr.sin_port = htons(25);

//Get the IP address and initialize the structure
st_Sockaddr.sin_addr.s_addr = inet_addr("127.0.0.1");

success = connect(skt_Smtp,(struct sockaddr *) &st_Sockaddr,sizeof(st_Sockaddr));

r_len = recv(skt_Smtp,recv_Buf,MAX_LENGTH,0);
recv_Buf[r_len] = '\0';

cout<< recv_Buf<<endl;

//Say Hello to the domain
strcpy(send_Buf,"HELO jameshernon123@oxymoron.homeip.net\r\n");

s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);

r_len = recv(skt_Smtp,recv_Buf,MAX_LENGTH,0);
recv_Buf[r_len] = '\0';

cout<< recv_Buf<<endl;

//Send from address

strcpy(send_Buf , "MAIL FROM: <jahernon@gmail.com>\r\n");


s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);

r_len = recv(skt_Smtp,recv_Buf,MAX_LENGTH,0);
recv_Buf[r_len] = '\0';

//cout<< recv_Buf<<endl;

//Send RCPT address
//strcpy(send_Buf,"RCPT TO: <jameshernon231@btinternet.com>\r\n");
cout << "RCPT TO:";
//strcpy(text,"RCPT TO:");
strcat(bbuf,"RCPT TO:<");

getline(text,255);

text[strlen(text)-1] = 0; /* 'P' is not in `p` (and it isn't in `mystr` either) */

strcat(bbuf, text );

//cout << text;

strcat(bbuf,">");


strcpy(send_Buf,bbuf);

s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);

strcpy(send_Buf,"\r\n");

s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);


cout<< send_Buf<<endl;

r_len = recv(skt_Smtp,recv_Buf,MAX_LENGTH,0);

recv_Buf[r_len] = '\0';

//cout<< recv_Buf<<endl;

// Send DATA
strcpy(send_Buf,"DATA\r\n");

s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);

r_len = recv(skt_Smtp,recv_Buf,MAX_LENGTH,0);
recv_Buf[r_len] = '\0';

//cout<< recv_Buf<<endl;


// Send DATA

//strcpy(send_Buf,"Subject:new subject\r\n\r\n\r\n");
cout << "Enter Subject:";
strcpy(text,"Subject:");
getline(&text[8],255);
int t;
//t=sizeof(text);
t = strlen(text);
strcpy(&text[t],"\r\n\r\n\r\n");

//** printf("\nSubject:%s",text);

strcpy(send_Buf,text);

s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);

strcpy(send_Buf,"\r\n\r\n");

s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);

//Send body
cout << "Enter Message:";
getline(text,1024);
int q;
q=sizeof(text);
strcpy(&text[q],"\r\n.\r\n");

//** printf("\nText:%s",text);

strcpy(send_Buf, text );

s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);

strcpy(send_Buf,"\r\n.\r\n");

s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);

r_len = recv(skt_Smtp,recv_Buf,MAX_LENGTH,0);
recv_Buf[r_len] = '\0';

cout<< recv_Buf<<endl;

//Send QUIT
strcpy(send_Buf,"QUIT.\n");

s_len = send(skt_Smtp,send_Buf,strlen(send_Buf),0);

r_len = recv(skt_Smtp,recv_Buf,MAX_LENGTH,0);
recv_Buf[r_len] = '\0';

cout<< recv_Buf<<endl;

closesocket(skt_Smtp);

}

WSACleanup();

}



/* fgets: get at most n chars from iop */
char *fgets(char *s, int n, FILE *iop)
{
register int c;
register char *cs;

cs = s;
while (--n > 0 && (c = getc(iop)) != EOF)
if ((*cs++ = c) == '\n')
break;
*cs = '\0';
return (c == EOF && cs == s) ? NULL : s;
}

/* fputs: put string s on file iop */
int fputs(char *s, FILE *iop)
{
int c;

while (c = *s++)
putc(c, iop);
return ferror(iop) ? EOF : 0;
}
int getline(char *line, int max)
{
if (fgets(line, max, stdin) == NULL)
return 0;
else
return strlen(line);
}

Edited by technology: n/a

1
Contributor
1
Reply
3
Views
7 Years
Discussion Span
Last Post by technology
0

Ive found a link mentioning IPSEC and Winsock Secure Socket Extensions http://msdn.microsoft.com/en-us/library/bb394815(VS.85).aspx

Nust be too complex for me at the moment can anyone recommeend a good book with examples for windows secure sockets extensions. Any help greatly appreciated.

Edited by technology: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.