0

Hello all I have been working on a small project and have been researching how to send emails via SMTP in C++. I found a nice looking tutorial and some code from cppforums but I am presented with 8 "LNK2019 unresolved external symbol errors" Here is the code, most of it is from forums. What am I doing wrong? I have read at least 5-6 other questions posted about this type of error but all of them correlate to certain problems within their code so therefore I shall post mine. Thank you for any help.

#include <stdio.h>
#include <io.h>
#include <winsock2.h>  
#define snprintf _snprintf


static void sendmail_write(
    const int  sock,
    const char *str,
    const char *arg
    ) {
    char buf[4096];

    if (arg != NULL)
        snprintf(buf, sizeof(buf), str, arg);
    else
        snprintf(buf, sizeof(buf), str);

    send(sock, buf, strlen(buf), 0);
}

static int sendmail(
    const char *from,
    const char *to,
    const char *subject,
    const char *body,
    const char *hostname,
    const int   port
    ) {

    struct hostent *host;
    struct sockaddr_in saddr_in;
    int sock = 0;


    WSADATA wsaData;
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
        return -1;
    }


    sock = socket(AF_INET, SOCK_STREAM, 0);
    host = gethostbyname(hostname);

    saddr_in.sin_family = AF_INET;
    saddr_in.sin_port = htons((u_short)port);
    saddr_in.sin_addr.s_addr = 0;

    memcpy((char*)&(saddr_in.sin_addr), host->h_addr, host->h_length);

    if (connect(sock, (struct sockaddr*)&saddr_in, sizeof(saddr_in)) == -1) {
        return -2;
    }



    sendmail_write(sock, "HELO %s\n", "Testname");    // greeting****************

    sendmail_write(sock, "MAIL FROM: %s\n", from);    // from

    sendmail_write(sock, "RCPT TO: %s\n", to);      // to    

    sendmail_write(sock, "DATA\n", NULL);    // begin data

    // next comes mail headers
    sendmail_write(sock, "From: %s\n", from);


    sendmail_write(sock, "To: %s\n", to);


    sendmail_write(sock, "Subject: %s\n", subject);

    sendmail_write(sock, "\n", NULL);

    sendmail_write(sock, "%s\n", body);    // data

    sendmail_write(sock, ".\n", NULL);    // end data
    sendmail_write(sock, "QUIT\n", NULL);    // terminate

    closesocket(sock);//*******************************

    return 0;
}


int main(int argc, char *argv[]) {

    int ret = sendmail(
        "support@mydomain.com",  // from - put an email address here
        "atomail", // to - put an email address here
        "test",
        "this would be cool if it worked!",
        "iputmyisphere",//this is my ISP -use your own
        25
        );

    if (ret != 0)
        fprintf(stderr, "Failed to send mail (code: %i).\n", ret);
    else
        fprintf(stdout, "Mail successfully sent.\n");

    return ret;
}
lave
  • 1
  • could you please write what the unresolved external error says? – J-Mik Nov 03 '15 at 21:38
  • If this is C++, please, **please** do not use fixed-sized buffers. `std::string` and streams are significantly safer. This dangerously presumes 4096 bytes is enough for anything you're dealing with, and considering this is email, I'm pretty sure that's nowhere near adequate. – tadman Nov 03 '15 at 21:38
  • Sure I will post an imgur link of the errors. http://i.imgur.com/6lHxuu9.png – lave Nov 03 '15 at 21:40
  • @lave _"Sure I will post an imgur link of the errors. "_ No you don't. Post the verbatim error message text in your question. – πάντα ῥεῖ Nov 03 '15 at 21:41

0 Answers0