void SendMail::sendMessage(const std::string& content) { try { const Poco::Util::AbstractConfiguration* config = Poco::Util::Application::instance().config().createView("ion.mail"); MailMessage message; message.setSender(config->getString("sender")); message.addRecipient(MailRecipient(MailRecipient::PRIMARY_RECIPIENT, config->getString("recipient"))); message.setSubject(MailMessage::encodeWord(config->getString("subject"), "UTF-8")); message.setContentType("text/plain; charset=UTF-8"); message.addContent(new Poco::Net::StringPartSource(content)); Poco::Net::SocketAddress address(config->getString("host"), config->getInt("port")); Poco::SharedPtr<Poco::Net::StreamSocket> socket(nullptr); if (config->getBool("ssl")) { socket = new Poco::Net::SecureStreamSocket(address); } else { socket = new Poco::Net::StreamSocket(address); } _logger.debug("Connecting to %s", address.toString()); SMTPClientSession session(*socket); session.login(getLoginMethod(config->getString("loginmethod")), config->getString("user"), config->getString("password")); session.sendMessage(message); session.close(); _logger.debug("Message sent"); } catch (Poco::Exception& ex) { _logger.error(ex.displayText()); throw; } }
void MailMessageTest::testWriteMultiPart() { MailMessage message; MailRecipient r1(MailRecipient::PRIMARY_RECIPIENT, "*****@*****.**", "John Doe"); message.addRecipient(r1); message.setSubject("Test Message"); message.setSender("*****@*****.**"); Timestamp ts(0); message.setDate(ts); message.addContent(new StringPartSource("Hello World!\r\n", "text/plain"), MailMessage::ENCODING_8BIT); StringPartSource* pSPS = new StringPartSource("This is some binary data. Really.", "application/octet-stream", "sample.dat"); pSPS->headers().set("Content-ID", "abcd1234"); message.addAttachment("sample", pSPS); assert (message.isMultipart()); std::ostringstream str; message.write(str); std::string s = str.str(); std::string rawMsg( "Date: Thu, 1 Jan 1970 00:00:00 GMT\r\n" "Content-Type: multipart/mixed; boundary=$\r\n" "Subject: Test Message\r\n" "From: [email protected]\r\n" "To: John Doe <*****@*****.**>\r\n" "Mime-Version: 1.0\r\n" "\r\n" "--$\r\n" "Content-Type: text/plain\r\n" "Content-Transfer-Encoding: 8bit\r\n" "Content-Disposition: inline\r\n" "\r\n" "Hello World!\r\n" "\r\n" "--$\r\n" "Content-ID: abcd1234\r\n" "Content-Type: application/octet-stream; name=sample\r\n" "Content-Transfer-Encoding: base64\r\n" "Content-Disposition: attachment; filename=sample.dat\r\n" "\r\n" "VGhpcyBpcyBzb21lIGJpbmFyeSBkYXRhLiBSZWFsbHku\r\n" "--$--\r\n" ); std::string::size_type p1 = s.find('=') + 1; std::string::size_type p2 = s.find('\r', p1); std::string boundary(s, p1, p2 - p1); std::string msg; for (std::string::const_iterator it = rawMsg.begin(); it != rawMsg.end(); ++it) { if (*it == '$') msg += boundary; else msg += *it; } assert (s == msg); }
void SMTPChannel::log(const Message& msg) { try { MailMessage message; message.setSender(_sender); message.addRecipient(MailRecipient(MailRecipient::PRIMARY_RECIPIENT, _recipient)); message.setSubject("Log Message from " + _sender); std::stringstream content; content << "Log Message\r\n" << "===========\r\n\r\n" << "Host: " << Environment::nodeName() << "\r\n" << "Logger: " << msg.getSource() << "\r\n"; if (_local) { DateTime dt(msg.getTime()); content << "Timestamp: " << DateTimeFormatter::format(LocalDateTime(dt), DateTimeFormat::RFC822_FORMAT) << "\r\n"; } else content << "Timestamp: " << DateTimeFormatter::format(msg.getTime(), DateTimeFormat::RFC822_FORMAT) << "\r\n"; content << "Priority: " << NumberFormatter::format(msg.getPriority()) << "\r\n" << "Process ID: " << NumberFormatter::format(msg.getPid()) << "\r\n" << "Thread: " << msg.getThread() << " (ID: " << msg.getTid() << ")\r\n" << "Message text: " << msg.getText() << "\r\n\r\n"; message.addContent(new StringPartSource(content.str())); if (!_attachment.empty()) { { Poco::FileInputStream fis(_attachment, std::ios::in | std::ios::binary | std::ios::ate); if (fis.good()) { int size = fis.tellg(); char* pMem = new char [size]; fis.seekg(std::ios::beg); fis.read(pMem, size); message.addAttachment(_attachment, new StringPartSource(std::string(pMem, size), _type, _attachment)); delete [] pMem; } } if (_delete) File(_attachment).remove(); } SMTPClientSession session(_mailHost); session.login(); session.sendMessage(message); session.close(); } catch (Exception&) { if (_throw) throw; } }
int main(int argc, char** argv) { SSLInitializer sslInitializer; if (argc < 4) { Path p(argv[0]); std::cerr << "usage: " << p.getBaseName() << " <mailhost> <sender> <recipient> [<username> <password>]" << std::endl; std::cerr << " Send an email greeting from <sender> to <recipient>," << std::endl; std::cerr << " using a secure connection to the SMTP server at <mailhost>." << std::endl; return 1; } std::string mailhost(argv[1]); std::string sender(argv[2]); std::string recipient(argv[3]); std::string username(argc >= 5 ? argv[4] : ""); std::string password(argc >= 6 ? argv[5] : ""); try { // Note: we must create the passphrase handler prior Context SharedPtr<InvalidCertificateHandler> pCert = new ConsoleCertificateHandler(false); // ask the user via console Context::Ptr pContext = new Context(Context::CLIENT_USE, "", "", "", Context::VERIFY_RELAXED, 9, true, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"); SSLManager::instance().initializeClient(0, pCert, pContext); MailMessage message; message.setSender(sender); message.addRecipient(MailRecipient(MailRecipient::PRIMARY_RECIPIENT, recipient)); message.setSubject("Hello from the POCO C++ Libraries"); std::string content; content += "Hello "; content += recipient; content += ",\r\n\r\n"; content += "This is a greeting from the POCO C++ Libraries.\r\n\r\n"; std::string logo(reinterpret_cast<const char*>(PocoLogo), sizeof(PocoLogo)); message.addContent(new StringPartSource(content)); message.addAttachment("logo", new StringPartSource(logo, "image/gif")); SecureSMTPClientSession session(mailhost); session.login(); session.startTLS(pContext); if (!username.empty()) { session.login(SMTPClientSession::AUTH_LOGIN, username, password); } session.sendMessage(message); session.close(); } catch (Exception& exc) { std::cerr << exc.displayText() << std::endl; return 1; } return 0; }
int main(int argc, char** argv) { if (argc != 4) { Path p(argv[0]); std::cerr << "usage: " << p.getBaseName() << " <mailhost> <sender> <recipient>" << std::endl; std::cerr << " Send an email greeting from <sender> to <recipient>," << std::endl; std::cerr << " the SMTP server at <mailhost>." << std::endl; return 1; } std::string mailhost(argv[1]); std::string sender(argv[2]); std::string recipient(argv[3]); try { MailMessage message; message.setSender(sender); message.addRecipient(MailRecipient(MailRecipient::PRIMARY_RECIPIENT, recipient)); message.setSubject("Hello from the POCO C++ Libraries"); std::string content; content += "Hello "; content += recipient; content += ",\r\n\r\n"; content += "This is a greeting from the POCO C++ Libraries.\r\n\r\n"; std::string logo(reinterpret_cast<const char*>(PocoLogo), sizeof(PocoLogo)); message.addContent(new StringPartSource(content)); message.addAttachment("logo", new StringPartSource(logo, "image/gif")); SMTPClientSession session(mailhost); session.login(); session.sendMessage(message); session.close(); } catch (Exception& exc) { std::cerr << exc.displayText() << std::endl; return 1; } return 0; }