|
| 1 | +/* |
| 2 | +* Copyright (c) <2002-2005> <Jean-Philippe Barrette-LaPierre> |
| 3 | +* |
| 4 | +* Permission is hereby granted, free of charge, to any person obtaining |
| 5 | +* a copy of this software and associated documentation files |
| 6 | +* (curlpp), to deal in the Software without restriction, |
| 7 | +* including without limitation the rights to use, copy, modify, merge, |
| 8 | +* publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | +* and to permit persons to whom the Software is furnished to do so, |
| 10 | +* subject to the following conditions: |
| 11 | +* |
| 12 | +* The above copyright notice and this permission notice shall be included |
| 13 | +* in all copies or substantial portions of the Software. |
| 14 | +* |
| 15 | +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 16 | +* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | +* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | +* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | +* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 | +* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | +*/ |
| 23 | + |
| 24 | +/** |
| 25 | +* \file |
| 26 | +* Send e-mail with SMTP. |
| 27 | +*/ |
| 28 | + |
| 29 | + |
| 30 | +#include <cstdlib> |
| 31 | +#include <cstdio> |
| 32 | +#include <cstring> |
| 33 | +#include <sstream> |
| 34 | + |
| 35 | +#include <curlpp/cURLpp.hpp> |
| 36 | +#include <curlpp/Easy.hpp> |
| 37 | +#include <curlpp/Options.hpp> |
| 38 | +#include <curlpp/Exception.hpp> |
| 39 | + |
| 40 | +/* |
| 41 | + anonymous namespace to prevent name clash in case other examples using the same global entities |
| 42 | + would be compiled in the same project |
| 43 | +*/ |
| 44 | +namespace |
| 45 | +{ |
| 46 | + |
| 47 | +// Change those to appropriate values: |
| 48 | +const std::string smtpUrl = "smtp://mail.example.com"; |
| 49 | +const std::string fromAddress = "<[email protected]>"; |
| 50 | +const std::string toAddress = "<[email protected]>"; |
| 51 | +const std::string ccAddress = "<[email protected]>"; |
| 52 | +const std::string fromMail = "Sender Person " + fromAddress; |
| 53 | +const std::string toMail = "A Receiver " + toAddress; |
| 54 | +const std::string ccMail = "John CC Smith " + ccAddress; |
| 55 | +const std::string mailDate = "Wed, 28 Aug 2019 16:26:00 +0300"; |
| 56 | +const std::string mailSubject = "SMTP example message"; |
| 57 | +const std::string mailText = "Hi there,\r\n\r\nThis is a message from curlpp!\r\n"; |
| 58 | + |
| 59 | +} // namespace |
| 60 | + |
| 61 | +int main(int argc, char *argv[]) |
| 62 | +{ |
| 63 | + std::stringstream dataStream; |
| 64 | + dataStream |
| 65 | + << "Date: " << mailDate << "\r\n" |
| 66 | + << "To: " << toMail << "\r\n" |
| 67 | + << "From: " << fromMail << "\r\n" |
| 68 | + << "Cc: " << ccMail << "\r\n" |
| 69 | + << "Subject: " << mailSubject << "\r\n" |
| 70 | + << "\r\n" |
| 71 | + << mailText; |
| 72 | + long size = dataStream.str().size(); |
| 73 | + |
| 74 | + std::list<std::string> recipients; |
| 75 | + recipients.push_back(toAddress); |
| 76 | + recipients.push_back(ccAddress); |
| 77 | + |
| 78 | + try |
| 79 | + { |
| 80 | + curlpp::Cleanup cleaner; |
| 81 | + curlpp::Easy request; |
| 82 | + |
| 83 | + using namespace curlpp::Options; |
| 84 | + request.setOpt(new Verbose(true)); |
| 85 | + request.setOpt(new Url(smtpUrl)); |
| 86 | + request.setOpt(new MailFrom(fromAddress)); |
| 87 | + request.setOpt(new MailRcpt(recipients)); |
| 88 | + request.setOpt(new ReadStream(&dataStream)); |
| 89 | + request.setOpt(new InfileSize(size)); |
| 90 | + request.setOpt(new Upload(true)); |
| 91 | + |
| 92 | + request.perform(); |
| 93 | + } |
| 94 | + catch (curlpp::LogicError & e) |
| 95 | + { |
| 96 | + std::cout << e.what() << std::endl; |
| 97 | + } |
| 98 | + catch (curlpp::RuntimeError & e) |
| 99 | + { |
| 100 | + std::cout << e.what() << std::endl; |
| 101 | + } |
| 102 | + |
| 103 | + return 0; |
| 104 | +} |
0 commit comments