bitmailer.cpp.svn-base

来自「这是一个很好的参考代码」· SVN-BASE 代码 · 共 188 行

SVN-BASE
188
字号
/*************************************************************************** *   Copyright (C) 2006 by Frank Fuhrmann                                  * *   bitmuncher@blhg.org                                                   * *                                                                         * *   This program is free software; you can redistribute it and/or modify  * *   it under the terms of the GNU General Public License as published by  * *   the Free Software Foundation; either version 2 of the License, or     * *   (at your option) any later version.                                   * *                                                                         * *   This program is distributed in the hope that it will be useful,       * *   but WITHOUT ANY WARRANTY; without even the implied warranty of        * *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         * *   GNU General Public License for more details.                          * *                                                                         * *   You should have received a copy of the GNU General Public License     * *   along with this program; if not, write to the                         * *   Free Software Foundation, Inc.,                                       * *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             * ***************************************************************************/#include <iostream>#include <string>#include <fstream>#include "mailer.h"/* show a warning message */void warning(std::string msg){  std::cout << "Warning: " << msg << std::endl;}/* print usage informations */void usage(char *prog){  std::cout << "Usage: " << prog << " <options>" << std::endl	    << "Available Options:" << std::endl	    << "   -t <to>          - recipient (needed)" << std::endl	    << "   -f <from>        - sender (needed)" << std::endl	    << "   -s <subject>     - subject" << std::endl	    << "   -m <message>     - message" << std::endl	    << "   -a <attachment>  - attachment" << std::endl	    << "   -S <server>      - SMTP server" << std::endl	    << "   -U <user>        - username for SMTP auth" << std::endl	    << "   -P <password>    - password for SMTP auth" << std::endl	    << "   -F <file>        - read message from 'file'" << std::endl	    << "   -h/-?            - this screen" << std::endl;}int main(int argc, char *argv[]){  char *progname = argv[0];  char *subject = "<no subject>";  char *message = "No message!";  char *server = "localhost";  std::string mfilename = ""; // name of our message file  extern char *optarg;  int ch;  char *to, *from, *user, *password, *attachment;  if(argc == 1) {    usage(progname);    return 1;  }  /* parse arguments */  while((ch = getopt(argc, argv, "t:f:s:m:S:U:P:F:h?")) != -1) {    switch(ch) {    case 't':      to = optarg;#ifdef DEBUG      std::cout << "TO: " << to << std::endl;#endif      break;    case 'f':      from = optarg;#ifdef DEBUG      std::cout << "FROM: " << from << std::endl;#endif      break;    case 's':      subject = optarg;#ifdef DEBUG      std::cout << "SUBJECT: " << subject << std::endl;#endif      break;    case 'm':      message = optarg;#ifdef DEBUG      std::cout << "MESSAGE: " << message << std::endl;#endif      break;    case 'a':      attachment = optarg;#ifdef DEBUG      std::cout << "ATTACHMENT: " << attachment << std::endl;#endif      break;    case 'S':      server = optarg;#ifdef DEBUG      std::cout << "SERVER: " << server << std::endl;#endif      break;    case 'U':      user = optarg;#ifdef DEBUG      std::cout << "USER: " << user << std::endl;#endif      break;    case 'P':      password = optarg;#ifdef DEBUG      std::cout << "PASSWORD: " << password << std::endl;#endif      break;    case 'F': // message file      mfilename = optarg;      break;    case 'h':    case '?':      usage(progname);      return 0;      break; // don't need this, normally    }  }  /* do we have at least to and from adresses */  if(!to || !from) { // don't have from or to, so print usage information    usage(progname);    return 1;  }    /* print warning, if we don't have a message */  if(!strcmp(message, "No message!")) {    if(mfilename == "") {      warning("Sending without any message");    } else { // use a message file      std::ifstream file(mfilename.c_str(), std::ios::binary | std::ios::in);      if(!file) {	std::cerr << "Couldn't open message file. Abort!" << std::endl;	return 1;      }      std::string filedata;      /* read file data */      char c = file.get();      for( ; file.good(); c=file.get()) {	if(file.bad()) {	  break;	}	filedata.push_back(c);      }      strcpy(message, filedata.c_str());    }  }  if(!strcmp(subject, "<no subject>")) {    warning("No subject");  }  if(!user || user == "") {    warning("no authentification used");    user = "";  }  if(!server || server == "") {    warning("no smtp server given, assuming localhost");  }  Mailer m(to, from, subject, message, server, Mailer::SMTP_PORT, false);    /* for authentification */  user = (char *)malloc(sizeof(user));      m.username(user);  m.password(password);  /* add attachmnent */  m.attach(attachment);    /* sending mail */  m.send();  /* print response of smtp server */  std::cout << m.response() << std::endl;  return 0;} 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?