smtp_client_example.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*************************************************
  2. Copyright (c) 2019
  3. All rights reserved.
  4. File name: smtp_client_example.c
  5. Description: smtp发送邮件示例邮件
  6. History:
  7. 1. Version: V1.0.0
  8. Date: 2019-10-14
  9. Author: wangjunjie
  10. Modify:
  11. 2. Version: V1.0.1
  12. Date: 2019-10-14
  13. Author: wangjunjie
  14. Modify: 添加多收件人功能
  15. 3. Version: V1.0.2
  16. Date: 2020-06-22
  17. Author: WKJay
  18. Modify: 增加附件功能
  19. *************************************************/
  20. #include "smtp_client.h"
  21. #include "rtthread.h"
  22. //若使用TLS加密则需要更大的堆栈空间
  23. #ifdef SMTP_CLIENT_USING_TLS
  24. #define SMTP_CLIENT_THREAD_STACK_SIZE 8192
  25. #else
  26. #define SMTP_CLIENT_THREAD_STACK_SIZE 4096
  27. #endif
  28. #define DBG_ENABLE
  29. #define DBG_LEVEL 3
  30. #define DBG_COLOR
  31. #define DBG_SECTION_NAME "SMTP_EXAMPLE"
  32. #include "rtdbg.h"
  33. /*
  34. *邮件信息相关宏定义
  35. */
  36. //smtp 服务器域名
  37. #define SMTP_SERVER_ADDR "smtp.163.com"
  38. //smtp 服务器端口号
  39. #define SMTP_SERVER_PORT "25"
  40. //smtp 登录用户名
  41. #define SMTP_USERNAME ""
  42. //smtp 登录密码(或凭证)
  43. #define SMTP_PASSWORD ""
  44. //邮件主题
  45. #define SMTP_SUBJECT "SMTP TEST"
  46. //邮件内容
  47. char *content = "THIS IS SMTP TEST\r\n"
  48. "HELLO SMTP\r\n"
  49. "--------------------------------------\r\n"
  50. "based on ---> RT-Thread\r\n"
  51. "based on ---> SMTP_CLIENT\r\n";
  52. uint8_t send_enable = 0;
  53. void smtp_thread(void *param)
  54. {
  55. //初始化smtp客户端
  56. smtp_client_init();
  57. //设置服务器地址
  58. smtp_set_server_addr(SMTP_SERVER_ADDR, ADDRESS_TYPE_DOMAIN, SMTP_SERVER_PORT);
  59. //设置服务器认证信息
  60. smtp_set_auth(SMTP_USERNAME, SMTP_PASSWORD);
  61. //添加收件人1
  62. smtp_add_receiver("66666@sharklasers.com");
  63. while (1)
  64. {
  65. if (send_enable)
  66. {
  67. smtp_add_attachment("/a.txt", "a.txt");
  68. smtp_add_attachment("/b.txt", "b.txt");
  69. //发送邮件
  70. LOG_D("start to send mail");
  71. if (smtp_send_mail(SMTP_SUBJECT, content) == 0)
  72. {
  73. //发送成功
  74. LOG_I("send mail success!");
  75. }
  76. else
  77. {
  78. //发送失败
  79. LOG_E("send mail fail!");
  80. }
  81. //清除附件
  82. smtp_clear_attachments();
  83. //防止频繁发送
  84. rt_thread_mdelay(30000);
  85. send_enable = 0;
  86. }
  87. else
  88. {
  89. rt_thread_mdelay(500);
  90. }
  91. }
  92. }
  93. int smtp_thread_entry(void)
  94. {
  95. rt_thread_t smtp_client_tid;
  96. //创建邮件发送线程(如果选择在主函数中直接调用邮件发送函数,需要注意主函数堆栈大小,必要时调大)
  97. smtp_client_tid = rt_thread_create("smtp", smtp_thread, RT_NULL, SMTP_CLIENT_THREAD_STACK_SIZE, 20, 5);
  98. if (smtp_client_tid != RT_NULL)
  99. {
  100. rt_thread_startup(smtp_client_tid);
  101. }
  102. return RT_EOK;
  103. }
  104. INIT_APP_EXPORT(smtp_thread_entry);
  105. int smtp_test(uint8_t argc, char *argv[])
  106. {
  107. send_enable = 1;
  108. return 0;
  109. }
  110. MSH_CMD_EXPORT(smtp_test, smtp test);