SMTP (Simple Mail Transfer Protocol) is one of the most common ways of sending e-mail. SMTP is a simple text conversation across a TCP/IP connection. The SMTP server usually resides on TCP port 25 waiting for clients to connect.
Sending mail with the
SMTP.LIBclient library is a four-step process. First, build your e-mail message, then callsmtp_sendmail(). Next, repetitively callsmtp_mailtick()while it is returningSMTP_PENDING. Finally, callsmtp_status()to determine if the mail was sent successfully. There is a sample program in Section 8.4 that outlines how to send a simple mail message.8.1 Sample Conversation
The following is a typical listing of mail from the controller (me@somewhere.com) to someone@somewhereelse.com. The mail server that the controller is talking to is mail.somehost.com. The lines that begin with a numeric value are coming from the mail server. The other lines were sent by the controller. More information on the exact specification of SMTP and the meanings of the commands and responses can be found in RFC821 at
http://www.ietf.org.
You can see a listing of the conversation between your controller and the mail server by defining the
SMTP_DEBUGmacro at the top of your program.Note that there must be a blank line after the line "Subject: test mail".
8.2 Configuration
The SMTP client is configured by using compiler macros.
SMTP_DEBUG
This macro tells the SMTP code to log events to the STDIO window in Dynamic C. This provides a convenient way of troubleshooting an e-mail problem.
SMTP_DOMAIN
This macro defines the text to be sent with the
HELOclient command. Many mail servers ignore the information supplied with theHELO, but some e-mail servers require the fully qualified name in this field (i.e., somemachine.somedomain.com). If you have problems with e-mail being rejected by the server, turn onSMTP_DEBUG. If it is giving an error message after theHELOline, talk to the administer of the machine for the appropriate value to place inSMTP_DOMAIN. If you do not define this macro, it will default toMY_IP_ADDRESS.#define SMTP_DOMAIN "somemachine.somedomain.com"SMTP_SERVER
This macro defines the mail server that will relay the controller's mail. This server must be configured to relay mail for your controller. You can either place a fully qualified domain name or an IP address in this field.
#define SMTP_SERVER "mail.mydomain.com"#define SMTP_SERVER "10.10.6.19"SMTP_TIMEOUT
This macro tells the SMTP code how long in seconds to try to send the e-mail before timing out. It defaults to 20 seconds.
#define SMTP_TIMEOUT 108.3 Functions
smtp_sendmail
void smtp_sendmail(char* to, char* from, char* subject, char* message);Description
This function initializes the internal data structures with strings for the to e-mail address, the from e-mail address, the subject, and the body of the message. You should not modify these strings until
smtp_mailtickno longer returnsSMTP_PENDING.Parameters
to
String containing the e-mail address of the destination.
from
String containing the e-mail address of the source.
subject
String containing the subject of the message.
message
String containing the message. (This string must NOT contain the byte sequence "\r\n.\r\n" (CRLF.CRLF), as this is used to mark the end of the e-mail, and will be appended to the e-mail automatically.)
Return value
Library
smtp_sendmailxmem
void smtp_sendmailxmem(char* to, char* from, char* subject, long message, long messagelen);Description
This function initializes the internal data structures with strings for the to e-mail address, the from e-mail address, the subject, and the body of the message. You should not modify these strings until
smtp_mailtickno longer returnsSMTP_PENDING.Parameters
to
String containing the e-mail address of the destination.
from
String containing the e-mail address of the source.
subject
String containing the subject of the message.
message
Physical address in xmem containing the message. (The message must NOT contain the byte sequence "\r\n.\r\n" (CRLF.CRLF), as this is used to mark the end of the e-mail, and will be appended to the e-mail automatically.)
messagelen
Length of the message in xmem.
Return value
Library
smtp_mailtick
int smtp_mailtick(void);Description
Repetitively call this function until e-mail is completely sent. For a small message, this function will need to be called about 20 times to send the message. The number of times will vary depending on the latency of you connection to the mail server and the size of your message.
Return value
SMTP_SUCCESS- e-mail sent.SMTP_PENDING- e-mail not sent yet callsmtp_mailtickagain.SMTP_TIME- e-mail not sent withinSMTP_TIMEOUTseconds.SMTP_UNEXPECTED- received an invalid response from SMTP server.Library
smtp_status
int smtp_status(void);Description
Return the status of the last e-mail processed.
Return value
SMTP_SUCCESS- e-mail sent.SMTP_PENDING- e-mail not sent yet call smtp_mailtick again.SMTP_TIME- e-mail not sent withinSMTP_TIMEOUTseconds.SMTP_UNEXPECTED- received an invalid response from SMTP server.Library
8.4 Sample Sending of an E-mail
This program,
smtp.c, uses the SMTP library to send an e-mail. For an example of usingsmtp_sendmailxmem(), see the sample programsamples\tcpip\smtp\smtpxmem.c.Program Name: /samples/tcpip/smtp/smtp.c
| Z-World http://www.zworld.com Voice: 530.757.3737 Fax: 530.757.3792 or 530.753.5141 sales@zworld.com |