How to send an Email using C++

C, C++, Visual C++, C++.Net Topics
Post Reply
User avatar
Enigma
Lieutenant
Lieutenant
Posts: 74
Joined: Sun Jan 16, 2011 12:40 am
Location: Colombo, Sri Lanka

How to send an Email using C++

Post by Enigma » Wed Jan 19, 2011 2:07 pm

Hi
Recently I tried to send an email using C++. Here is the solution that I found. Code will work without any issues in windows environment. I have tested this on visual studio 2005. But if you want to run this on linux/unix then use berkeley socket interface instead of winsock. I have used gamil server as the mail server. But I think any SMTP mail server with telnet enabled can use for this. Hope this will help you. :)

Code: Select all

#include <stdlib.h>
#include <winsock2.h>
#include <stdio.h>

#pragma comment(lib,"ws2_32.lib") // use to link the winsock library

#define MailServer "gmail-smtp-in.l.google.com"
#define Mailto "[email protected]"
#define Mailfrom "[email protected]"
#define bufsize 10000

int main()
{
	char* subject = "This is a Test Email";
	char* Message = "Hi\n This is a test email from my computer";

	SOCKET mail_socket;
	sockaddr_in service;
	WORD wVersionRequested;
	WSADATA wsaData;
	struct hostent *host;
	int wsaerr;
	int recvBytes=0;
    
	char *Temp_Buf = (char*) malloc(bufsize+1);

	wVersionRequested = MAKEWORD(2, 2);
	wsaerr = WSAStartup(wVersionRequested, &wsaData);
	if (wsaerr != 0)
	{
		return 0;
	}

	mail_socket = socket(AF_INET,SOCK_STREAM,0);
	if(mail_socket == INVALID_SOCKET)
	{
		WSACleanup();
		return 0;
	}
	host = gethostbyname(MailServer);
	memset(&service,0,sizeof(service));
    memcpy(&(service.sin_addr),host->h_addr,host->h_length);

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

	 if (connect(mail_socket, (struct sockaddr *)&service,sizeof(service)) == -1)
	 {
		 perror("connect");
		 closesocket(mail_socket);
		 WSACleanup();
		 return 0;
     }
	
	 memset(Temp_Buf,'\0',bufsize);
	 recvBytes = recv(mail_socket,Temp_Buf,bufsize,0);
	 memset(Temp_Buf,'\0',bufsize);

	 strcpy(Temp_Buf,"helo me.hi.com\n");
	 send(mail_socket,Temp_Buf,strlen(Temp_Buf),0);
	
	 recvBytes = recv(mail_socket,Temp_Buf,bufsize,0);
	 memset(Temp_Buf,'\0',bufsize);
	
	 
	 sprintf(Temp_Buf,"MAIL FROM:<%s>\n",Mailfrom);
	 send(mail_socket,Temp_Buf,strlen(Temp_Buf),0);

	 recvBytes = recv(mail_socket,Temp_Buf,bufsize,0);
	 memset(Temp_Buf,'\0',bufsize);

     
	 sprintf(Temp_Buf,"RCPT TO:<%s>\n",Mailto);
	 send(mail_socket,Temp_Buf,strlen(Temp_Buf),0);
	
	 recvBytes = recv(mail_socket,Temp_Buf,bufsize,0);
	 memset(Temp_Buf,'\0',bufsize);
	

	 strcpy(Temp_Buf,"DATA\n");
	 send(mail_socket,Temp_Buf,strlen(Temp_Buf),0);
	
	 recvBytes = recv(mail_socket,Temp_Buf,bufsize,0);
	 memset(Temp_Buf,'\0',bufsize);

	 sprintf(Temp_Buf,"To:%s\nFrom:\%s\nSubject:%s\n%s\r\n.\r\n",Mailto,Mailfrom,subject,Message);
	 send(mail_socket,Temp_Buf,strlen(Temp_Buf),0);
	
	 memset(Temp_Buf,'\0',bufsize);
	 recvBytes = recv(mail_socket,Temp_Buf,bufsize,0);
	 memset(Temp_Buf,'\0',bufsize);
	
	 strcpy(Temp_Buf,"quit\n");
	 send(mail_socket,Temp_Buf,strlen(Temp_Buf),0);
	
	 recvBytes = recv(mail_socket,Temp_Buf,bufsize,0);
	 memset(Temp_Buf,'\0',bufsize);
	

     closesocket(mail_socket);
	 WSACleanup();

return 0;
}

User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

Re: How to send an Email using C++

Post by Saman » Wed Jan 19, 2011 2:48 pm

Dude, you are an asset to us.

BTW: Under Linux, you can directly include <sys/socket.h> and use sockets without need of any additional library.

I have added a sample with server and client code.

See Sample code to use sockets in Linux/C++.
User avatar
Enigma
Lieutenant
Lieutenant
Posts: 74
Joined: Sun Jan 16, 2011 12:40 am
Location: Colombo, Sri Lanka

Re: How to send an Email using C++

Post by Enigma » Wed Jan 19, 2011 4:03 pm

wow I didn't know that. That's grate. :)
Pansophic
Sergeant
Sergeant
Posts: 25
Joined: Sun Feb 13, 2011 4:05 pm

Re: How to send an Email using C++

Post by Pansophic » Tue Mar 01, 2011 2:42 pm

OMG, didn't knew we could make that in c++ will try this for sure.
Post Reply

Return to “C/C++ Programming”