Does any one have a simple C++ example for sending e-mail using smtp api?
There is no SMTP API but you can take a look at this CFastSMTP class on codeproject
You can use the system() method to invoke sendmail on the host (assuming *nix).
char sendmail_command[1024];
char message[] = "This is the message body.";
snprintf(sendmail_command, 1024, "echo '%s' | mail -s 'title' [email protected]", message);
system(sendmail_command);
This library isn't pure C++ (it uses MFC) but this guy writes some really good libraries that I have extracted huge chunks of functionality from. Even if you don't use the library in its entirety, it would be worth looking at the source.
I enjoyed reading through the implementation that PHP uses to get a feel for how to write it. Of course, the RFC is required reading as well :-)
If system() is an option then a cross platform method would be to use sendmail.pl
The .zip linked on this codeguru forum page contains a simple command line (!) smtp client example that uses sockets to send simple e-mail messages. It contained all I needed to get started.
Check out the POCO C++ Libraries. There is a SMTPClientSession class in the Net library with allows you to send mail messages.
#include<iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
using namespace std;
#define HELO "HELO 192.168.1.1\r\n"
#define DATA "DATA\r\n"
#define QUIT "QUIT\r\n"
//#define h_addr h_addr_list[0]
//FILE *fin;
int sock;
struct sockaddr_in server;
struct hostent *hp, *gethostbyname();
char buf[BUFSIZ+1];
int len;
char *host_id="192.168.1.10";
char *from_id="[email protected]";
char *to_id="[email protected]";
char *sub="testmail\r\n";
char wkstr[100]="hello how r u\r\n";
/*=====Send a string to the socket=====*/
void send_socket(char *s)
{
write(sock,s,strlen(s));
write(1,s,strlen(s));
//printf("Client:%s\n",s);
}
//=====Read a string from the socket=====*/
void read_socket()
{
len = read(sock,buf,BUFSIZ);
write(1,buf,len);
//printf("Server:%s\n",buf);
}
/*=====MAIN=====*/
int main(int argc, char* argv[])
{
/*=====Create Socket=====*/
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock==-1)
{
perror("opening stream socket");
exit(1);
}
else
cout << "socket created\n";
/*=====Verify host=====*/
server.sin_family = AF_INET;
hp = gethostbyname(host_id);
if (hp==(struct hostent *) 0)
{
fprintf(stderr, "%s: unknown host\n", host_id);
exit(2);
}
/*=====Connect to port 25 on remote host=====*/
memcpy((char *) &server.sin_addr, (char *) hp->h_addr, hp->h_length);
server.sin_port=htons(25); /* SMTP PORT */
if (connect(sock, (struct sockaddr *) &server, sizeof server)==-1)
{
perror("connecting stream socket");
exit(1);
}
else
cout << "Connected\n";
/*=====Write some data then read some =====*/
read_socket(); /* SMTP Server logon string */
send_socket(HELO); /* introduce ourselves */
read_socket(); /*Read reply */
send_socket("MAIL FROM: ");
send_socket(from_id);
send_socket("\r\n");
read_socket(); /* Sender OK */
send_socket("VRFY ");
send_socket(from_id);
send_socket("\r\n");
read_socket(); // Sender OK */
send_socket("RCPT TO: "); /*Mail to*/
send_socket(to_id);
send_socket("\r\n");
read_socket(); // Recipient OK*/
send_socket(DATA);// body to follow*/
send_socket("Subject: ");
send_socket(sub);
read_socket(); // Recipient OK*/
send_socket(wkstr);
send_socket(".\r\n");
read_socket();
send_socket(QUIT); /* quit */
read_socket(); // log off */
//=====Close socket and finish=====*/
close(sock);
exit(0);
}
ssmtp is probably a good example of how to use the SMTP protocol. Only that it is in C, not C++.
In all honesty, I'd start with the SMTP RFC (821), and work from there.
It's all just a socket connection... The most basic commands are:
HELO
MAIL FROM:
RCPT TO:
DATA
QUIT
Pretty much in that order... Just look for the response codes to make sure things are ok. In short, they are:
220 - Hello request
221 - Goodbye
250 - Ok
354 - Send your message
501 - Syntax error
55x - Failure
Here's what a simple transaction looks like using the telnet application to port 25:
220 mlinks.net ESMTP Postfix
helo larryf.mlinks.net
250 mlinks.net
mail from:[email protected]
250 2.1.0 Ok
rcpt to:[email protected]
250 2.1.5 Ok
data
354 End data with <CR><LF>.<CR><LF>
This is the message body.
.
250 2.0.0 Ok: queued as C2BEC5B0143
quit
221 2.0.0 Bye
Hope this helps, but it looks like you already got a lot of help on the subject...