1 | // |
---|---|
2 | // Mail.cpp |
3 | // |
4 | // This sample demonstrates the MailMessage and SMTPClientSession 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/SMTPClientSession.h" |
16 | #include "Poco/Net/StringPartSource.h" |
17 | #include "Poco/Path.h" |
18 | #include "Poco/Exception.h" |
19 | #include <iostream> |
20 | |
21 | |
22 | using Poco::Net::MailMessage; |
23 | using Poco::Net::MailRecipient; |
24 | using Poco::Net::SMTPClientSession; |
25 | using Poco::Net::StringPartSource; |
26 | using Poco::Path; |
27 | using Poco::Exception; |
28 | |
29 | |
30 | const unsigned char PocoLogo[] = |
31 | { |
32 | #include "PocoLogo.hpp" |
33 | }; |
34 | |
35 | |
36 | int main(int argc, char** argv) |
37 | { |
38 | if (argc != 4) |
39 | { |
40 | Path p(argv[0]); |
41 | std::cerr << "usage: "<< p.getBaseName() << " <mailhost> <sender> <recipient>"<< std::endl; |
42 | std::cerr << " Send an email greeting from <sender> to <recipient>,"<< std::endl; |
43 | std::cerr << " using the SMTP server at <mailhost>."<< std::endl; |
44 | return 1; |
45 | } |
46 | |
47 | std::string mailhost(argv[1]); |
48 | std::string sender(argv[2]); |
49 | std::string recipient(argv[3]); |
50 | |
51 | try |
52 | { |
53 | MailMessage message; |
54 | message.setSender(sender); |
55 | message.addRecipient(MailRecipient(MailRecipient::PRIMARY_RECIPIENT, recipient)); |
56 | message.setSubject("Hello from the POCO C++ Libraries"); |
57 | std::string content; |
58 | content += "Hello "; |
59 | content += recipient; |
60 | content += ",\r\n\r\n"; |
61 | content += "This is a greeting from the POCO C++ Libraries.\r\n\r\n"; |
62 | std::string logo(reinterpret_cast<const char*>(PocoLogo), sizeof(PocoLogo)); |
63 | message.addContent(new StringPartSource(content)); |
64 | message.addAttachment("logo", new StringPartSource(logo, "image/gif")); |
65 | |
66 | SMTPClientSession session(mailhost); |
67 | session.login(); |
68 | session.sendMessage(message); |
69 | session.close(); |
70 | } |
71 | catch (Exception& exc) |
72 | { |
73 | std::cerr << exc.displayText() << std::endl; |
74 | return 1; |
75 | } |
76 | return 0; |
77 | } |
78 |