Updated and solved! Issues with sending telnet commands to SMTP server

UPDATE: Paistebin of working code http://pastebin.com/VMj7UNqU

Hello all,

In my current project I am attempting to send emails out to a list of clients which are stored in a file on an sd card. I have been able to send out emails as well as read email addresses from the sd card but I am having an issue combining the two processes.

Here is an example of code that works.

if (client.connect()) {
        client.println("HELO domain.com");
        delay(2000);
        client.println("MAIL FROM: fromemail@domain.com");
	delay(2000);
	client.println("RCPT TO: toemail@domain.com); // Receiving email is hard coded        
	delay(2000);
	client.println("DATA");
	delay(2000);
	client.println(message);
	delay(2000);
	client.println(".");
	delay(2000);
	client.stop();
	PgmPrintln("Email Sent");
} else {
	PgmPrintln("Could not connect");
}

And here is the code which pulls the email address from the file and builds the "RCPT TO: " line.
This doesn’t work for some reason…

f (client.connect()) {
        client.println("HELO domain.com");
        delay(2000);
        client.println("MAIL FROM: fromemail@domain.com");
	delay(2000);char rcptLine[50] = "RCPT TO: ";
	for (byte i = 0; i < strlen(clientName); i++) {
		rcptLine[i+9] = clientName[i]; // Client name taken from the sd card
	}
	client.println(rcptLine); // rcptLine should now be "RCPT TO: email@domain.com"
	delay(2000);
	client.println("DATA");
	delay(2000);
	client.println(message);
	delay(2000);
	client.println(".");
	delay(2000);
	client.stop();
	PgmPrintln("Email Sent");
} else {
	PgmPrintln("Could not connect");
}

Any thoughts or suggestions would be greatly appreciated :slight_smile:
If any other info is needed just ask

After some code changes I now have the output from the server. :grin:
waiting for connection…
Connected!
220 imp12 ESMTP server ready
250 imp12 hello [XX.XX.XX.XX], pleased to meet you
250 2.1.0 email@domain.com sender ok
550 5.1.1 kFgo1h00b3uJrsw01FgrAo <otherdomain.com> does not exist E3110 ← (THIS IS THE ERROR THROWN WHEN I SPECIFY THE RCPT!)
503 5.5.0 need RCPT before DATA

I think the ethernet shield and the SD card use some of the same pins, so you may need to disable one while using the other.

Whats really strange is I can verify that the string is correct using Serial.write() but for some reason the client appears to be getting a malformed string and I can't figure it out! :~

So I just figured it out…

Basically what I did was surrounded the recipient email with “<>” characters and this solve the issue.

For those who may run into this problem here is what my code looks like.

client.println("HELO domain.com");
delay(2000);
client.println("MAIL FROM: name@domain.com");
delay(2000);
char rcptLine[60] = "RCPT To: <"; // Notice the "<" character is explicitly specified.
strcat(rcptLine, clientName); // clientName contains the recipient email address
strcat(rcptLine, ">\r\n"); // Notice the ">" character is explicitly specified.
client.write(rcptLine);
delay(2000);
client.println("DATA");
delay(2000);
client.println(message);
delay(2000);
client.println(".");
delay(2000);

Don't know if you need all those delays. The below works with my ISP's mail server.

void setup()
{
  Ethernet.begin(mac, ip);
 Serial.begin(9600);

 Serial.println("connecting...");

 if (client.connect()) {
                        Serial.println("connected");
   
                        client.println("HELO itismeletschat"); /* say hello (statement after helo is needed but irrelevant)*/

                        client.println("MAIL From: me@my-isp.net"); /* identify sender, this should be the same as the smtp server you are using */

                        client.println("RCPT To: you@your-isp.net"); /* identify recipient */

                        client.println("DATA"); 

                        client.println("To: you@your-isp.net"); /* identify recipient */
                        client.println("Subject: You Have Arduino Mail!!"); /* insert subject */

                        client.println("Please let me know it worked!!!"); /* insert body */
                        client.println("."); /* end email */
                 
                        client.println("QUIT"); /* terminate connection */
                        
                        client.println();
   
 } else {
   Serial.println("connection failed");
 }
}

zoomkat: Don't know if you need all those delays. The below works with my ISP's mail server.

void setup()
{
  Ethernet.begin(mac, ip);
 Serial.begin(9600);

Serial.println("connecting...");

if (client.connect()) {                         Serial.println("connected");                             client.println("HELO itismeletschat"); /* say hello (statement after helo is needed but irrelevant)*/

                        client.println("MAIL From: me@my-isp.net"); /* identify sender, this should be the same as the smtp server you are using */

                        client.println("RCPT To: you@your-isp.net"); /* identify recipient */

                        client.println("DATA");

                        client.println("To: you@your-isp.net"); /* identify recipient /                         client.println("Subject: You Have Arduino Mail!!"); / insert subject */

                        client.println("Please let me know it worked!!!"); /* insert body /                         client.println("."); / end email /                                           client.println("QUIT"); / terminate connection */                                                 client.println();     } else {    Serial.println("connection failed"); } }

I shortened them to around half a second, but am keeping them for cautionary purposes. Thanks for your input :)