2012년 10월 28일 일요일

Gmail SMTP서버를 이용한 메일 보내기


#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <winsock.h>

#pragma comment(lib, "ws2_32.lib")

#define cmailserver "gmail-smtp-in.l.google.com"
#define cemailto "gonanhack@gmail.com"
#define cemailfrom "gonanhack@gmail.com"
#define SMTPLog "log.txt"
#define cemailsubject "test"
#define waittime 30
#define emailmessage "asdfasdfasdfasdfasdfasd"

int MailIt(char *mailserver, char *emailto, char *emailfrom,
           char *emailsubject, char *emailmessage, int maxlen);

int main(int argc, char* argv[])
{
 MailIt(cmailserver, cemailto, cemailfrom, cemailsubject, "asdfsadf", 255);
 return 0;
}


int MailIt(char *mailserver, char *emailto, char *emailfrom,
           char *emailsubject, char *emailmessage, int maxlen)
{

 SOCKET sockfd;
    WSADATA wsaData;
    FILE *smtpfile;

    #define bufsize 300
    int bytes_sent;   /* Sock FD */
    int err;
    struct hostent *host;   /* info from gethostbyname */
    struct sockaddr_in dest_addr;   /* Host Address */
    char line[256+100];
    char *Rec_Buf = (char*) malloc(bufsize+1);
    smtpfile=fopen(SMTPLog,"a+");
    if (WSAStartup(0x202,&wsaData) == SOCKET_ERROR)
 {
  fputs("WSAStartup failed",smtpfile);
  WSACleanup();
  return -1;
    }
    if ( (host=gethostbyname(mailserver)) == NULL)
 {
  perror("gethostbyname");
  exit(1);
    }
    memset(&dest_addr,0,sizeof(dest_addr));
    memcpy(&(dest_addr.sin_addr),host->h_addr,host->h_length);


 dest_addr.sin_family= host->h_addrtype;  
 dest_addr.sin_port= htons(25);



 if ((sockfd=socket(AF_INET,SOCK_STREAM,0)) < 0)
 {
  perror("socket");
  exit(1);
 }

 fputs("Connecting....\n",smtpfile);

 if (connect(sockfd, (struct sockaddr *)&dest_addr,sizeof(dest_addr)) == -1)
 {
  perror("connect");
        exit(1);
 }
 Sleep(waittime);
 err=recv(sockfd,Rec_Buf,bufsize,0);Rec_Buf[err] = '\0';
 fputs(Rec_Buf,smtpfile);
 strcpy(line,"helo www.gmail.com\n");
 fputs(line,smtpfile);
 bytes_sent=send(sockfd,line,strlen(line),0);
 Sleep(waittime);
 err=recv(sockfd,Rec_Buf,bufsize,0);Rec_Buf[err] = '\0';
 fputs(Rec_Buf,smtpfile);
 strcpy(line,"MAIL FROM:<");
 strncat(line,emailfrom,strlen(emailfrom));
 strncat(line,">\n",3);
 fputs(line,smtpfile);
 bytes_sent=send(sockfd,line,strlen(line),0);
 Sleep(waittime);
 err=recv(sockfd,Rec_Buf,bufsize,0);Rec_Buf[err] = '\0';
 fputs(Rec_Buf,smtpfile);
 strcpy(line,"RCPT TO:<");
 strncat(line,emailto,strlen(emailto));
 strncat(line,">\n",3);
 fputs(line,smtpfile);
 bytes_sent=send(sockfd,line,strlen(line),0);
 Sleep(waittime);
 err=recv(sockfd,Rec_Buf,bufsize,0);Rec_Buf[err] = '\0';
 fputs(Rec_Buf,smtpfile);
 strcpy(line,"DATA\n");
 fputs(line,smtpfile);
 bytes_sent=send(sockfd,line,strlen(line),0);
 Sleep(waittime);
 err=recv(sockfd,Rec_Buf,bufsize,0);Rec_Buf[err] = '\0';
 fputs(Rec_Buf,smtpfile);
 Sleep(waittime);
 strcpy(line,"To:");
 strcat(line,emailto);
 strcat(line,"\n");
 strcat(line,"From:");
 strcat(line,emailfrom);
 strcat(line,"\n");
 strcat(line,"Subject:");
 strcat(line,emailsubject);
 strcat(line,"\n");
 strcat(line,emailmessage);
 strcat(line,"\r\n.\r\n");
 fputs(line,smtpfile);
 bytes_sent=send(sockfd,line,strlen(line),0);
 Sleep(waittime);
 err=recv(sockfd,Rec_Buf,bufsize,0);Rec_Buf[err] = '\0';
 fputs(Rec_Buf,smtpfile);
 strcpy(line,"quit\n");
 fputs(line,smtpfile);
 bytes_sent=send(sockfd,line,strlen(line),0);
 Sleep(waittime);
 err=recv(sockfd,Rec_Buf,bufsize,0);Rec_Buf[err] = '\0';
 fputs(Rec_Buf,smtpfile);
 fclose(smtpfile);
 #ifdef WIN32
 closesocket(sockfd);
 WSACleanup();
 #else
 close(sockfd);
 #endif
}

댓글 없음:

댓글 쓰기

국정원의 댓글 공작을 지탄합니다.

UPBIT is a South Korean company, and people died of suicide cause of coin investment.

 UPBIT is a South Korean company, and people died of suicide cause of coin. The company helps the people who control the market price manipu...