一个发送邮件的C++库–jwsmtp

简介: 接收邮件的,暂时没搞定,不过在SF上找到了这个发送邮件的库文件.它提供了一个比较完整的类,可以简单的调用发送邮件.下面是作者提供的一个例子,不过由于连SMTP发邮件需要密码,所以代码我改了一下.// This file is part of the jwSMTP library.

接收邮件的,暂时没搞定,不过在SF上找到了这个发送邮件的库文件.它提供了一个比较完整的类,可以简单的调用发送邮件.下面是作者提供的一个例子,不过由于连SMTP发邮件需要密码,所以代码我改了一下.
// This file is part of the jwSMTP library. 
// 
//    jwSMTP library 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. 
// 
//    jwSMTP library 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 jwSMTP library; if not, write to the Free Software 
//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA    02111-1307    USA 
// 
//      http://johnwiggins.net 
//      smtplib@johnwiggins.net 
// 
// http://www.boost.org 
//#include <boost\thread\thread.hpp> 
 
#include <iostream> 
#include “jwsmtp/jwsmtp.h” 
 
using std::cout; 
using std::cin; 
using std::string; 
 
void Usage() { 
   cout << “jwSMTP library demo program\n” 
           “maildemo <email toaddress> <email fromaddress> <smtpserver>\n” 
           “   e.g.\n” 
           “      maildemo recipient@there.com me@server.com mail.server.com\n”; 

 
int main(int argc, char* argv[]) 

   if(argc != 4) { 
      Usage(); 
      return 0; 
   } 
 
   cout << “jwSMTP library demo program\n\n”; 
   string to(argv[1]); 
   string from(argv[2]); 
   string smtpserver(argv[3]); 
 
   if(to.length() < 2 || from.length() < 2 || smtpserver.length() < 2) { 
      Usage(); 
      return 0; 
   } 
 
   char str[2048]; 
   cout << “Please enter the subject of the mail\n”; 
   cin.getline(str, 500);     
   string subject(str); 
   strcpy(str, “”); 
 
   cout << “Please enter the message body end with \”.\” on a line by itself\n”; 
   string mailmessage; 
   while(true) { 
      cin.getline(str, 2048); 
      if(!strcmp(str, “.”)) 
         break; 
         
      mailmessage += str; 
      mailmessage += “\r\n”; 
      strcpy(str, “”); 
   } 
 
   cout << “\nPlease wait sending mail\n”; 
   // This is how to tell the mailer class that we are using a direct smtp server 
   // preventing the code from doing an MX lookup on ’smtpserver’ i.e. passing 
   // false as the last parameter. 
   jwsmtp::mailer mail(to.c_str(), from.c_str(), subject.c_str(), mailmessage.c_str(), 
                       smtpserver.c_str(), jwsmtp::mailer::SMTP_PORT, false); 
 
   mail.username(“suei8423@163.com”); 
   mail.password(“###”); 
   // using a local file as opposed to a full path. 
   mail.attach(“/root/Desktop/223901.jpg”); 
 
   // add another recipient (carbon copy) 
   //mail.addrecipient(“someoneelse@somewhere.net”, mailer::Cc); 
 
   // set a new smtp server! This is the same as setting a nameserver. 
   // this depends on the constructor call. i.e. in the constructor 
   // If MXLookup is true this is a nameserver 
   // If MXLookup is false this is an smtp server 
   //mail.setserver(“mail.somewherefun.com”); 
   // same again except using an IP address instead. 
   //mail.setserver(“192.168.0.1″); 
 
   // boost::thread thrd(mail); 
   // thrd.join(); // optional 
   // or:- 
 
   // Use authentication 
   //mail.username(“testuser”); 
   //mail.password(“secret”); 
   // LOGIN authentication by default 
   // if you want plain as opposed to login authentication 
   //mail.authtype(jwsmtp::mailer::PLAIN); 
       
   mail.operator()(); 
   cout << mail.response() << “\n\n”; 
 
   //mail.reset(); // now we can mail someone else. 
   //mail.setmessage(“flibbletooting”); 
   //mail.setsubject(“another message same object”); 
   //mail.attach(“/home/user1/image.gif”); 
   // or a win example 
   //mail.attach(“C:\\image.gif”); 
   //mail.addrecipient(“someoneelseagain@foobar.net”); 
 
   //mail.operator ()(); 
   //cout << mail.response() << “\n”; 
   return 0; 
}

作者提供的库要自己编译一次.
在编译例子程序时,WINDOWS的例子可以直接编译,在LINUX下要用类似下面的命令
g++ demo2.cpp -o demo2 -I/usr/local/include/jwsmtp-1.32 /usr/local/lib/libjwsmtp.a

目录
相关文章
|
1月前
|
存储 C++ 容器
C++STL(标准模板库)处理学习应用案例
【4月更文挑战第8天】使用C++ STL,通过`std:vector`存储整数数组 `{5, 3, 1, 4, 2}`,然后利用`std::sort`进行排序,输出排序后序列:`std:vector<int> numbers; numbers = {5, 3, 1, 4, 2}; std:sort(numbers.begin(), numbers.end()); for (int number : numbers) { std::cout << number << " "; }`
21 2
|
2月前
|
存储 算法 安全
深入理解C++中的std::chrono库:持续时间的比较与应用
深入理解C++中的std::chrono库:持续时间的比较与应用
54 1
|
2月前
|
算法 数据处理 C++
【C++ 20 新特性 算法和迭代器库的扩展和泛化 Ranges】深入浅出C++ Ranges库 (Exploring the C++ Ranges Library)
【C++ 20 新特性 算法和迭代器库的扩展和泛化 Ranges】深入浅出C++ Ranges库 (Exploring the C++ Ranges Library)
112 1
|
2月前
|
存储 监控 安全
【深入探究C++日志库写入策略】glog、log4cplus与spdlog的写入策略比较
【深入探究C++日志库写入策略】glog、log4cplus与spdlog的写入策略比较
114 0
|
1天前
|
机器学习/深度学习 JSON 编译器
C++ 资源大全:标准库、Web框架、人工智能等 | 最全整理
C++ 资源列表,内容包括: 标准库、Web应用框架、人工智能、数据库、图片处理、机器学习、日志、代码分析等
14 1
|
7天前
|
JSON Java Linux
【探索Linux】P.30(序列化和反序列化 | JSON序列化库 [ C++ ] )
【探索Linux】P.30(序列化和反序列化 | JSON序列化库 [ C++ ] )
21 2
|
7天前
|
存储 安全 算法
【C++入门到精通】 原子性操作库(atomic) C++11 [ C++入门 ]
【C++入门到精通】 原子性操作库(atomic) C++11 [ C++入门 ]
15 1
|
7天前
|
算法 安全 调度
【C++入门到精通】 线程库 | thread类 C++11 [ C++入门 ]
【C++入门到精通】 线程库 | thread类 C++11 [ C++入门 ]
16 1
|
14天前
|
存储 算法 C++
详解C++中的STL(标准模板库)容器
【4月更文挑战第30天】C++ STL容器包括序列容器(如`vector`、`list`、`deque`、`forward_list`、`array`和`string`)、关联容器(如`set`、`multiset`、`map`和`multimap`)和容器适配器(如`stack`、`queue`和`priority_queue`)。它们为动态数组、链表、栈、队列、集合和映射等数据结构提供了高效实现。选择合适的容器类型可优化性能,满足不同编程需求。
|
20天前
|
存储 算法 程序员
C++从入门到精通:2.2.1标准库与STL容器算法深度解析
C++从入门到精通:2.2.1标准库与STL容器算法深度解析