Sending SMTP commands causes my program to bail

Recommended Posts

jon723    168
Greetings all, I have a small program that I wrote which opens a connection to my mail server (a telnet connection) and issues some commands which sends an email message to the email address specified. When I first make a connection to the server it responds telling me that the server is ready. The next command that I tell it is who I am (this is done with HELO or EHLO along with my local address). The problem is, when I send the HELO call and then check for a response the program bails at the recv command. Can someone tell me what is going on?? NOTE: I changed the IP and email address to something bogus to protect myself ;-) Here is a snippet of the code:
messages[0] = "EHLO 195.168.100.66";
messages[1] = "MAIL FROM: myemail@email.com";    // sending the mail to myself
messages[2] = "RCPT TO: myemail@email.com";
messages[3] = "DATA";
messages[4] = "This is a simple\nTest of email from a program\n";
messages[5] = ".";
messages[6] = "QUIT";

for(int n = -1; n < 7; n++)
{
if(n == -1)		// This gets the first message sent by the server
{
recCode = new char[MAXLENGTH];
err = recv(h_email, recCode, MAXLENGTH-1, 0);
perror("recv");
if(err == -1)
{
cout << errno << endl;
return -1;
}
recCode[err]='\0';
cout << recCode << endl;	// should output that the server is ready
delete recCode;

continue;
}

err = send(h_email,messages[n].c_str(),messages[n].length(), MSG_OOB);
perror("send");
if(err == -1)
{
cout << errno << endl;
cerr << "Unable to send message " << messages[n].c_str();
}
sleep(500);

recCode = new char[MAXLENGTH];
err = recv(h_email, recCode, MAXLENGTH-1, 0); // the program bails out here......WTF!!!!!
perror("recv");
if(err == -1)
{
cout << errno << endl;
return -1;
}
recCode[err]='\0';
cout << recCode << endl; // should output that the server is ready
delete recCode;
}


I'm able to issue these commands from the command prompt without a problem.

Share on other sites
jon723    168
I realized that the problem occurs on the first message send and this causes the receive to fail. Does anyone have an idea as to why this is happening (why the send command is failing even though it is valid on the command prompt)?

Share on other sites
ntg1976    100
could it be:

messages[0] = "EHLO 195.168.100.66";

Share on other sites
jon723    168
Even switching the EHLO to HELO has no effect. Both are valid commands.

Share on other sites
nirosys    124
SMTP commands are expected to be delimited with "\r\n".

So, it should probably be,

messages[0] = "EHLO 195.168.100.66\r\n";
.
.
etc.

HTH :)

-niro

Share on other sites
hplus0603    11347
In addition, your message is improperly formatted; it should start with a header block, which should include at least Subject:, To:, From: and Message-ID: headers. Each line should be terminated with \r\n. Then should come an empty line, and then the actual lines of the message.

However, that's probably not the problem you're seeing. To debug this problem, I would fire up Ethereal, and see what both sides are actually sending. I would also check the log of my mail server, to see if it has any information about why the connection goes wrong.

Last, when you say that your receive or send is failing, the specific error message (WSAGetLastError() or similar) would be useful information for those trying to help.

Share on other sites
hplus0603    11347
Actually reading your code: the real problem in this case is the MSG_OOB flags. TCP only supports one byte of OOB data AFAICR, and SMTP does not use OOB at all. You should send your commands as real data.

Share on other sites
jon723    168
Thanks for the reply, I'll implement these in the morning. the header block isnt needed. When I did this stright from the command prompt I received the message without a problem. The header stuff is just to make it look nicer (something Ill do once i get the basics working). I wasnt aware that the messages needed an '\r\n' at the end. I tried it with the '\n' but that wasnt working as well. Thanks again everyone.

Create an account

Register a new account