Friday, March 25, 2011

VC++ Simple Send Mail

See the GNU/C++ Simple Send Mail here.

I did the class below for Visual C++ based in these links:

SendMail.h:

#ifndef _SENDMAILHEADER
#define _SENDMAILHEADER

#include "stdafx.h"
#include <stdio.h>
#include <Winsock2.h>
#include <Windows.h>
#include <string>

#pragma comment(lib, "ws2_32.lib")

#define HELO "HELO\r\n"
#define DATA "DATA\r\n"
#define QUIT "QUIT\r\n"

namespace mail {
class SendMail {
private:
int sock;
char buf[BUFSIZ];
void send_socket(char *s)
{
send(sock, s, strlen(s), 0);
//fprintf(stderr, s);
}
void read_socket()
{
int len = recv(sock, buf, BUFSIZ, 0);
//fprintf(stderr, ((std::string)buf).substr(0, len).c_str());
}
public:
char* HostIp;
unsigned short HostPort;
char* To;
char* From;
char* Subject;
char* Message;
SendMail() {
HostPort = 25;
};
~SendMail() { };
void Send() {
struct sockaddr_in hostAddr;
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) {
fprintf(stderr, "SendMail: WSAStartup() failed");
return;
}
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
fprintf(stderr, "SendMail: socket() failed");
return;
}
memset(&hostAddr, 0, sizeof(hostAddr));
hostAddr.sin_family = AF_INET;
hostAddr.sin_addr.s_addr = inet_addr(HostIp);
hostAddr.sin_port = htons(HostPort);
if (connect(sock, (struct sockaddr *) &hostAddr, sizeof(hostAddr)) < 0) {
fprintf(stderr, "SendMail: connect() failed");
return;
}
read_socket();
send_socket(HELO);
read_socket();
send_socket("MAIL FROM: ");
send_socket(From);
send_socket("\r\n");
read_socket();
send_socket("VRFY ");
send_socket(From);
send_socket("\r\n");
read_socket();
send_socket("RCPT TO: ");
send_socket(To);
send_socket("\r\n");
read_socket();
send_socket(DATA);
send_socket("Subject: ");
send_socket(Subject);
send_socket("\r\n");
read_socket();
send_socket(Message);
send_socket("\r\n");
send_socket(".\r\n");
read_socket();
send_socket(QUIT);
read_socket();
closesocket(sock);
WSACleanup();
};
};
}

#endif


Usage:

#include "stdafx.h"
#include "SendMail.h"

int main(int argc, char* argv[])
{
mail::SendMail* sendmail = new mail::SendMail();
sendmail->HostIp = "192.168.1.10";
sendmail->From = "mail@address.com";
sendmail->To = "mail@address.com";
sendmail->Subject = "Test!";
sendmail->Message = "Content:\nTest 123...";
sendmail->Send();
return 0;
}

No comments:

Post a Comment

Post a Comment