Author Topic: How to send mail in NASM ??  (Read 10490 times)

Offline ngochuan1st

  • Jr. Member
  • *
  • Posts: 30
How to send mail in NASM ??
« on: August 28, 2013, 05:26:26 AM »
i want to send mail by nasm using Gmail account??

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How to send mail in NASM ??
« Reply #1 on: August 28, 2013, 07:07:38 AM »
Gunner gave you some pretty good hints to get started here:

http://forum.nasm.us/index.php?topic=1497.msg6133#msg6133

Socket, connect, read or recv. It'll probably say something like "+OK Gmail ready", but check. Then write or send your username. It'll be picky about the fact that it needs to end in both a carriage return and linefeed. Then read or recv again. Maybe just say "+OK" but might say something about more authentification needed. Write/send your password (CRLF again). Read/recv again - hopefully it'll say "+OK logged in"! (no use in continuing if you get an error at any point along the line!) Write/send the command to send mail - I imagine it's "post"(CRLF) but check those links Gunner gave you. Read/recv "+OK"? Write/send your mail. It'll need a certain minimal header - To: From: Date:, at least. End the header with a blank line (CRLFCRLF), then the content of your mail. End with a single '.' on a line by itself. Read/recv to make sure it's okay. Write/send the "quit" command. You really should close your socket, but if you just exit the OS will take care of that, I think.

Yeah... I know you want an example. I don't have an example of sending mail at hand. Would an example of getting a web page help you at all?

Best,
Frank


Offline ngochuan1st

  • Jr. Member
  • *
  • Posts: 30
Re: How to send mail in NASM ??
« Reply #2 on: August 28, 2013, 07:52:58 AM »
This code C++ but doesn't work:
Code: [Select]
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <io.h>
#pragma comment(lib,"ws2_32.lib")
#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

close(sock);//*******************************

return 0;
}


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

    int ret = sendmail("from@gmail.com", "to@gmail.com", "Subject", "body", "smtp.gmail.com", 25);

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