Jump to content
Tuts 4 You

(C++) Send Email


deepzero

Recommended Posts

Posted (edited)

Hi,

so, i``m trying to send an email from one gmail acc. to another using winosck:

Code looks good, google is "at my service", but the email never arrives. :teehee:


#include "stdafx.h"
#include <winsock2.h>
#include <string>
#include <stdlib.h>
#include <iostream>
using namespace std;#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "urlmon.lib")
#pragma comment(lib, "shell32.lib")
int main()
{
int iProtocolPort = 0;
char szBuffer[4096] = "";
SOCKET hServer;
LPHOSTENT lpHostEntry;
LPSERVENT lpServEntry;
SOCKADDR_IN SockAddr; WSADATA WSAData;
DWORD WSAResult; WSAResult = WSAStartup(MAKEWORD(2,2), &WSAData); //lookup SMTP server's IP address
lpHostEntry = gethostbyname("smtp.gmail.com"); if(!lpHostEntry)
{
// SMTP server not found!
return false;
} // create a TCP/IP socket, no specific protocol
hServer = socket(PF_INET, SOCK_STREAM, 0);
if (hServer == INVALID_SOCKET)
return false;//connection failed SockAddr.sin_family = AF_INET;
SockAddr.sin_port = htons(25);//iProtocolPort;
SockAddr.sin_addr = *((LPIN_ADDR) *lpHostEntry->h_addr_list); // connect the Socket
if(connect(hServer, (PSOCKADDR) &SockAddr, sizeof(SockAddr)))
return false;//connection failed .)
// receive initial response from SMTP server
recv(hServer, szBuffer, sizeof(szBuffer), 0); // send HELO smtp_server.com
sprintf(szBuffer, "HELO %s\r\n", "smtp.gmail.com");//SMTP
send(hServer, szBuffer, strlen(szBuffer), 0);
recv(hServer, szBuffer, sizeof(szBuffer), 0); //###### sprintf(szBuffer,"ehlo\r\n");
send(hServer,szBuffer,strlen(szBuffer),0);
recv(hServer,szBuffer,sizeof(szBuffer),0);
strcpy(szBuffer, "STARTTLS\r\n");
send(hServer, szBuffer, strlen(szBuffer), 0);
recv(hServer, szBuffer, sizeof(szBuffer), 0);
// Sending login request
strcpy(szBuffer, "auth login\r\n");
send(hServer, szBuffer, strlen(szBuffer), 0);
recv(hServer, szBuffer, sizeof(szBuffer), 0); // Send userid
sprintf(szBuffer,"%s\r\n", "base64(a2@gmail.com");
send(hServer, szBuffer, strlen(szBuffer), 0);
recv(hServer, szBuffer, sizeof(szBuffer), 0);
// Send Pwd
sprintf(szBuffer,"%s\r\n", "base64(password)");
send(hServer, szBuffer, strlen(szBuffer), 0);
recv(hServer, szBuffer, sizeof(szBuffer), 0); //######
// send From
sprintf(szBuffer, "MAIL FROM:<%s>\r\n", "a2@gmail.com");
send(hServer, szBuffer, strlen(szBuffer), 0);
recv(hServer, szBuffer, sizeof(szBuffer), 0); // Send To
sprintf(szBuffer, "RCPT TO:<%s>\r\n", "a1@gmail.com");
send(hServer, szBuffer, strlen(szBuffer), 0);
recv(hServer, szBuffer, sizeof(szBuffer), 0); // Send Data
sprintf(szBuffer, "DATA\r\n");
send(hServer, szBuffer, strlen(szBuffer), 0);
recv(hServer, szBuffer, sizeof(szBuffer), 0);
// Setting Subject and Body
sprintf(szBuffer,"Subject: %s\r\n\r\n%s\r\n.\r\n","subject", "msgmsgmsg"); //message & subject send(hServer, szBuffer, strlen(szBuffer), 0);
recv(hServer, szBuffer, sizeof(szBuffer), 0);//blank line to end
sprintf(szBuffer, "");
send(hServer, szBuffer, strlen(szBuffer), 0);
recv(hServer, szBuffer, sizeof(szBuffer), 0); // Sending end command
sprintf(szBuffer, "quit\r\n");
send(hServer, szBuffer, strlen(szBuffer), 0);
recv(hServer, szBuffer, sizeof(szBuffer), 0); // clean up - close server socket
closesocket(hServer); return true;
}

I base64 encode the uid and the password manually, then just use the encoded strings in the code.

Gmail states here:
/>http://mail.google.com/support/bin/answer.py?answer=78799

that i am supposed to use port 465, but then the connection fails.

Using port 25 works fine (server response: gmail is at you service).

Does anyone know what goes wrong? :)

dp0 :)

Edited by deepzero
Posted

Do you know what the command STARTTLS is doing?

Posted

not really! :/

I was told by the gmail server to use it. :woot:

will do some more reading...

Posted

After sending "STARTTLS" to the server i get the response:

"220 2.0.0 Ready to start TLS

ice, [%myip%]

250-SIZE 35651584

250-8BITMIME

250-STARTTLS

250 ENHANCEDSTATUSCODES

So gmail is ready to start tls, but how do i start it?

Couldnt find any info, just desperate people in other forums... :dunno:

Posted

Check this CodeProject page out, it might help with this issue:
/>http://www.codeproject.com/KB/IP/smtp_ssl.aspx

Posted

dont you think that this will not work? even old troyans that report back with ip in email to attacker dont work.

due all email connections are encrypted today and not on 25 port but 465 Secure to dedicated port(TLS).

so i think you need ssl and somehow mix it with google api.

im not expert in that field so cant tell how.

Posted (edited)

So are you trying to use TLS? I'm no expert on the protocol but just telnetting to smtp.google.com I think you can send a spoof email to yourself, try just connecting to port 25 and not sending STARTTLS and your code should work, but if you want encryption I have no idea how TLS works...lol

edit: found this project, take a look at the source: http://www.codeproject.com/KB/IP/smtp_ssl.aspx

It requires a lot of complicated header files but those are all GNU and you wont have to look at those in detail.

EDIT2: just realized this was already posted lol whatever...

Edited by Dooms_day
Posted

You can debug "Foxmail",then write your code!

Posted (edited)

You gotta use RFC 2821 protocal standards

its

HELO JMC31337

mail from:<jmc31337@gmail.com>

rcpt to:<jmc31337@gmail.com>

data

123

.

quit

then it will end up in your SPAM folder as unknown sender
/>http://www.ietf.org/rfc/rfc2821.txt

use nslookup

set type=mx

gmail.com

telnet gmail-smtp-in.l.google.com 25

now with Nemesis SMTP Worm

it'll use C# to pass a null pointer and use DEFAULT USER CREDENTIALS which uses YOUR ISP to send email making it look as though its a legit source...

client.Credentials = new System.Net.NetworkCredential(null, "");

Edited by JMC31337

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...