SMTP client (send mail) GSwifi see: http://mbed.org/users/gsfan/notebook/gainspan_wifi/

Dependencies:   GSwifi mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "GSwifi.h"
00003 
00004 #define SERVER  "mail.example.com"
00005 #define PORT    587   // or 25
00006 #define TO      "to@example.com"
00007 #define FROM    "from@example.com"
00008 #define SUBJECT "TEST"
00009 #define MESG    "Hello!\r\n"
00010 #define USER    "username"  // SMTP Auth
00011 #define PWD     "password"  // SMTP Auth
00012 
00013 #define SECURE GSwifi::GSSEC_WPA_PSK
00014 #define SSID "SSID"
00015 #define PASS "PASSPHRASE"
00016 
00017 GSwifi gs(p13, p14, p20); // TX, RX, Reset (no flow control)
00018 //GSwifi gs(p13, p14, p12, P0_22, p20, NC, 115200); // TX, RX, CTS, RTS, Reset, Alarm
00019 
00020 Serial pc(USBTX, USBRX);
00021 DigitalOut led1(LED1), led2(LED2);
00022 
00023 int main () {
00024     IpAddr ipaddr, netmask, gateway, nameserver;
00025     Host host;
00026     int r;
00027 
00028     led1 = 1;
00029     pc.baud(115200);
00030 
00031     pc.printf("connecting...\r\n");
00032     if (gs.connect(SECURE, SSID, PASS)) {
00033         return -1;
00034     }
00035     gs.getAddress(ipaddr, netmask, gateway, nameserver);
00036     pc.printf("ip %d.%d.%d.%d\r\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]);
00037 
00038     led2 = 1;
00039     pc.printf("sendmail\r\n");
00040     host.setName(SERVER);
00041     host.setPort(PORT);
00042 //    r = gs.mail(host, TO, FROM, SUBJECT, MESG);  // no auth
00043     r = gs.mail(host, TO, FROM, SUBJECT, MESG, USER, PWD);  // SMTP auth
00044     if (r == 0) {
00045         pc.printf("sent\r\n");
00046     } else {
00047         pc.printf("error\r\n");
00048     }
00049 
00050     for (;;) {
00051         gs.poll();
00052 
00053         wait_ms(50);
00054         led1 = !led1;
00055         led2 = 0;
00056     }
00057 }