00001 /*************************************************************************** 00002 * Copyright (C) 2005-2007 Stefan Schwarzer, Jens Schneider, * 00003 * Matthias Hardt, Guido Madaus * 00004 * * 00005 * Copyright (C) 2007-2008 BerLinux Solutions GbR * 00006 * Stefan Schwarzer & Guido Madaus * 00007 * * 00008 * Copyright (C) 2009-2013 BerLinux Solutions GmbH * 00009 * * 00010 * Authors: * 00011 * Stefan Schwarzer <stefan.schwarzer@diskohq.org>, * 00012 * Matthias Hardt <matthias.hardt@diskohq.org>, * 00013 * Jens Schneider <jens.schneider@diskohq.org>, * 00014 * Guido Madaus <guido.madaus@diskohq.org>, * 00015 * Patrick Helterhoff <patrick.helterhoff@diskohq.org>, * 00016 * René Bählkow <rene.baehlkow@diskohq.org> * 00017 * * 00018 * This library is free software; you can redistribute it and/or * 00019 * modify it under the terms of the GNU Lesser General Public * 00020 * License version 2.1 as published by the Free Software Foundation. * 00021 * * 00022 * This library is distributed in the hope that it will be useful, * 00023 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00024 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00025 * Lesser General Public License for more details. * 00026 * * 00027 * You should have received a copy of the GNU Lesser General Public * 00028 * License along with this library; if not, write to the * 00029 * Free Software Foundation, Inc., * 00030 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * 00031 **************************************************************************/ 00032 00033 #include "mmstools/mmsmail.h" 00034 #ifdef __HAVE_VMIME__ 00035 MMSMail::MMSMail(const std::string _subject, const std::string _returnAddress, const std::string _host) : 00036 subject(_subject), returnAddress(_returnAddress) { 00037 vmime::platform::setHandler<vmime::platforms::posix::posixHandler>(); 00038 setHost(_host); 00039 } 00040 00041 MMSMail::~MMSMail() { 00042 00043 } 00044 00045 std::string MMSMail::getSubject() const { 00046 return this->subject; 00047 } 00048 00049 void MMSMail::setSubject(const std::string subject) { 00050 this->subject = subject; 00051 } 00052 00053 std::string MMSMail::getReturnAddress() const { 00054 return this->returnAddress; 00055 } 00056 00057 void MMSMail::setReturnAddress(std::string returnAddress) { 00058 this->returnAddress = returnAddress; 00059 } 00060 00061 std::string MMSMail::getRecipient(int index) const { 00062 if (!this->recipients.empty() && index < this->recipients.size()) 00063 return this->recipients.at(index); 00064 else 00065 return NULL; 00066 } 00067 00068 const int MMSMail::getRecipientCount() const { 00069 return this->recipients.size(); 00070 } 00071 00072 void MMSMail::addRecipient(std::string recipient) { 00073 this->recipients.push_back(recipient); 00074 } 00075 00076 void MMSMail::removeRecipient(int index) { 00077 this->recipients.erase(this->recipients.begin() + index); 00078 } 00079 00080 std::string MMSMail::getMailBody() const { 00081 return mailBody; 00082 } 00083 00084 void MMSMail::setMailBody(std::string mailBody) { 00085 this->mailBody = mailBody; 00086 } 00087 00088 std::string MMSMail::getHost() const { 00089 return this->host; 00090 } 00091 00092 void MMSMail::setHost(std::string host) { 00093 this->host = host; 00094 00095 std::string hoststring = "smtp://" + host; 00096 vmime::utility::url url(hoststring); 00097 vmime::ref <vmime::net::session> sess = vmime::create <vmime::net::session>(); 00098 this->transportService = sess->getTransport(url); 00099 } 00100 00101 void MMSMail::setAuthData(const std::string &user, const std::string &password) { 00102 this->transportService->setProperty("options.need-authentication", true); 00103 this->transportService->setProperty("auth.username", user); 00104 this->transportService->setProperty("auth.password", password); 00105 } 00106 00107 void MMSMail::send() { 00108 try { 00109 vmime::messageBuilder mb; 00110 mb.setSubject(vmime::text(this->subject)); 00111 mb.setExpeditor(vmime::mailbox(this->returnAddress)); 00112 00113 for (std::vector<std::string>::iterator i = this->recipients.begin(); i != this->recipients.end(); i++) { 00114 mb.getRecipients().appendAddress(vmime::create<vmime::mailbox>(*i)); 00115 } 00116 00117 mb.getTextPart()->setCharset(vmime::charsets::ISO8859_15); 00118 mb.getTextPart()->setText(vmime::create<vmime::stringContentHandler>(this->mailBody)); 00119 vmime::ref<vmime::message> msg = mb.construct(); 00120 00121 this->transportService->connect(); 00122 this->transportService->send(msg); 00123 this->transportService->disconnect(); 00124 } 00125 catch (vmime::exception& e) 00126 { 00127 throw MMSError(0, e.what()); 00128 } 00129 catch (std::exception& e) 00130 { 00131 throw MMSError(0, e.what()); 00132 } 00133 } 00134 #endif