4 Replies - 33582 Views - Last Post: 10 October 2011 - 08:21 AM Rate Topic: -----

#1 Wuzseen   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 72
  • Joined: 04-October 11

Sending an SMTP Email

Posted 10 October 2011 - 07:45 AM

Howdy, I've got a quick question regarding sending an SMTP email.

http://stackoverflow.../c-smtp-example

In that link, how to do it is outlined--at this point I just want to get this code to work before tailoring it to my own needs. I'm having a couple issues though. I can't find several of the libraries in the code. Specifically sys/socket.h netinet/in.h and netdb.h

Also, does anyone know how I would go about adding attachments and sending an HTML email? I need to automate some reports and what not. Internally, we have an API that uses VB to send emails through a service we use, but for some of our tests that doesn't work. There's a couple tables and other formatting things that are available via an HTML email. We have a program that generates the code/HTML file, we just need to send it with SMTP or a protocol that supports it. We're using microsoft exchange but the exchange protocol (MAPI) doesn't support HTML email.

#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);
  }



Is This A Good Question/Topic? 0
  • +

Replies To: Sending an SMTP Email

#2 Karel-Lodewijk   User is offline

  • D.I.C Addict
  • member icon

Reputation: 455
  • View blog
  • Posts: 864
  • Joined: 17-March 11

Re: Sending an SMTP Email

Posted 10 October 2011 - 07:51 AM

sys/socket.h and others are posix headers. If you want to use them on windows, you need to use cygwin and the gcc.

You can also port that snippet program to winsock, they are not so different.
#include <winsock2.h> and some minor changes here and there.

This post has been edited by Karel-Lodewijk: 10 October 2011 - 07:54 AM

Was This Post Helpful? 0
  • +
  • -

#3 Wuzseen   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 72
  • Joined: 04-October 11

Re: Sending an SMTP Email

Posted 10 October 2011 - 07:52 AM

Alllright, I've got cygwin installed, what do?
Was This Post Helpful? 0
  • +
  • -

#4 Karel-Lodewijk   User is offline

  • D.I.C Addict
  • member icon

Reputation: 455
  • View blog
  • Posts: 864
  • Joined: 17-March 11

Re: Sending an SMTP Email

Posted 10 October 2011 - 07:59 AM

I would recommend option 2.

Going the cygwin route means you can probably no longer use visual studio. Besides applications that link against cygwin are not quite native and generally larger and less efficient.

There are also cross platform network libraries. For your particular purpose curl comes to mind or even something more specifically geared towards smtp.

This post has been edited by Karel-Lodewijk: 10 October 2011 - 08:02 AM

Was This Post Helpful? 0
  • +
  • -

#5 Wuzseen   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 72
  • Joined: 04-October 11

Re: Sending an SMTP Email

Posted 10 October 2011 - 08:21 AM

Thanks, Karel--I've been trying to do this for about a week now (not constantly mind you) and now it's at the top of my list of things to do as opposed to the bottom.

I could really use some help with installing curl/libcurl here on my Windows machine--I've attempted it in the past but have had issues; it seems that there is not clear cut tutorial anywhere on how to do it. I've downloaded the binary package from the following link:

http://curl.haxx.se/download.html

I grabbed the files for Win32 - Generic, the developer ones specifically. Right now the files are just sitting on my desktop. I understand how I would access the curl command from console, but I'm at a loss as to how I would integrate these headers and what not into C++--I presume there's more to it than simply dropping the headers into your project folder.

Thanks.

This post has been edited by Wuzseen: 10 October 2011 - 08:27 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1