Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
2678 connectés 

  FORUM HardWare.fr
  Programmation
  C

  envoie d'un fichier par email -> manque de librairies ?????

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

envoie d'un fichier par email -> manque de librairies ?????

n°1051900
roromaino
Posté le 19-04-2005 à 10:51:37  profilanswer
 

bonjour,
 
J'ai recuperer sur le net un exemple d'envoie d'un fichier par email.
Lorsque je compile le programme avec borland 5.0, des erreurs se produisent dû au manque de certaines librairies.
 
voici le programme:

Code :
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <netdb.h>
  6. #include <arpa/inet.h>
  7. #include <netinet/in.h>
  8. #include <sys/socket.h>
  9. #define DEBUG 0 /* Attention il y des pb si le msg envoye contient des % */
  10. #define PACKET_SIZE  1024
  11. void appli(char *f);
  12. int writen(int fd, char *ptr, int n);
  13. int readn(int fd, char *ptr, int n);
  14. int to_server_socket = -1;
  15. char server_name[] = "SERVEUR SMTP";  /* nom du host du serveur */
  16. int port = 25;
  17. char helo[] = "helo plus.bas\n";
  18. char from[] = "mail from: <nic@salemioche.com>\n";
  19. char to[] = "rcpt to: <ADDR DEST>\n";
  20. char subject[] = "Subject: transfert ";
  21. int main (int argc, char *argv[])
  22. {
  23.     struct sockaddr_in serverSockAddr;    /* addresse de la socket */
  24.     struct hostent *serverHostEnt;        /* description du host serveur */
  25.     unsigned long hostAddr;                       /* addr du serveur */
  26.     char *filename;
  27.     if ( argc == 2 ) filename = argv[1];
  28.     else filename = NULL; /* use stdin */
  29.     bzero(&serverSockAddr,sizeof(serverSockAddr));
  30.     /* converti l'adresse ip 9.100.1.1 en entier long */
  31.     hostAddr = inet_addr(server_name);
  32.     if ( (long)hostAddr != (long)-1)
  33.             bcopy(&hostAddr,&serverSockAddr.sin_addr,sizeof(hostAddr));
  34.     else                                /* si on a donne un nom  */
  35.     {
  36.             serverHostEnt = gethostbyname(server_name);
  37.             if (serverHostEnt == NULL)
  38.             {
  39.                     printf("probleme gethost\n" );
  40.                     exit(0);
  41.             }
  42.             bcopy(serverHostEnt->h_addr,&serverSockAddr.sin_addr,
  43.                   serverHostEnt->h_length);
  44.     }
  45.     serverSockAddr.sin_port = htons(port);   /* host to network port  */
  46.     serverSockAddr.sin_family = AF_INET;     /* AF_*** : INET=internet */
  47.     /* creation de la socket */
  48.     if ( (to_server_socket = socket(AF_INET,SOCK_STREAM,0)) < 0)
  49.     {
  50.             printf("probleme creation socket client\n" );
  51.             exit(0);
  52.     }
  53.     /* requete de connexion */
  54.     if(connect(to_server_socket,(struct sockaddr *)&serverSockAddr,
  55.                sizeof(serverSockAddr))<0)
  56.     {
  57.             printf("probleme demande de connection\n" );
  58.             exit(0);
  59.     }
  60.     appli(filename);
  61.     /* fermeture de la connection */
  62.     shutdown(to_server_socket,2);
  63.     close(to_server_socket);
  64.     return 0;
  65. }
  66. void appli (char *filename) {
  67. char buf[PACKET_SIZE+1], *ptr;
  68. FILE *bulk;
  69. int nb;
  70.         if (filename == NULL) {
  71.           bulk = stdin;
  72.         } else {
  73.             if ( ( bulk = fopen(filename,"rb" )) == NULL ) {
  74.                     printf ( "can't open file to read\n" );
  75.                     exit(1);
  76.             }
  77.         }
  78.         buf[0] = 0x00;
  79.         readn(to_server_socket,buf,PACKET_SIZE);
  80.         printf(buf);
  81.         writen(to_server_socket,helo,strlen(helo));
  82.         readn(to_server_socket,buf,PACKET_SIZE);
  83.         printf(buf);
  84.         writen(to_server_socket,from,strlen(from));
  85.         readn(to_server_socket,buf,PACKET_SIZE);
  86.         printf(buf);
  87.         writen(to_server_socket,to,strlen(to));
  88.         readn(to_server_socket,buf,PACKET_SIZE);
  89.         printf(buf);
  90.         writen(to_server_socket,"data\n",6);
  91.         readn(to_server_socket,buf,PACKET_SIZE);
  92.         printf(buf);
  93.         sprintf(buf,"%s %s\n\n", subject,
  94.                               (( filename == NULL ) ? "STDIN" : filename ) );
  95.         writen(to_server_socket,buf,strlen(buf));
  96. /* the file */
  97.         while ( !feof(bulk) ) {
  98.           bzero(buf, PACKET_SIZE+1);
  99.           nb = fread(buf,sizeof(char), PACKET_SIZE, bulk);
  100.           buf[nb] = 0x00;
  101.           while ( (ptr = strstr(buf,"\r\n.\r\n" ) ) != NULL ) *(ptr+3) = '.';
  102.           writen(to_server_socket,buf,nb);
  103.           if (DEBUG) printf(buf);
  104.         }
  105.         writen(to_server_socket,"\r\n.\r\n",5);
  106.         readn(to_server_socket,buf,PACKET_SIZE);
  107.         printf(buf);
  108.         fclose(bulk);
  109. }
  110. int writen(int fd, char *ptr, int n)
  111. {
  112. int nl, nw;
  113.         nl = n;
  114.         while ( nl > 0 ) {
  115.                 nw = write(fd, ptr, nl);
  116.                 if ( nw <= 0 )
  117.                         return nw;     /*error*/
  118.                 nl -= nw;
  119.                 ptr += nw;
  120.         }
  121.         return (n-nl);
  122. }
  123. int readn(int fd, char *ptr, int n){
  124. int nl, nr;
  125.         nl = n;
  126.         while ( nl > 0 ) {
  127.                 nr = read(fd,ptr,nl);
  128.                 if (nr < 0 )
  129.                         return nr;     /*error*/
  130.                 else
  131.                         if ( nr == 0 )
  132.                                 break;
  133.                 nl -= nr;
  134.                 ptr += nr;
  135.                 if ( *(ptr-2) == '\r' &&  *(ptr-1) == '\n' )
  136.                   break;
  137.         }
  138.         *ptr = 0x00;
  139.         return (n-nl);
  140. }


 
