tags:

views:

8901

answers:

11
+9  Q: 

C++ SMTP Example

Does any one have a simple C++ example for sending e-mail using smtp api?

+1  A: 

There is no SMTP API but you can take a look at this CFastSMTP class on codeproject

Brian R. Bondy
A: 

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);
hoyhoy
This approach must not be used if the user can specify the message body. Otherwise, the user can arrange for any command to be run on the system.
Chris Jester-Young
What are you talking about? A Linux user can always run any command on the system from if they have +x permission on that file. If this were exposed to to the Web via a an apache module, you'd be out of your mind to start with for not just using PHP.
hoyhoy
+1  A: 

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.

http://www.naughter.com/smtp.html

Brad Bruce
A: 

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 :-)

David Citron
RFC 2821 has been obsoleted by RFC 5321
Alnitak
Fixed the link. So, though 5321 is a "DRAFT STANDARD," we should assume it will be approved?
David Citron
+1  A: 

If you use ATL on Windows, have a look at the source-code folder for mfcatl in a recent Visual Studio, there is a SMTP-implementation!

A: 

If system() is an option then a cross platform method would be to use sendmail.pl

David Sykes
A: 

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.

jilles de wit
+1  A: 

Check out the POCO C++ Libraries. There is a SMTPClientSession class in the Net library with allows you to send mail messages.

+7  A: 
 #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);

}

Besides the bad formatting, (probably an issue with the HTML parsing in this forum), this is exactly what he asked for.. :) Good work! Right to the point, however, I wouldn't use it in production.
LarryF
A: 

ssmtp is probably a good example of how to use the SMTP protocol. Only that it is in C, not C++.

PolyThinker
+3  A: 

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...

LarryF
Thanks LarryF. I always appreciate understanding whats going on under the hood as well.
simon
I totally agree. It's always good to know what's going on under the hood. To me, that makes the difference in a programmer and a professional developer. Anyone can follow instructions, but to understand WHAT you are doing at the low level is important.
LarryF