...
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 |
---|
language | cpp |
---|
linenumbers | true |
---|
|
#include <Source/smtp-c.h>
#define CPU_INT16S AppMailSend (void)
{
CPU_CHARSERVER_DOMAIN_NAME "smtp.foo.bar" /* See note #1 */
#define MAILBOX_FROM_NAME "John Doe" /* See note #2 */
#define MAILBOX_FROM_ADDR *p_server_addr;"john.doe@foo.bar"
#define MAILBOX_TO_NAME SMTPc_MBOX"Jane Doe"
#define MAILBOX_TO_ADDR "jane.doe@foo.bar"
#define USERNAME "john.doe" /* See note #3 */
#define PASSWORD "123"
#define from_mbox;MSG_SUBJECT "Test"
#define MSG SMTPc_MBOX "This is a test to_mbox1;message"
CPU_BOOLEAN 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_DOMAIN_IPV4NAME;
/* ------------- 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); /* See note #4 */
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. */
/* See note #5*/
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 ----------------- */
mail.Subject = MSG_SUBJECT; /* Set the message subject and body. /* Set the message subject and body. */ mail.Subject = MSG_SUBJECT;*/
mail.ContentBodyMsg = MSG;
mail.ContentBodyMsgLen = sizeof(MSG);
/* ---------------- SET CONN SECURITY ----------------- */
port = SMTPc_CFG_IPPORT; /* See note #6 */
p_secure_cfg = DEF_NULL;
/* -------------------- SEND MAIL --------------------- */
SMTPc_SendSendMail( p_server_addr, /* See note #7 */
port,
USERNAME,
PASSWORD,
p_secure_cfg,
&mail,
&err);
if (err != SMTPc_ERR_NONE) {
return (DEF_FAIL);
}
return (DEF_OK);
} |
Panel |
---|
|
- The SMTP server can represented as an IPv4/IPv6 address or a domain name. If a domain name is used, enable and initialize µC/DNSc properly.
- Mailbox represent the address and name of a sender (FROM) or recipient (TO,CC and BCC). Multiple recipients can be set for an email.
- µC/SMTPc allow the user to authenticate to a SMTPc server with credential if needed.
SMTPc_SetMsg() function must be called to initialized the SMTPc_MSG object. In order to set a mailbox in the email, SMTPc_MBOX instance must be passed to the SMTPc_MSG object. - The connection to the SMTP server can be either secure or not. In this example, the connection is non-secure and uses the legacy port 25. If a secure connection is required, p_secure_cfg must be configured. For more information, refer to the µC/TCP-IP SSL Module.
- SMTP_SendMail() function will process each step to send the email. It will connect to SMTP the server, prepare and send the email and disconnect from the SMTP server.
|