/***************************************************************************** Function: void SMTPDemo(void) Summary: Demonstrates use of the e-mail (SMTP) client. Description: This function demonstrates the use of the SMTP client. The function is called periodically by the stack, and checks if BUTTON2 and BUTTON3 are pressed simultaneously. If they are, it attempts to send an e-mail message using parameters hard coded in the function below. While the client is executing, LED1 will be used as a busy indicator. LED2 will light when the transmission has been completed successfully. If both LEDs extinguish, an error occurred. For an example of sending a longer message (one that does not exist in RAM all at once), see the commented secondary implementation of this function in this file (SMTPDemo.c) below. For an example of sending a message using parameters gathered at run time, and/or a message with attachments, see the implementation of HTTPPostEmail in CustomHTTPApp.c. Precondition: The SMTP client is initialized. Parameters: None Returns: None ***************************************************************************/ void SMTPDemo(void) { // Send an email once if someone pushes BUTTON2 and BUTTON3 at the same time // This is a simple message example, where the message // body must already be in RAM. // LED1 will be used as a busy indicator // LED2 will be used as a mail sent successfully indicator static enum { MAIL_HOME = 0, MAIL_BEGIN, MAIL_SMTP_FINISHING, MAIL_DONE } MailState = MAIL_HOME; static DWORD WaitTime; switch(MailState) { case MAIL_HOME: if((BUTTON2_IO == 0u) && (BUTTON3_IO == 0u)) { // Start sending an email LED1_IO = 1; MailState++; LED2_IO = 0; } break; case MAIL_BEGIN: if(SMTPBeginUsage()) { // Note that these strings must stay allocated in // memory until SMTPIsBusy() returns FALSE. To // guarantee that the C compiler does not reuse this // memory, you must allocate the strings as static. static BYTE RAMStringTo[] = "*****@*****.**"; //static BYTE RAMStringCC[] = "[email protected], \"Jane Smith\" <*****@*****.**>"; //static BYTE RAMStringBCC[] = ""; static BYTE RAMStringBody[] = "Message generated by stack " TCPIP_STACK_VERSION " \r\n\r\nButtons: 3210"; RAMStringBody[sizeof(RAMStringBody)-2] = '0' + BUTTON0_IO; RAMStringBody[sizeof(RAMStringBody)-3] = '0' + BUTTON1_IO; RAMStringBody[sizeof(RAMStringBody)-4] = '0' + BUTTON2_IO; RAMStringBody[sizeof(RAMStringBody)-5] = '0' + BUTTON3_IO; SMTPClient.Server.szROM = (ROM BYTE*)"mail"; // SMTP server address SMTPClient.ROMPointers.Server = 1; //SMTPClient.Username.szROM = (ROM BYTE*)"mchpboard"; //SMTPClient.ROMPointers.Username = 1; //SMTPClient.Password.szROM = (ROM BYTE*)"secretpassword"; //SMTPClient.ROMPointers.Password = 1; SMTPClient.To.szRAM = RAMStringTo; SMTPClient.From.szROM = (ROM BYTE*)"\"SMTP Service\" <*****@*****.**>"; SMTPClient.ROMPointers.From = 1; SMTPClient.Subject.szROM = (ROM BYTE*)"Hello world! SMTP Test."; SMTPClient.ROMPointers.Subject = 1; SMTPClient.Body.szRAM = RAMStringBody; SMTPSendMail(); MailState++; } break; case MAIL_SMTP_FINISHING: if(!SMTPIsBusy()) { // Finished sending mail LED1_IO = 0; MailState++; WaitTime = TickGet(); LED2_IO = (SMTPEndUsage() == SMTP_SUCCESS); } break; case MAIL_DONE: // Wait for the user to release BUTTON2 or BUTTON3 and for at // least 1 second to pass before allowing another // email to be sent. This is merely to prevent // accidental flooding of email boxes while // developing code. Your application may wish to // remove this. if(BUTTON2_IO && BUTTON3_IO) { if(TickGet() - WaitTime > TICK_SECOND) MailState = MAIL_HOME; } break; } }
void MyWIFI_SMTP(void) { static DWORD WaitTime; switch (MailState) { case MAIL_HOME: if (MyMail_Flag) { // Start sending an email MyMail_Flag = FALSE; MailState++; } break; case MAIL_BEGIN: if (SMTPBeginUsage()) { // Note that these strings must stay allocated in // memory until SMTPIsBusy() returns FALSE. To // guarantee that the C compiler does not reuse this // memory, you must allocate the strings as static. static BYTE RAMStringTo[] = "*****@*****.**"; //static BYTE RAMStringCC[] = "[email protected], \"Jane Smith\" <*****@*****.**>"; //static BYTE RAMStringBCC[] = ""; static BYTE RAMStringBody[] = "Message generated by stack \r\n\r\nButtons: 3210"; SMTPClient.Server.szROM = (ROM BYTE*)"relay.skynet.be"; // SMTP server address SMTPClient.ROMPointers.Server = 1; SMTPClient.ServerPort = (WORD) 25; //SMTPClient.UseSSL = TRUE; //SMTPClient.Username.szROM = (ROM BYTE*)"mchpboard"; //SMTPClient.ROMPointers.Username = 1; //SMTPClient.Password.szROM = (ROM BYTE*)"secretpassword"; //SMTPClient.ROMPointers.Password = 1; SMTPClient.To.szRAM = RAMStringTo; SMTPClient.From.szROM = (ROM BYTE*)"\"Home\" <*****@*****.**>"; SMTPClient.ROMPointers.From = 1; SMTPClient.Subject.szROM = (ROM BYTE*) "Hello world! SMTP Test."; SMTPClient.ROMPointers.Subject = 1; SMTPClient.Body.szRAM = RAMStringBody; SMTPSendMail(); MailState++; } break; case MAIL_SMTP_FINISHING: if (!SMTPIsBusy()) { // Finished sending mail MailState++; WaitTime = TickGet(); if (SMTPEndUsage() == SMTP_SUCCESS) MyConsole_SendMsg("Mail sent successfully\n"); else MyConsole_SendMsg("Mail sent not successfully\n"); } break; case MAIL_DONE: // Wait for at least 1 second to pass before allowing another // email to be sent. This is merely to prevent // accidental flooding of email boxes while // developing code. Your application may wish to // remove this. if (TickGet() - WaitTime > TICK_SECOND) MailState = MAIL_HOME; break; } }
/***************************************************************************** Function: void SMTPDemo(void) Summary: Demonstrates use of the e-mail (SMTP) client. Description: This function demonstrates the use of the SMTP client. The function is called periodically by the stack, and checks if BUTTON2 and BUTTON3 are pressed simultaneously. If they are, it attempts to send an e-mail message using parameters hard coded in the function below. While the client is executing, LED1 will be used as a busy indicator. LED2 will light when the transmission has been completed successfully. If both LEDs extinguish, an error occurred. For an example of sending a longer message (one that does not exist in RAM all at once), see the commented secondary implementation of this function in this file (SMTPDemo.c) below. For an example of sending a message using parameters gathered at run time, and/or a message with attachments, see the implementation of HTTPPostEmail in CustomHTTPApp.c. Precondition: The SMTP client is initialized. Parameters: None Returns: None ***************************************************************************/ void SMTPDemo(void) { // Send an email once if someone pushes BUTTON2 and BUTTON3 at the same time // This is a simple message example, where the message // body must already be in RAM. // LED1 will be used as a busy indicator // LED2 will be used as a mail sent successfully indicator static enum { MAIL_HOME = 0, MAIL_BEGIN, MAIL_SMTP_FINISHING, MAIL_DONE } MailState = MAIL_HOME; static SYS_TICK WaitTime; SMTP_POINTERS mySMTPClient; switch(MailState) { case MAIL_HOME: if(SYS_USERIO_ButtonGet((SYS_USERIO_BUTTON_1|SYS_USERIO_BUTTON_2),SYS_USERIO_BUTTON_ASSERTED)) { // Start sending an email SYS_USERIO_SetLED(SYS_USERIO_LED_1, SYS_USERIO_LED_ASSERTED); MailState++; SYS_USERIO_SetLED(SYS_USERIO_LED_2, SYS_USERIO_LED_DEASSERTED); } break; case MAIL_BEGIN: if(SMTPBeginUsage()) { // Note that these strings must stay allocated in // memory until SMTPIsBusy() returns false. To // guarantee that the C compiler does not reuse this // memory, you must allocate the strings as static. static uint8_t RAMStringTo[] = "*****@*****.**"; //static uint8_t RAMStringCC[] = "[email protected], \"Jane Smith\" <*****@*****.**>"; //static uint8_t RAMStringBCC[] = ""; static uint8_t RAMStringBody[] = "Message generated by stack " TCPIP_STACK_VERSION " \r\n\r\nButtons: 3210"; RAMStringBody[sizeof(RAMStringBody)-2] = '0' + BUTTON0_IO; RAMStringBody[sizeof(RAMStringBody)-3] = '0' + BUTTON1_IO; RAMStringBody[sizeof(RAMStringBody)-4] = '0' + BUTTON2_IO; RAMStringBody[sizeof(RAMStringBody)-5] = '0' + BUTTON3_IO; memset(&mySMTPClient, 0, sizeof(mySMTPClient)); mySMTPClient.Server = "mail"; // SMTP server address //mySMTPClient.Username = "******"; //mySMTPClient.Password = "******"; mySMTPClient.To = (char*)RAMStringTo; mySMTPClient.From = "\"SMTP Service\" <*****@*****.**>"; mySMTPClient.Subject = "Hello world! SMTP Test."; mySMTPClient.Body = (char*)RAMStringBody; SMTPSendMail(&mySMTPClient); MailState++; } break; case MAIL_SMTP_FINISHING: if(!SMTPIsBusy()) { // Finished sending mail SYS_USERIO_SetLED (SYS_USERIO_LED_1, SYS_USERIO_LED_DEASSERTED); MailState++; WaitTime = SYS_TICK_Get(); SYS_USERIO_SetLED(SYS_USERIO_LED_2,(SMTPEndUsage() == SMTP_SUCCESS)); } break; case MAIL_DONE: // Wait for the user to release BUTTON2 or BUTTON3 and for at // least 1 second to pass before allowing another // email to be sent. This is merely to prevent // accidental flooding of email boxes while // developing code. Your application may wish to // remove this. if(SYS_USERIO_ButtonGet((SYS_USERIO_BUTTON_1|SYS_USERIO_BUTTON_2),SYS_USERIO_BUTTON_ASSERTED)) { if(SYS_TICK_Get() - WaitTime > SYS_TICK_TicksPerSecondGet()) MailState = MAIL_HOME; } break; } }
void SMTP_Mail_alarm(void) { // Send an email once // the entier message body and all parameters must be in RAM for the duration the // message is beeing sent WORD SMTP_error; char temp[6]; switch (MailState) { case MAIL_HOME: if (Alarms) { strcpy((char*) RAMStringBody, "Alarm # "); if (Alarms & 1 << 1) strcat((char*) RAMStringBody, "1 (SV2:6),"); if (Alarms & 1 << 2) strcat((char*) RAMStringBody, "2 (SV2:7),"); if (Alarms & 1 << 3) strcat((char*) RAMStringBody, "3 (SV2:8,"); if (Alarms & 1 << 4) strcat((char*) RAMStringBody, "4 (SV2:9),"); if (Alarms & 1 << 5) strcat((char*) RAMStringBody, "5 (SV2:10),"); RAMStringBody[strlen((char *) RAMStringBody) - 1] = ' '; // remove trailing coma strcat((char*) RAMStringBody, "is active!\n\r"); Alarms = 0; MailState++; } break; case MAIL_BEGIN: if (SMTPBeginUsage()) { // Note that these strings must stay allocated in // memory until SMTPIsBusy() returns FALSE. To // guarantee that the C compiler does not reuse this // memory, you must allocate the strings as static. // To SMTPClient.To.szRAM = (BYTE *) WX.Mail.SendTo; // From //SMTPClient.From.szROM = (ROM BYTE*)"<*****@*****.**>"; //SMTPClient.From.szROM = (ROM BYTE*)"\"WX_Station\" <*****@*****.**>"; //SMTPClient.ROMPointers.From = 1; // Because of antispam measures we need a valid e-mail address to send mail from, or at least a valid, not black-listed domain" // Since amy "made-up" From-email-address might get black listed in the future I'm using the To-email address also here as well. // since it always should be perfectly legal to send mail to yourself SMTPClient.From.szRAM = (BYTE *) WX.Mail.SendTo; // Using the Send To as SendFrom to use a valid e-mail address // Subject //SMTPClient.Subject.szROM = (ROM BYTE*)"WX Station Alarm"; SMTPClient.Subject.szRAM = (BYTE *) WX.Wunder.StationID; //SMTPClient.ROMPointers.Subject = 1; // Body SMTPClient.Body.szRAM = RAMStringBody; // Server items -- Must use a mail server that is not tied to a IP addressfrom the ISP (like astound, comcast etc) SMTPClient.Server.szRAM = (BYTE *) WX.Mail.Server; // SMTP server address SMTPClient.ServerPort = WX.Mail.port; // SMTPClient.ROMPointers.Server = 1; // only needed if username/passowrd is required by the mail server used SMTPClient.Username.szRAM = (BYTE *) WX.Mail.User_name; //SMTPClient.ROMPointers.Username = 1; SMTPClient.Password.szRAM = (BYTE *) WX.Mail.password; // SMTPClient.ROMPointers.Password = 1; SMTPSendMail(); MailState++; } break; case MAIL_SMTP_FINISHING: if (!SMTPIsBusy()) { // Finished sending mail MailState++; WaitTime = TickGet(); if ( (SMTP_error = SMTPEndUsage()) != 0) { putrsUART("SMTP Error: "); stoa_dec(temp, SMTP_error, 0); putsUART( temp ); putrsUART("\n\r"); } } break; case MAIL_DONE: if (TickGet() > (WaitTime + MAIL_DEADTIME * TICK_SECOND)) // plus a dead time { MailState = MAIL_HOME; } break; } }