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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define DEBUG 0 /* Attention il y des pb si le msg envoye contient des % */
#define PACKET_SIZE 1024
void appli(char *f);
int writen(int fd, char *ptr, int n);
int readn(int fd, char *ptr, int n);
int to_server_socket = -1;
char server_name[] = "smtp.gmail.com"; /* nom du host du serveur */
int port = 25;
char helo[] = "helo plus.bas\n";
char from[] = "mail from: <fatma.sarhan@gmail.com>\n";
char to[] = "rcpt to: <nejiba.taher@gmail.com>\n";
char subject[] = "Subject: transfert ";
int main (int argc, char *argv[])
{
struct sockaddr_in serverSockAddr; /* addresse de la socket */
struct hostent *serverHostEnt; /* description du host serveur */
unsigned long hostAddr; /* addr du serveur */
char *filename;
if ( argc == 2 ) filename = argv[1];
else filename = NULL; /* use stdin */
bzero(&serverSockAddr,sizeof(serverSockAddr));
/* converti l'adresse ip 9.100.1.1 en entier long */
hostAddr = inet_addr(server_name);
if ( (long)hostAddr != (long)-1)
bcopy(&hostAddr,&serverSockAddr.sin_addr,sizeof(hostAddr));
else /* si on a donne un nom */
{
serverHostEnt = gethostbyname(server_name);
if (serverHostEnt == NULL)
{
printf("probleme gethost\n");
exit(0);
}
bcopy(serverHostEnt->h_addr,&serverSockAddr.sin_addr,
serverHostEnt->h_length);
}
serverSockAddr.sin_port = htons(port); /* host to network port */
serverSockAddr.sin_family = AF_INET; /* AF_*** : INET=internet */
/* creation de la socket */
if ( (to_server_socket = socket(AF_INET,SOCK_STREAM,0)) < 0)
{
printf("probleme creation socket client\n");
exit(0);
}
/* requete de connexion */
if(connect(to_server_socket,(struct sockaddr *)&serverSockAddr,
sizeof(serverSockAddr))<0)
{
printf("probleme demande de connection\n");
exit(0);
}
appli(filename);
/* fermeture de la connection */
shutdown(to_server_socket,2);
close(to_server_socket);
return 0;
}
void appli (char *filename) {
char buf[PACKET_SIZE+1], *ptr;
FILE *bulk;
int nb;
if (filename == NULL) {
bulk = stdin;
} else {
if ( ( bulk = fopen(filename,"rb")) == NULL ) {
printf ( "can't open file to read\n" );
exit(1);
}
}
buf[0] = 0x00;
readn(to_server_socket,buf,PACKET_SIZE);
printf(buf);
writen(to_server_socket,helo,strlen(helo));
readn(to_server_socket,buf,PACKET_SIZE);
printf(buf);
writen(to_server_socket,from,strlen(from));
readn(to_server_socket,buf,PACKET_SIZE);
printf(buf);
writen(to_server_socket,to,strlen(to));
readn(to_server_socket,buf,PACKET_SIZE);
printf(buf);
writen(to_server_socket,"data\n",6);
readn(to_server_socket,buf,PACKET_SIZE);
printf(buf);
sprintf(buf,"%s %s\n\n", subject,
(( filename == NULL ) ? "STDIN" : filename ) );
writen(to_server_socket,buf,strlen(buf));
/* the file */
while ( !feof(bulk) ) {
bzero(buf, PACKET_SIZE+1);
nb = fread(buf,sizeof(char), PACKET_SIZE, bulk);
buf[nb] = 0x00;
while ( (ptr = strstr(buf,"\r\n.\r\n") ) != NULL ) *(ptr+3) = '.';
writen(to_server_socket,buf,nb);
if (DEBUG) printf(buf);
}
writen(to_server_socket,"\r\n.\r\n",5);
readn(to_server_socket,buf,PACKET_SIZE);
printf(buf);
fclose(bulk);
}
int writen(int fd, char *ptr, int n)
{
int nl, nw;
nl = n;
while ( nl > 0 ) {
nw = write(fd, ptr, nl);
if ( nw <= 0 )
return nw; /*error*/
nl -= nw;
ptr += nw;
}
return (n-nl);
}
int readn(int fd, char *ptr, int n){
int nl, nr;
nl = n;
while ( nl > 0 ) {
nr = read(fd,ptr,nl);
if (nr < 0 )
return nr; /*error*/
else
if ( nr == 0 )
break;
nl -= nr;
ptr += nr;
if ( *(ptr-2) == '\r' && *(ptr-1) == '\n' )
break;
}
*ptr = 0x00;
return (n-nl);
} |
Partager