SMTPChannel.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //
  2. // SMTPChannel.cpp
  3. //
  4. // Library: Net
  5. // Package: Logging
  6. // Module: SMTPChannel
  7. //
  8. // Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
  9. // and Contributors.
  10. //
  11. // SPDX-License-Identifier: BSL-1.0
  12. //
  13. #include "Poco/Net/SMTPChannel.h"
  14. #include "Poco/Net/MailMessage.h"
  15. #include "Poco/Net/MailRecipient.h"
  16. #include "Poco/Net/SMTPClientSession.h"
  17. #include "Poco/Net/StringPartSource.h"
  18. #include "Poco/Message.h"
  19. #include "Poco/DateTimeFormatter.h"
  20. #include "Poco/DateTimeFormat.h"
  21. #include "Poco/LocalDateTime.h"
  22. #include "Poco/LoggingFactory.h"
  23. #include "Poco/Instantiator.h"
  24. #include "Poco/NumberFormatter.h"
  25. #include "Poco/FileStream.h"
  26. #include "Poco/File.h"
  27. #include "Poco/Environment.h"
  28. namespace Poco {
  29. namespace Net {
  30. const std::string SMTPChannel::PROP_MAILHOST("mailhost");
  31. const std::string SMTPChannel::PROP_SENDER("sender");
  32. const std::string SMTPChannel::PROP_RECIPIENT("recipient");
  33. const std::string SMTPChannel::PROP_LOCAL("local");
  34. const std::string SMTPChannel::PROP_ATTACHMENT("attachment");
  35. const std::string SMTPChannel::PROP_TYPE("type");
  36. const std::string SMTPChannel::PROP_DELETE("delete");
  37. const std::string SMTPChannel::PROP_THROW("throw");
  38. SMTPChannel::SMTPChannel():
  39. _mailHost("localhost"),
  40. _local(true),
  41. _type("text/plain"),
  42. _delete(false),
  43. _throw(false)
  44. {
  45. }
  46. SMTPChannel::SMTPChannel(const std::string& mailhost, const std::string& sender, const std::string& recipient):
  47. _mailHost(mailhost),
  48. _sender(sender),
  49. _recipient(recipient),
  50. _local(true),
  51. _type("text/plain"),
  52. _delete(false),
  53. _throw(false)
  54. {
  55. }
  56. SMTPChannel::~SMTPChannel()
  57. {
  58. try
  59. {
  60. close();
  61. }
  62. catch (...)
  63. {
  64. poco_unexpected();
  65. }
  66. }
  67. void SMTPChannel::open()
  68. {
  69. }
  70. void SMTPChannel::close()
  71. {
  72. }
  73. void SMTPChannel::log(const Message& msg)
  74. {
  75. try
  76. {
  77. MailMessage message;
  78. message.setSender(_sender);
  79. message.addRecipient(MailRecipient(MailRecipient::PRIMARY_RECIPIENT, _recipient));
  80. message.setSubject("Log Message from " + _sender);
  81. std::stringstream content;
  82. content << "Log Message\r\n"
  83. << "===========\r\n\r\n"
  84. << "Host: " << Environment::nodeName() << "\r\n"
  85. << "Logger: " << msg.getSource() << "\r\n";
  86. if (_local)
  87. {
  88. DateTime dt(msg.getTime());
  89. content << "Timestamp: " << DateTimeFormatter::format(LocalDateTime(dt), DateTimeFormat::RFC822_FORMAT) << "\r\n";
  90. }
  91. else
  92. content << "Timestamp: " << DateTimeFormatter::format(msg.getTime(), DateTimeFormat::RFC822_FORMAT) << "\r\n";
  93. content << "Priority: " << NumberFormatter::format(msg.getPriority()) << "\r\n"
  94. << "Process ID: " << NumberFormatter::format(msg.getPid()) << "\r\n"
  95. << "Thread: " << msg.getThread() << " (ID: " << msg.getTid() << ")\r\n"
  96. << "Message text: " << msg.getText() << "\r\n\r\n";
  97. message.addContent(new StringPartSource(content.str()));
  98. if (!_attachment.empty())
  99. {
  100. {
  101. Poco::FileInputStream fis(_attachment, std::ios::in | std::ios::binary | std::ios::ate);
  102. if (fis.good())
  103. {
  104. typedef std::allocator<std::string::value_type>::size_type SST;
  105. std::streamoff size = fis.tellg();
  106. poco_assert (std::numeric_limits<unsigned int>::max() >= size);
  107. poco_assert (std::numeric_limits<SST>::max() >= size);
  108. char* pMem = new char [static_cast<unsigned int>(size)];
  109. fis.seekg(std::ios::beg);
  110. fis.read(pMem, size);
  111. message.addAttachment(_attachment,
  112. new StringPartSource(std::string(pMem, static_cast<SST>(size)),
  113. _type,
  114. _attachment));
  115. delete [] pMem;
  116. }
  117. }
  118. if (_delete) File(_attachment).remove();
  119. }
  120. SMTPClientSession session(_mailHost);
  121. session.login();
  122. session.sendMessage(message);
  123. session.close();
  124. }
  125. catch (Exception&)
  126. {
  127. if (_throw) throw;
  128. }
  129. }
  130. void SMTPChannel::setProperty(const std::string& name, const std::string& value)
  131. {
  132. if (name == PROP_MAILHOST)
  133. _mailHost = value;
  134. else if (name == PROP_SENDER)
  135. _sender = value;
  136. else if (name == PROP_RECIPIENT)
  137. _recipient = value;
  138. else if (name == PROP_LOCAL)
  139. _local = isTrue(value);
  140. else if (name == PROP_ATTACHMENT)
  141. _attachment = value;
  142. else if (name == PROP_TYPE)
  143. _type = value;
  144. else if (name == PROP_DELETE)
  145. _delete = isTrue(value);
  146. else if (name == PROP_THROW)
  147. _throw = isTrue(value);
  148. else
  149. Channel::setProperty(name, value);
  150. }
  151. std::string SMTPChannel::getProperty(const std::string& name) const
  152. {
  153. if (name == PROP_MAILHOST)
  154. return _mailHost;
  155. else if (name == PROP_SENDER)
  156. return _sender;
  157. else if (name == PROP_RECIPIENT)
  158. return _recipient;
  159. else if (name == PROP_LOCAL)
  160. return _local ? "true" : "false";
  161. else if (name == PROP_ATTACHMENT)
  162. return _attachment;
  163. else if (name == PROP_TYPE)
  164. return _type;
  165. else if (name == PROP_DELETE)
  166. return _delete ? "true" : "false";
  167. else if (name == PROP_THROW)
  168. return _throw ? "true" : "false";
  169. else
  170. return Channel::getProperty(name);
  171. }
  172. void SMTPChannel::registerChannel()
  173. {
  174. Poco::LoggingFactory::defaultFactory().registerChannelClass("SMTPChannel",
  175. new Poco::Instantiator<SMTPChannel, Poco::Channel>);
  176. }
  177. } } // namespace Poco::Net