Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 5 Next »

Initialization

µC/SMTPc requires no initialization. 

Note that µC/TCP-IP must be initialized without error. Also, µC/DNSc must be initialize to resolve SMTP Server host name. 

Sending an e-mail

#include <Source/smtp-c.h>

#define SERVER_DOMAIN_NAME   "smtp.foo.bar"

#define MAILBOX_FROM_NAME    "John Doe"
#define MAILBOX_FROM_ADDR    "john.doe@foo.bar"
#define MAILBOX_TO_NAME      "Jane Doe"
#define MAILBOX_TO_ADDR      "jane.doe@foo.bar"

#define USERNAME             "john.doe"
#define PASSWORD             "123"

#define MSG_SUBJECT          "Test"
#define MSG                  "This is a test message"

CPU_BOOLEAN  AppMailSend (void)
{
    CPU_CHAR                *p_server_addr;
    SMTPc_MBOX               from_mbox;
    SMTPc_MBOX               to_mbox;
    SMTPc_MSG                mail;
    SMTPc_ERR                err;
    CPU_INT16U               port;
    NET_APP_SOCK_SECURE_CFG *p_secure_cfg;
    p_server_addr = SERVER_DOMAIN_NAME;
                                                                /* ------------- INITIALIZE THE MAIL OBJ -------------- */
                                                                /* Set the email object mailbox and content to Null.    */
                                                                /* All fields set to Null will be ignore in the email...*/
                                                                /* ...transmission.                                     */
    SMTPc_SetMsg (&mail, &err);
    if (err != SMTPc_ERR_NONE) {
        return (DEF_FAIL);
    }
                                                                /* ---------------- SET MAIL ENVELOP ------------------ */
                                                                /* Set all the mailbox which the email is related to.   */
                                                                /* SMTPc_MSG need at least the TO and FROM mailbox...   */
                                                                /* ...in order to be sent properly.                     */
    mail.From = &from_mbox;                                     /* Set the FROM mailbox of the e-mail.                  */
    SMTPc_SetMbox(mail.From, MAILBOX_FROM_NAME, MAILBOX_FROM_ADDR, &err);
    if (err != SMTPc_ERR_NONE) {
        return (DEF_FAIL);
    }
    mail.ToArray[0] = &to_mbox;                                 /* Set the TO mailbox of the e-mail.                    */
    SMTPc_SetMbox(mail.ToArray[0], MAILBOX_TO_NAME, MAILBOX_TO_ADDR, &err);
    if (err != SMTPc_ERR_NONE) {
        return (DEF_FAIL);
    }
                                                                /* ----------------- SET MAIL CONTENT ----------------- */
                                                                /* Set the message subject and body.                    */
    mail.Subject           = MSG_SUBJECT;
    mail.ContentBodyMsg    = MSG;
    mail.ContentBodyMsgLen = sizeof(MSG);
                                                                /* ---------------- SET CONN SECURITY ----------------- */
    port                   = SMTPc_CFG_IPPORT;
    p_secure_cfg           = DEF_NULL;
                                                                /* -------------------- SEND MAIL --------------------- */
    SMTPc_SendMail( p_server_addr,
                    port,
                    USERNAME,
                    PASSWORD,
                    p_secure_cfg,
                   &mail,
                   &err);
    if (err != SMTPc_ERR_NONE) {
        return (DEF_FAIL);
    }
    return (DEF_OK);
}
  • No labels