Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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

 

Code Block
languagecpp
#include <Source/smtp-c.h>

 
CPU_INT16S  AppMailSend (void)
{#define SERVER_DOMAIN_NAME   "smtp.foo.bar"
 
#define MAILBOX_FROM_NAME     CPU_CHAR  "John Doe"
#define MAILBOX_FROM_ADDR    "john.doe@foo.bar"
#define MAILBOX_TO_NAME	     "Jane Doe"
#define *pMAILBOX_serverTO_addr;ADDR     SMTPc_MBOX  "jane.doe@foo.bar"


#define USERNAME          from_mbox;   "john.doe
#define SMTPc_MBOXPASSWORD             "123"
 
to_mbox1;CPU_INT16S  AppMailSend (void)
{
    SMTPcCPU_MBOXCHAR                to_mbox2*p_server_addr;
    SMTPc_MBOX               ccfrom_mbox;
    SMTPc_MBOX               bccto_mbox;
    SMTPc_MSG                mail;
    SMTPc_ERR                err;
    CPU_INT16U               port;
    NET_APP_SOCK_SECURE_CFG *p_secure_cfg;

    p_server_addr = SERVER_IPV4DOMAIN_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_mbox1mbox;                                 /* Set the TO mailbox of the e-mail.                    */
    SMTPc_SetMbox(mail.ToArray[0], MAILBOX_TO_NAME_1, MAILBOX_TO_ADDR_1, &err);
    if (err != SMTPc_ERR_NONE) {
        return (DEF_FAIL);
    }
                                                                /* Set the CC mailbox of the e-mail.                    */
    mail.CCArray[0] = &cc_mbox;
    SMTPc_SetMbox(mail.CCArray[0], MAILBOX_CC_NAME, MAILBOX_CC_ADDR, &err);
    if (err != SMTPc_ERR_NONE) {
        return (DEF_FAIL);
    }
                                                                /* Set the BCC mailbox of the e-mail.                   */
    mail.BCCArray[0] = &bcc_mbox;
    SMTPc_SetMbox(mail.BCCArray[0], MAILBOX_BCC_NAME, MAILBOX_BCC_ADDR, &err);
    if (err != SMTPc_ERR_NONE) {
        return (DEF_FAIL);
    	}
                                                                /*
The TO,CC and BCC
mailbox array allow to send...     */                                                                 /* ... e-mails to multiple destination.                 */
    mail.ToArray[1] = &to_mbox2;
    SMTPc_SetMbox(mail.ToArray[1], MAILBOX_TO_NAME_2, MAILBOX_TO_ADDR_2, &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_SendSendMail( p_server_addr,
                    port,
                    USERNAME,
                    PASSWORD,
                    p_secure_cfg,
                   &mail,
                   &err);
    if (err != SMTPc_ERR_NONE) {
        return (DEF_FAIL);
    }
    return (DEF_OK);
}