Create socket and send SMTP mail

Hello. Im trying to get a respons from my mail server on localhost.

I can connect to it through command shell like this:

telnet mail.mydomian.com 25
If I then write "HELO mail.myhost.com" Then I get a answer ( 250 Hello )

The problem is that I dont get any respons back when I try to this with my own program/code:


iResult = connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) );

iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);

cout << recvbuf;
//220 mail.mydomain.com 25... So far so good

char message[] = "HELO mail.myhost.com";

send(ConnectSocket, message, strlen(message), 0);

iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);

cout << recvbuf;
//Nothing


Obviously I'm missing something, but don't know what


Last edited on
I was first thinking of suggesting curl for you, but the syntax is a lot better in a language like python... and from looking around smtp does seem like a basic tcp connection like how http websites work (unless you want portability, curl is great for that).

step 1: figure out if you are using winsock(windows) or bsdsock(linux)
step 2: find a website with documentation (winsock: msdn/IBM, bsdsock: one of the many gnu doc sites)
step 3: go through every function, read what they return, and error check everything correctly, and if you are lucky you will find an example showing you how it is done.

There may be a firewall, or you are missing a critical part of your code like WSA startup or something like that.

also avoid posting questions on forums without putting a bit of effort in error checking, or at least say in your post that you removed the error checks for shortening the code. And it always helps to put out something that compiles.

how to use code tags: http://www.cplusplus.com/articles/jEywvCM9/
You must download and install https://www.wireshark.org/
If you're writing any kind of networking s/w, it's invaluable because it allows you to see what's going on.

In particular, being able to see the difference between
telnet mail.mydomian.com 25
"HELO mail.myhost.com"


vs

1
2
3
4
5
6
iResult = connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) );
iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
cout << recvbuf;
//220 mail.mydomain.com 25... So far so good
char message[] = "HELO mail.myhost.com";
send(ConnectSocket, message, strlen(message), 0);



> char message[] = "HELO mail.myhost.com";
Try it with \r\n appended to the end of the line.
https://www.ietf.org/rfc/rfc2821.txt - 2.3.7 Lines
Thanks Salem.

I was missing the new lines (\r\n).
Topic archived. No new replies allowed.