-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSMTPDemo.c
369 lines (333 loc) · 11.3 KB
/
SMTPDemo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*********************************************************************
*
* SMTP Client Demonstrations
* - SMTP Client (short message)
* - SMTP Client (long message, not enabled)
*
*********************************************************************
* FileName: SMTPDemo.c
* Dependencies: TCP/IP stack
* Processor: PIC18, PIC24F, PIC24H, dsPIC30F, dsPIC33F, PIC32
* Compiler: Microchip C32 v1.05 or higher
* Microchip C30 v3.12 or higher
* Microchip C18 v3.30 or higher
* HI-TECH PICC-18 PRO 9.63PL2 or higher
* Company: Microchip Technology, Inc.
*
* Software License Agreement
*
* Copyright (C) 2002-2009 Microchip Technology Inc. All rights
* reserved.
*
* Microchip licenses to you the right to use, modify, copy, and
* distribute:
* (i) the Software when embedded on a Microchip microcontroller or
* digital signal controller product ("Device") which is
* integrated into Licensee's product; or
* (ii) ONLY the Software driver source files ENC28J60.c, ENC28J60.h,
* ENCX24J600.c and ENCX24J600.h ported to a non-Microchip device
* used in conjunction with a Microchip ethernet controller for
* the sole purpose of interfacing with the ethernet controller.
*
* You should refer to the license agreement accompanying this
* Software for additional information regarding your rights and
* obligations.
*
* THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT
* WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
* LIMITATION, ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
* MICROCHIP BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF
* PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
* BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE
* THEREOF), ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER
* SIMILAR COSTS, WHETHER ASSERTED ON THE BASIS OF CONTRACT, TORT
* (INCLUDING NEGLIGENCE), BREACH OF WARRANTY, OR OTHERWISE.
*
*
* Author Date Comment
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* E. Wood 4/26/08 Moved from MainDemo.c
********************************************************************/
#define __SMTPDEMO_C
#include "TCPIPConfig.h"
#if defined(STACK_USE_SMTP_CLIENT)
#include "TCPIP Stack/TCPIP.h"
#include "MainDemo.h"
/*****************************************************************************
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[] = "[email protected]";
//static BYTE RAMStringCC[] = "[email protected], \"Jane Smith\" <[email protected]>";
//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\" <[email protected]>";
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 SMTPDemo(void)
{
// Send an email once if someone pushes BUTTON2 and BUTTON3 simultaneously
// This is a multi-part message example, where the message
// body is dynamically generated and need not fit 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_PUT_DATA,
MAIL_PUT_DATA2,
MAIL_SMTP_FINISHING,
MAIL_DONE
} MailState = MAIL_HOME;
static BYTE *MemPtr;
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[] = "[email protected]";
//static BYTE RAMStringCC[] = "[email protected], \"Jane Smith\" <[email protected]>";
SMTPClient.Server.szROM = "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.CC.szRAM = RAMStringCC;
SMTPClient.From.szROM = (ROM BYTE*)"\"SMTP Service\" <[email protected]>";
SMTPClient.ROMPointers.From = 1;
SMTPClient.Subject.szROM = (ROM BYTE*)"Hello world! SMTP Test.";
SMTPClient.ROMPointers.Subject = 1;
SMTPSendMail();
MailState++;
}
break;
case MAIL_PUT_DATA:
// Check to see if a failure occured
if(!SMTPIsBusy())
{
// Finished sending mail
LED1_IO = 0;
MailState = MAIL_DONE;
WaitTime = TickGet();
LED2_IO = (SMTPEndUsage() == SMTP_SUCCESS);
break;
}
if(SMTPIsPutReady() >= 121u)
{
SMTPPutROMString((ROM BYTE*)"Hello!\r\n\r\nThis mail was automatically generated by Microchip TCP/IP Stack " TCPIP_STACK_VERSION ".\r\n\r\nThe following is a snapshot of RAM:\r\n");
SMTPFlush();
MemPtr = 0x0000;
MailState++;
}
break;
case MAIL_PUT_DATA2:
// Check to see if a failure occured
if(!SMTPIsBusy())
{
// Finished sending mail
LED1_IO = 0;
MailState = MAIL_DONE;
WaitTime = TickGet();
LED2_IO = (SMTPEndUsage() == SMTP_SUCCESS);
break;
}
if(SMTPIsPutReady() >= 75u)
{
BYTE i, c;
WORD_VAL w;
// Write line address
w.Val = (WORD)MemPtr;
SMTPPut(btohexa_high(w.v[1]));
SMTPPut(btohexa_low(w.v[1]));
SMTPPut(btohexa_high(w.v[0]));
SMTPPut(btohexa_low(w.v[0]));
SMTPPut(' ');
// Write data bytes in hex
for(i = 0; i < 16u; i++)
{
SMTPPut(' ');
#if defined(__C32__) // PIC32 has memory protection, so you can't just read from any old address
c = 'R';
MemPtr++;
#else
c = *MemPtr++;
#endif
SMTPPut(btohexa_high(c));
SMTPPut(btohexa_low(c));
if(i == 7u)
SMTPPut(' ');
}
SMTPPut(' ');
SMTPPut(' ');
// Write data bytes in ASCII
MemPtr -= 16;
for(i = 0; i < 16u; i++)
{
#if defined(__C32__) // PIC32 has memory protection, so you can't just read from any old address
c = 'R';
MemPtr++;
#else
c = *MemPtr++;
#endif
if(c < ' ' || c > '~')
c = '.';
SMTPPut(c);
if(i == 7u)
SMTPPut(' ');
}
SMTPPut('\r');
SMTPPut('\n');
SMTPFlush();
// Make sure not to read from memory above address 0x0E7F.
// Doing so would disrupt volatile pointers, ERDPT, FSR0, FSR1, FSR2, etc.
if((WORD)MemPtr >= 0xE7Fu)
{
SMTPPutDone();
MailState++;
}
}
break;
case MAIL_SMTP_FINISHING:
// Check to see if we are done communicating with the SMTP server
if(!SMTPIsBusy())
{
// Finished sending mail
LED1_IO = 0;
MailState = MAIL_DONE;
WaitTime = TickGet();
LED2_IO = (SMTPEndUsage() == SMTP_SUCCESS);
}
break;
case MAIL_DONE:
// Wait for the user to release BUTTON2 or BUTTON3 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;
}
}
*/
#endif //#if defined(STACK_USE_SMTP_CLIENT)