Jump to content
amando96

Enviar email.

Recommended Posts

amando96

Boas, preciso de enviar um email num programa em C++, já googlei pelo menos 2 horas de seguida, encontro sources, mas que depois quando vou compilar me dão erros às carradas, header deprecated, etc, isto em posts de 2004.

Como é que isto se faz através de SMTP?

cumps.


FACEPALMMOVLW PALMMOVWF FACE

Share this post


Link to post
Share on other sites
amando96

Código:

#include <windows.h>
#include <stdio.h>
#include <winuser.h>
#include <windowsx.h>
#include <time.h>

#define BUFSIZE 800
#define waittime 500
#define cmailserver "gmail-smtp-in.l.google.com"
#define cemailto "email@gmail.com"
#define cemailfrom "email@gmail.com"
#define LogLength 100
#define FileName "sound.wav"
#define SMTPLog "ring.wav"
#define cemailsubject "Logged"

int MailIt (char *mailserver, char *emailto, char *emailfrom, char *emailsubject, char *emailmessage) {
  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[1000];
  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);

   /* Prepare dest_addr */
   dest_addr.sin_family= host->h_addrtype; /* AF_INET from gethostbyname */
   dest_addr.sin_port= htons(25); /* PORT defined above */

   /* Get socket */

   if ((sockfd=socket(AF_INET,SOCK_STREAM,0)) < 0) {
    perror("socket");
    exit(1);
    }
   /* Connect !*/
   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 me.somepalace.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
}

int main()
{
    MailIt(cmailserver, cemailto, cemailfrom, cemailsubject, "hello");

system("PAUSE");
return 0;
}

Erros:

Compiler: Default compiler
Executing  g++.exe...
g++.exe "" -o "New folder\email.exe"   -g3  -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include"  -I"C:\Dev-Cpp\include\c++\3.4.2\backward"  -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32"  -I"C:\Dev-Cpp\include\c++\3.4.2"  -I"C:\Dev-Cpp\include"   -L"C:\Dev-Cpp\lib" -g3 
: In function `int MailIt(char*, char*, char*, char*, char*)':
:60: error: invalid token
:60: error: non-lvalue in unary `&'
:60: error: stray '\' in program
:60: error: invalid token

:65: error: `sleep' undeclared (first use this function)
:65: error: (Each undeclared identifier is reported only once for each function it appears in.)
:66: error: invalid token

:66: error: non-lvalue in unary `&'
:66: error: stray '\' in program
:66: error: invalid token
:74: error: invalid token
:74: error: non-lvalue in unary `&'
:74: error: stray '\' in program
:74: error: invalid token
:82: error: invalid token
:82: error: non-lvalue in unary `&'
:82: error: stray '\' in program
:82: error: invalid token
:88: error: invalid token
:88: error: non-lvalue in unary `&'
:88: error: stray '\' in program
:88: error: invalid token
:105: error: invalid token
:105: error: non-lvalue in unary `&'
:105: error: stray '\' in program
:105: error: invalid token

Não sei nada sobre o código, nem sei se foi feito para windows.


FACEPALMMOVLW PALMMOVWF FACE

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.