1 | // |
2 | // Mail.cpp |
3 | // |
4 | // This sample demonstrates the MailMessage and SecureSMTPClientSession classes. |
5 | // |
6 | // Copyright (c) 2005-2011, Applied Informatics Software Engineering GmbH. |
7 | // and Contributors. |
8 | // |
9 | // SPDX-License-Identifier: BSL-1.0 |
10 | // |
11 | |
12 | |
13 | #include "Poco/Net/MailMessage.h" |
14 | #include "Poco/Net/MailRecipient.h" |
15 | #include "Poco/Net/SecureSMTPClientSession.h" |
16 | #include "Poco/Net/StringPartSource.h" |
17 | #include "Poco/Net/SSLManager.h" |
18 | #include "Poco/Net/KeyConsoleHandler.h" |
19 | #include "Poco/Net/ConsoleCertificateHandler.h" |
20 | #include "Poco/SharedPtr.h" |
21 | #include "Poco/Path.h" |
22 | #include "Poco/Exception.h" |
23 | #include <iostream> |
24 | |
25 | |
26 | using Poco::Net::MailMessage; |
27 | using Poco::Net::MailRecipient; |
28 | using Poco::Net::SMTPClientSession; |
29 | using Poco::Net::SecureSMTPClientSession; |
30 | using Poco::Net::StringPartSource; |
31 | using Poco::Net::SSLManager; |
32 | using Poco::Net::Context; |
33 | using Poco::Net::KeyConsoleHandler; |
34 | using Poco::Net::PrivateKeyPassphraseHandler; |
35 | using Poco::Net::InvalidCertificateHandler; |
36 | using Poco::Net::ConsoleCertificateHandler; |
37 | using Poco::SharedPtr; |
38 | using Poco::Path; |
39 | using Poco::Exception; |
40 | |
41 | |
42 | class SSLInitializer |
43 | { |
44 | public: |
45 | SSLInitializer() |
46 | { |
47 | Poco::Net::initializeSSL(); |
48 | } |
49 | |
50 | ~SSLInitializer() |
51 | { |
52 | Poco::Net::uninitializeSSL(); |
53 | } |
54 | }; |
55 | |
56 | |
57 | const unsigned char PocoLogo[] = |
58 | { |
59 | #include "PocoLogo.hpp" |
60 | }; |
61 | |
62 | |
63 | int main(int argc, char** argv) |
64 | { |
65 | SSLInitializer sslInitializer; |
66 | |
67 | if (argc < 4) |
68 | { |
69 | Path p(argv[0]); |
70 | std::cerr << "usage: " << p.getBaseName() << " <mailhost> <sender> <recipient> [<username> <password>]" << std::endl; |
71 | std::cerr << " Send an email greeting from <sender> to <recipient>," << std::endl; |
72 | std::cerr << " using a secure connection to the SMTP server at <mailhost>." << std::endl; |
73 | return 1; |
74 | } |
75 | |
76 | std::string mailhost(argv[1]); |
77 | std::string sender(argv[2]); |
78 | std::string recipient(argv[3]); |
79 | std::string username(argc >= 5 ? argv[4] : "" ); |
80 | std::string password(argc >= 6 ? argv[5] : "" ); |
81 | |
82 | try |
83 | { |
84 | // Note: we must create the passphrase handler prior Context |
85 | SharedPtr<InvalidCertificateHandler> pCert = new ConsoleCertificateHandler(false); // ask the user via console |
86 | Context::Ptr pContext = new Context(Context::CLIENT_USE, "" , "" , "" , Context::VERIFY_RELAXED, 9, true, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH" ); |
87 | SSLManager::instance().initializeClient(0, pCert, pContext); |
88 | |
89 | MailMessage message; |
90 | message.setSender(sender); |
91 | message.addRecipient(MailRecipient(MailRecipient::PRIMARY_RECIPIENT, recipient)); |
92 | message.setSubject("Hello from the POCO C++ Libraries" ); |
93 | std::string content; |
94 | content += "Hello " ; |
95 | content += recipient; |
96 | content += ",\r\n\r\n" ; |
97 | content += "This is a greeting from the POCO C++ Libraries.\r\n\r\n" ; |
98 | std::string logo(reinterpret_cast<const char*>(PocoLogo), sizeof(PocoLogo)); |
99 | message.addContent(new StringPartSource(content)); |
100 | message.addAttachment("logo" , new StringPartSource(logo, "image/gif" )); |
101 | |
102 | SecureSMTPClientSession session(mailhost); |
103 | session.login(); |
104 | session.startTLS(pContext); |
105 | if (!username.empty()) |
106 | { |
107 | session.login(SMTPClientSession::AUTH_LOGIN, username, password); |
108 | } |
109 | session.sendMessage(message); |
110 | session.close(); |
111 | } |
112 | catch (Exception& exc) |
113 | { |
114 | std::cerr << exc.displayText() << std::endl; |
115 | return 1; |
116 | } |
117 | return 0; |
118 | } |
119 | |