bref voici les librairies qui me manques :

Code :
  1. #include <unistd.h>
  2. #include <netdb.h>
  3. #include <arpa/inet.h>
  4. #include <netinet/in.h>
  5. #include <sys/socket.h>


 
où puis-je telecharger ces librairies, j'ai cherché sur le net, sans succes.
 
merci d'avance de vos reponses.
 
 :bounce:  :bounce:  :hello:  :bounce:  :bounce:  

mood
Publicité
Posté le 19-04-2005 à 10:51:37  profilanswer
 

n°1052255
matafan
Posté le 19-04-2005 à 14:41:06  profilanswer
 

C'est fait pour tourner sous unix/linux.

n°1052281
Tamahome
⭐⭐⭐⭐⭐
Posté le 19-04-2005 à 14:50:49  profilanswer
 

+1 ca utilise des lib minux... tu les auras ptet avec des compilo style mingw ou cygnus (ca fait longtemps alors je me souviens plus trop des noms...)

n°1052306
roromaino
Posté le 19-04-2005 à 15:04:39  profilanswer
 

ha zout j'avais po vu!!!!!!!!
 
bon j'ai plus qu'a trouvé autre chose LOL

n°1052508
niemad
Posté le 19-04-2005 à 16:49:32  profilanswer
 

roromaino a écrit :

ha zout j'avais po vu!!!!!!!!
 
bon j'ai plus qu'a trouvé autre chose LOL


 
Sinon tu peux installer Cygwin sur ton ordi. Ca marche tres bien. Ca te permet de faire tourner des programmes linux sous Windows (sous reserve que tu as les fichiers sources et que tu les recompile sous cygwin). Pour plus d info cherche Cygwin sous google.

n°1052528
roromaino
Posté le 19-04-2005 à 16:57:43  profilanswer
 

ok merci
 
mais j'ai besoin d'integrer une gestion de mail automatique a un programme qui doit tourner sous windows.
 
donc là je l'ais dans l'os.
 
je vais chercher autre chose. :jap:  
 

n°1052787
Emmanuel D​elahaye
C is a sharp tool
Posté le 19-04-2005 à 20:14:35  profilanswer
 

roromaino a écrit :

mais j'ai besoin d'integrer une gestion de mail automatique a un programme qui doit tourner sous windows.


Encore un générateur de spam... Si tu y tiens, Windows a des fonctions pour invoquer le mailer courant si il est installé...
 
Voir sur le forum consacré à Windows...


Message édité par Emmanuel Delahaye le 19-04-2005 à 20:15:29

---------------
Des infos sur la programmation et le langage C: http://www.bien-programmer.fr Pas de Wi-Fi à la maison : http://www.cpl-france.org/

Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C

  envoie d'un fichier par email -> manque de librairies ?????

 

Sujets relatifs
Générer un Fichier à partir d'un formulaire JSPeffacer une ligne dans un fichier texte
Modifier un fichier "rc"comment faire un import d'un fichier passé en paramètre??
VBS - ouverture fichier excel si existeune fonction PHP incroyable. ecrire un fichier avec une var. et zipper
Lecture d'un fichier en mode binaire[HTML] Création d'email
comparer 2 fichier excelprotéger un fichier compressé
Plus de sujets relatifs à : envoie d'un fichier par email -> manque de librairies ?????


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR