Problem in SMTP

Hey guis, i found this code on internet and it is crashing. Some one know why?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <stdio.h>
#include <io.h>

#include <stdio.h>

#define WIN32 /* define this if you are on windows */
#include "io.h"
#include "winsock2.h"      /* WSAGetLastError, WSAStartUp  */
#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;


//CRASHING HERE!
//CRASHING HERE!
//CRASHING HERE!
//CRASHING HERE!
//CRASHING HERE!

	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",       from);    // 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@from-host.org",   /* from     */
        "extremez3r0@gmail.com",       /* to       */
        "Subject",              /* subject  */
        "body",                 /* body     */
        "hostname",             /* hostname */
        25                      /* port     */
    );

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

    return ret;
}


I try fix but i can't.

help?
As you have wriiten it - it is going to fail.
You have not given a valid hostname to the sendmail function - so gethostbyname(hostname) function will
return a null pointer.
You then proceed to try to copydata from the NULL pointer which will cause a CRASH
¬¬ ... lol. I'm an idiot hehehe

Thanks Thanks...

I fix it, i change hotsname by smtp.google.com

The program execute with success, but the email don't show in my inBox...

THe hostname is the smtp server by I understand, correct?



Sorry for the bad english =/
You have syntax errors.
You haven't got any error checking in your code as you are not checking the recturn codes from the server so you cannot see the errors.

The text for the HELO statement cannot contain certain characters like @.
So you cannot say HELO myname@somewhere.com.

try something like:
sendmail_write(sock, "HELO %s\n", "gabrielsaraiva" ); // greeting
This very old library has an SMTP send function that deals with attachments and so on.
http://legacy.imatix.com/html/sfl/
Topic archived. No new replies allowed.