1//
2// Mail.cpp
3//
4// This sample demonstrates the SMTPChannel class.
5//
6// Copyright (c) 2005-2006, 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/SMTPChannel.h"
15#include "Poco/Logger.h"
16#include "Poco/Path.h"
17#include "Poco/AutoPtr.h"
18#include "Poco/Exception.h"
19#include <iostream>
20
21
22using Poco::Net::SMTPChannel;
23using Poco::Logger;
24using Poco::Path;
25using Poco::AutoPtr;
26using Poco::Exception;
27
28
29#if defined(POCO_OS_FAMILY_UNIX)
30const std::string fileName = "${POCO_BASE}/Net/samples/SMTPLogger/res/logo.gif";
31#elif defined(POCO_OS_FAMILY_WINDOWS)
32const std::string fileName = "%POCO_BASE%/Net/samples/SMTPLogger/res/logo.gif";
33#endif
34
35
36int 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 log entry from <sender> to <recipient>," << std::endl;
43 std::cerr << " 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 std::string attachment = Path::expand(fileName);
51
52 try
53 {
54 AutoPtr<SMTPChannel> pSMTPChannel = new SMTPChannel(mailhost, sender, recipient);
55 pSMTPChannel->setProperty("attachment", attachment);
56 pSMTPChannel->setProperty("type", "image/gif");
57 pSMTPChannel->setProperty("delete", "false");
58 pSMTPChannel->setProperty("throw", "true");
59
60 Logger& logger = Logger::get("POCO Sample SMTP Logger");
61 logger.setChannel(pSMTPChannel.get());
62 logger.critical("Critical message");
63 }
64 catch (Exception& exc)
65 {
66 std::cerr << exc.displayText() << std::endl;
67 return 1;
68 }
69 return 0;
70}
71