Sign in to follow this  
jon723

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

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

Sign in to follow this