初版
This commit is contained in:
commit
c3f3188bf2
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
build
|
||||
*.user
|
39
CMakeLists.txt
Normal file
39
CMakeLists.txt
Normal file
@ -0,0 +1,39 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
project(mail-cpp LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
set(CMAKE_PREFIX_PATH
|
||||
${CMAKE_PREFIX_PATH}
|
||||
"/data/data/com.termux/files/home/pocolib"
|
||||
)
|
||||
|
||||
# QT 中使用 VCPKG 在项目中的 Initial Configure 中设置
|
||||
# CMAKE_TOOLCHAIN_FILE
|
||||
# E:/vcpkg/scripts/buildsystems/vcpkg.cmake
|
||||
# 然后点击 Re-configure with initial parameters
|
||||
# -DCMAKE_TOOLCHAIN_FILE:STRING=E:/vcpkg/scripts/buildsystems/vcpkg.cmake
|
||||
|
||||
if (MSVC)
|
||||
if (GBK)
|
||||
add_compile_options(/source-charset:utf-8)
|
||||
else()
|
||||
add_compile_options(/utf-8)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
find_package(Poco REQUIRED NetSSL Foundation Zip)
|
||||
add_executable(mail-cpp main.cpp)
|
||||
|
||||
target_link_libraries(mail-cpp PRIVATE
|
||||
Poco::NetSSL
|
||||
Poco::Foundation
|
||||
Poco::Zip
|
||||
)
|
||||
include(GNUInstallDirs)
|
||||
install(TARGETS mail-cpp
|
||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
||||
)
|
109
main.cpp
Normal file
109
main.cpp
Normal file
@ -0,0 +1,109 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <Poco/Net/MailMessage.h>
|
||||
#include <Poco/Net/SMTPClientSession.h>
|
||||
#include <Poco/Net/SecureSMTPClientSession.h>
|
||||
#include <Poco/Net/NetException.h>
|
||||
#include <Poco/Net/FilePartSource.h>
|
||||
#include <Poco/Net/StringPartSource.h>
|
||||
#include <Poco/Path.h>
|
||||
#include <Poco/Zip/Compress.h>
|
||||
#include <Poco/FileStream.h>
|
||||
#include <Poco/File.h>
|
||||
|
||||
constexpr int port = 25;
|
||||
constexpr auto sender{"taynpg@163.com"};
|
||||
constexpr auto subject{"转tangjie收"};
|
||||
constexpr auto attach_name{"转tangjie收.zip"};
|
||||
constexpr auto smtp_server{ "smtp.163.com"};
|
||||
|
||||
bool compress(const Poco::Path& dirPath, std::string& out_file) {
|
||||
// 获取目录路径的字符串表示
|
||||
std::string dirStr = dirPath.toString();
|
||||
|
||||
// 构造压缩文件路径 /a/b/xx.zip
|
||||
Poco::Path zipPath(dirPath);
|
||||
Poco::Path parent_path = zipPath.parent();
|
||||
parent_path.setFileName(attach_name);
|
||||
|
||||
// 获取压缩文件的字符串表示
|
||||
out_file = parent_path.toString();
|
||||
|
||||
try {
|
||||
// 检查压缩文件是否存在,如果存在则删除
|
||||
Poco::File zipFileObj(out_file);
|
||||
if (zipFileObj.exists()) {
|
||||
std::cout << "存在同名压缩文件,删除中..." << std::endl;
|
||||
zipFileObj.remove();
|
||||
}
|
||||
|
||||
// 创建压缩文件的输出流
|
||||
Poco::FileOutputStream zipStream(out_file);
|
||||
|
||||
// 创建压缩对象
|
||||
Poco::Zip::Compress compress(zipStream, true); // true 表示递归压缩子目录
|
||||
|
||||
// 添加目录及其内容到压缩包
|
||||
compress.addRecursive(dirStr, Poco::Zip::ZipCommon::CL_NORMAL);
|
||||
|
||||
// 关闭压缩流
|
||||
compress.close();
|
||||
|
||||
std::cout << "压缩完成:" << out_file << std::endl;
|
||||
return true;
|
||||
} catch (Poco::Exception& e) {
|
||||
std::cerr << "压缩过程中发生异常:" << e.displayText() << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool send(const std::string& file, const std::string& pwd, const std::string& recipient)
|
||||
{
|
||||
try {
|
||||
Poco::Net::MailMessage message;
|
||||
message.setSender(sender);
|
||||
message.addRecipient(Poco::Net::MailRecipient(Poco::Net::MailRecipient::PRIMARY_RECIPIENT, recipient));
|
||||
message.setSubject(subject);
|
||||
message.addContent(new Poco::Net::StringPartSource(subject), Poco::Net::MailMessage::ENCODING_8BIT);
|
||||
|
||||
auto file_content = new Poco::Net::FilePartSource(file, "application/zip");
|
||||
message.addAttachment(attach_name, file_content);
|
||||
|
||||
Poco::Net::SecureSMTPClientSession smtp(smtp_server, port);
|
||||
smtp.login(Poco::Net::SMTPClientSession::LoginMethod::AUTH_LOGIN, sender, pwd);
|
||||
smtp.sendMessage(message);
|
||||
|
||||
smtp.close();
|
||||
}
|
||||
catch (Poco::Net::NetException& e) {
|
||||
std::cerr << "error: " << e.displayText() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc != 4) {
|
||||
std::cout << "first: dir_path, second:pwd, third: recipient." << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if _WIN32
|
||||
system("chcp 65001");
|
||||
#endif
|
||||
|
||||
std::string out_file{};
|
||||
if (!compress(argv[1], out_file)) {
|
||||
return 0;
|
||||
}
|
||||
if (send(out_file, argv[2], argv[3])) {
|
||||
std::cout << "Send Success." << std::endl;
|
||||
}
|
||||
else {
|
||||
std::cout << "Send Failed." << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user