Bonsoir,
dans le cadre d'un cours je dois faire un client smtp capable d'envoyer un mail. Je me suis inspiré de beaucoup d'exemples sur le net, mais mon code refuse de marcher.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
109
110
111
112
113
114
115
 
#include <stdio.h>
 
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
 
 
static void sendmail_write(const int  sock,const char *str,const char *arg)
{
    char buf[500];
 
    if (arg != NULL)
        snprintf(buf, sizeof(buf), str, arg);
    else
        snprintf(buf, sizeof(buf), str);
 
    printf("%s \n",buf);
 
    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;
 
 
    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",       from);
    sendmail_write(sock, "MAIL FROM: %s\n", from);
    sendmail_write(sock, "RCPT TO: %s\n",   to);
    sendmail_write(sock, "DATA\n",          NULL);
 
    sendmail_write(sock, "Subject: %s\n",   subject);
    sendmail_write(sock, "From: %s\n",      from);
    sendmail_write(sock, "To: %s\n",        to);
 
 
    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[])
{
    char texte[100];
    char texte1[100];
    char texte2[200];
    char texte3[1024];
    char texte4[100];
 
    //from
    printf("votre addresse: \n");
    scanf("%s",texte);
    char * from = texte;
 
    //destinataire
    printf("destinataire: \n");
    scanf("%s",texte1);
    char * to = texte1;
 
    //sujet
    printf("sujet: \n");
    scanf("%s",texte2);
    char * sujet = texte2;
 
    //corps du texte
    printf("texte: \n");
    scanf("%s",texte3);
    char * data = texte3;
 
    //hostname
    printf("hostname: \n");
    scanf("%s",texte4);
    char * hN = texte4;
 
    int ret = sendmail(
                  from,   /* from     */
                  to,       /* to       */
                  sujet,              /* sujet  */
                  data,                 /* data    */
                  hN,             /* hostname */
                  25                      /* port     */
              );
 
    if (ret != 0)
        fprintf(stderr, "erreur d'envoi du mail (code: %i).\n", ret);
    else
        fprintf(stdout, "mail envoyé avec succès à %s.\n", to);
 
    return ret;
}
le programme m'indique que le mail est envoyé, mais je ne recois rien.
merci d'avance de votre aide.